2013-07-30 39 views
0

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

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,它们都给出相同的错误。

什么可能是错的?

回答

1

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

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

+0

确实是这个问题。我曾经在另一堂课中使用过它,但在另一堂课中却忘记了。我没有意识到它超出了范围。谢谢! – Jeremy