【问题标题】:SQLite can't connect to DB from JarSQLite 无法从 Jar 连接到数据库
【发布时间】:2013-07-30 12:55:58
【问题描述】:

我正在编写一个需要加载本地 SQLite 数据库的 java 程序。在我打包并在另一台计算机上尝试之前,一切都按预期工作。数据库查询似乎是唯一的问题。尝试返回 “找不到适合 jdbc:sqlite:C:\Data\Test.db 的驱动程序”。该错误让我认为它正在访问 SQL 类,但没有正确加载驱动程序本身。

String db_string = "C:\Data\Test.db";
try{
    connection = DriverManager.getConnection("jdbc:sqlite:" + db_string);
    Statement statement = connection.createStatement();
    statement.setQueryTimeout(30); // 30 seconds
    ResultSet rs = statement.executeQuery("select * from " + table);
    while(rs.next()){
        // Read the results
        fileList.add(rs.getLong("modifieddate"));
    }
} catch (SQLException e){
    System.err.println(e.getMessage());
}
// Close the connection
finally {
    try{
        if(connection != null)
            connection.close();
    } catch (SQLException e){
        System.err.println(e);
    }
}

在 IntelliJ 中,我通过创建新工件并指定主类来构建 Jar。我用来读取/写入数据库信息的 SQLite 包由 IntelliJ 提取到 Jar 中。当我打开 Jar 时,我可以看到我的代码和 SQLite 代码以及它的 JDBC 驱动程序。

Manifest-Version: 1.0
Main-Class: com.myproject

我尝试过的其他计算机正在运行 Windows 8 和 Windows 7。它们都给出了相同的错误。

可能出了什么问题?

【问题讨论】:

    标签: java sqlite


    【解决方案1】:

    您没有通过调用 Class.forName(...) 来加载驱动程序类的静态初始化程序,并且由于您重新打包了 JAR,因此可能不包括自动检测驱动程序类所需的 SPI 清单。

    手动调用驱动程序或将原始 SQLite 驱动程序 JAR 添加到您的类路径中,而不是将其捆绑到单个 JAR 中。

    【讨论】:

    • 这确实是问题所在。我在另一个班级用过一次,但在另一个班级忘记了。我没有意识到它超出了范围。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2021-01-29
    • 2012-07-24
    • 1970-01-01
    相关资源
    最近更新 更多