gpt4 book ai didi

java - ORMLite MySQL 初始化似乎不起作用

转载 作者:行者123 更新时间:2023-11-29 22:17:54 26 4
gpt4 key购买 nike

我一直在尝试创建一个 ORMLite JdbcPooledConnectionSource 来连接到数据库。

这是我创建它的代码:

    try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

String url = String.format("jdbc:%s%s%s%s/%s",
hostname.startsWith("mysql://") ? "" : "mysql://", hostname,
port != "" ? ":" : "", port, databaseName);

try {
JdbcPooledConnectionSource connection = new JdbcPooledConnectionSource(
url, username, password);

connection.setMaxConnectionsFree(config.getInt(database
+ ".connections", 1));

connection.setTestBeforeGet(false);
connection.setMaxConnectionAgeMillis(900000);
connection.setCheckConnectionsEveryMillis(0);
connection.setDatabaseType(new MysqlDatabaseType());
connection.initialize();

return connection;
} catch (SQLException e) {
throw new ConnectionException(e);
}

这就是我使用它的地方。所有这些“存储”都实现 BaseDaoImpl 并将连接传递给它们的 super 构造函数:

public static void setupStorage() throws SQLException {
if(connection == null){
System.err.println("Could not set up storage. No connections avaliable.");
return;
}
playerBanStorage = new PlayerBanStorage(connection);
playerKickStorage = new PlayerKickStorage(connection);
playerMuteStorage = new PlayerMuteStorage(connection);
playerStorage = new PlayerStorage(connection);
playerWarnStorage = new PlayerWarnStorage(connection);
ipBanStorage = new IpBanStorage(connection);
playerBanRecordStorage = new PlayerBanRecordStorage(connection);
ipBanRecordStorage = new IpBanRecordStorage(connection);
playerMuteRecordStorage = new PlayerMuteRecordStorage(connection);
}

但是,我遇到了这个异常:

18:13:10 [Severe] java.sql.SQLException: Could not call the constructor in class class de.tbi.bans.storage.PlayerStorage
18:13:10 [Severe] at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22)
18:13:10 [Severe] at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:95)
18:13:10 [Severe] at com.j256.ormlite.field.FieldType.configDaoInformation(FieldType.java:297)
18:13:10 [Severe] at com.j256.ormlite.dao.BaseDaoImpl.initialize(BaseDaoImpl.java:201)
18:13:10 [Severe] at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:128)
18:13:10 [Severe] at com.j256.ormlite.dao.BaseDaoImpl.<init>(BaseDaoImpl.java:119)
18:13:10 [Severe] at de.tbi.bans.storage.IpBanStorage.<init>(IpBanStorage.java:29)
18:13:10 [Severe] at de.tbi.bans.Bans.setupStorage(Bans.java:50)
18:13:10 [Severe] at de.tbi.bans.runnables.Setup.run(Setup.java:11)
18:13:10 [Severe] at net.md_5.bungee.scheduler.BungeeTask.run(BungeeTask.java:63)
18:13:10 [Severe] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
18:13:10 [Severe] at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
18:13:10 [Severe] at java.lang.Thread.run(Unknown Source)
18:13:10 [Severe] Caused by: java.lang.reflect.InvocationTargetException
18:13:10 [Severe] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
18:13:10 [Severe] at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
18:13:10 [Severe] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
18:13:10 [Severe] at java.lang.reflect.Constructor.newInstance(Unknown Source)
18:13:10 [Severe] at com.j256.ormlite.dao.DaoManager.createDao(DaoManager.java:92)
18:13:10 [Severe] ... 11 more
18:13:10 [Severe] Caused by: java.lang.IllegalStateException: you must call initialize() before you can use the dao
18:13:10 [Severe] at com.j256.ormlite.dao.BaseDaoImpl.checkForInitialized(BaseDaoImpl.java:927)
18:13:10 [Severe] at com.j256.ormlite.dao.BaseDaoImpl.isTableExists(BaseDaoImpl.java:688)
18:13:10 [Severe] at de.tbi.bans.storage.PlayerStorage.<init>(PlayerStorage.java:39)
18:13:10 [Severe] ... 16 more

这是我不明白的异常(exception):

java.lang.IllegalStateException: you must call initialize() before you can use the dao

因为我清楚地初始化了连接!

connection.initialize();

还是我做错了什么?

最佳答案

好吧,我自己想出来了......

显然,您不仅必须为连接调用 initialize(),还必须为 BaseDaoImpl 调用。

关于java - ORMLite MySQL 初始化似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31033916/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com