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

标签 java mysql jdbc ormlite

我一直在尝试创建一个 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/

相关文章:

java - 在 spring Controller 中找不到请求映射

java - 反对注解的论点

PHP:一个 mysql_query 语句中的多个 SQL 查询

mysql - 从具有某些条件的两个相同表中获取记录

mysql - 如何使用 Java 在不同线程或不同进程上运行不同的 sql 查询

java - Spring Boot 外部化属性不起作用

java - Spring Cloud DataFlow Local-Server 1.2.2 无法在 Windows 10 上启动

mysql - Debezium CDC Connector 任务出现错误 : javax. management.InstanceAlreadyExistsException

java - 无法在 Netbeans 中连接 SQL 数据库

java - 为 JDBC 连接获取 Null