我正在尝试在 Eclipse Luna 和 Windows 7 下使用 Sqlite、jdbc。
当我使用 Sqlite 数据库的绝对路径时,一切正常,但是当使用相对路径时,我得到这个错误:
java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database.
我花了一些时间在谷歌上搜索这个问题,答案是:是的,你可以在 jdbc 连接中使用相对路径。但是它对我不起作用。
我的代码:
package PortiaMoxy;
import static net.mindview.util.Print.*;
String inPath; // incoming file
String outPath; // converted file from incoming file
public File() {
// Connect to Sqlite db
Connection c = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("org.sqlite.JDBC");
//
// This connection works fine
//
//c = DriverManager.getConnection("jdbc:sqlite:/JavaProjects/workspace/Polymorphism/sources/PortiaMoxy/moxyimport.sqlite");
//
// This connection doesn't work. ???
//
c = DriverManager.getConnection("jdbc:sqlite:moxyimport.sqlite");
c.setAutoCommit(false);
print("File(): Opened database successfully.");
stmt = c.createStatement();
String query = "select ID \"id\", VALUE \"value\"";
query += "from infiles;";
rs = stmt.executeQuery(query);
if (!rs.isBeforeFirst() ) {
System.out.println("No data");
}
while(rs.next()) {
String id = rs.getString(1);
String value = rs.getString(2);
print(" ID = " + id + " Value = " + value);
} // end of while
rs.close();
stmt.close();
c.close();
} // end of try
catch (SQLException ex) {
print(ex.getClass().getName() + ": " + ex.getMessage());
System.exit(0);
}
catch (Exception e ) {
print(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
} // end of class
public int convert() {
print(what());
print("Generic convert");
return 0;
}
public String what() {
return "File";
}
}
最佳答案
将您的数据库放在以下文件夹中,然后使用相对路径重试:
/JavaProjects/workspace/Polymorphism
这应该有效。
关于java - sqlite jdbc Eclipse 数据库相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31611785/