【发布时间】:2015-04-29 02:55:33
【问题描述】:
目前正在尝试使用作为作业的一部分提供给我的sqlite-dbc4-3.8.2-SNAPSHOT.jar。我尝试运行我的主文件,但出现以下错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
org.sqlite.JDBC cannot be resolved to a variable
at DbBasic.open(DbBasic.java:54)
at DbBasic.<init>(DbBasic.java:67)
at DbUser.<init>(DbUser.java:40)
at Main.go(Main.java:12)
at Main.main(Main.java:65)
这是DbBasic 类的一部分,它尝试使用 JDBC 连接和打开数据库:
private Connection getConnection()
// get the connection
{
Connection con = null;
try {
con = DriverManager.getConnection(SQLITE_DATABASE_LOCATION+dbName);
con.setAutoCommit(false);
}
catch (SQLException sqle)
{
notify("Db.getConnection database location ["+SQLITE_DATABASE_LOCATION+"] db name["+dbName+"]", sqle);
};
return con;
} // end of method "getConnection"
private void open()
// "open" the database : actually really setting up the connection and obtaining the metadata about the server
// makes sure that database file is present before trying to establish connection
// otherwise SQLite will create a new, empty database with the name provided
{
File dbf = new File(dbName);
if (dbf.exists() == false)
{
System.out.println("SQLite database file ["+dbName+"] does not exist");
System.exit(0);
};
try {
Class.forName(org.sqlite.JDBC);
con = getConnection();
} catch ( ClassNotFoundException cnfe ) {
notify("Db.Open",cnfe);
};
if (debug) System.out.println("Db.Open : leaving");
} // end of constructor "Open"
我已经尝试添加外部 JAR,然后将 .jar 文件添加到我在 Eclipse 中的“引用库”中。
我无法理解 Class.forName(org.sqlite.JDBC) 以及如何使其与我的 .jar 文件一起使用
【问题讨论】:
-
你是如何构建你的项目的?您需要使用 maven 或 ant 构建来使用您的依赖项设置您的项目。
-
我已将 Eclipse 设置为“自动构建”
-
这不是我在 maven build 上阅读的问题的答案。您需要使用它来构建您的项目。
-
Class.forName() 接受一个字符串参数,试试 Class.forName("org.sqlite.JDBC"); (带引号)
标签: java eclipse sqlite jdbc classpath