![](/img/trans.png)
[英]Sqlite database ..java.lang.ClassNotFoundException:org.sqlite.jdbc
[英]java.lang.ClassNotFoundException: org.sqlite.JDBC error in Sample.java program from xerial
我試圖讓 Xerial 的 Sample 類在 Eclipse 中使用 sqlite 工作,但我不斷收到錯誤“ClassNotFoundException: org.sqlite.JDBC”
我從https://bitbucket.org/xerial/sqlite-jdbc/downloads下載了 sqlite-jdbc-3.7.2.jar 文件。 將其復制到我在eclipse中的項目“database_test”下的lib文件夾中。 然后右鍵Project->Properties->Java Build Path->Libraries Tab->Add JARs->選擇jar文件。 我正在嘗試從此處找到的 Xerial 執行此代碼: https ://bitbucket.org/xerial/sqlite-jdbc#markdown-header-usage
// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");
Connection connection = null;
try
{
// create a database connection
connection = DriverManager.getConnection("jdbc:sqlite:sample.db");
Statement statement = connection.createStatement();
statement.setQueryTimeout(30); // set timeout to 30 sec.
statement.executeUpdate("drop table if exists person");
statement.executeUpdate("create table person (id integer, name string)");
statement.executeUpdate("insert into person values(1, 'leo')");
statement.executeUpdate("insert into person values(2, 'yui')");
ResultSet rs = statement.executeQuery("select * from person");
while(rs.next())
{
// read the result set
System.out.println("name = " + rs.getString("name"));
System.out.println("id = " + rs.getInt("id"));
}
}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
}
finally
{
try
{
if(connection != null)
connection.close();
}
catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}
} }
我去過的每個站點都說將 jar 文件添加到您的構建路徑或類路徑,我相信我已經這樣做了,但沒有解決問題。 任何幫助,將不勝感激。 謝謝。
感謝用戶 phew 的幫助/想法。
我錯過了Xerial 網站上關於 Sample program的明顯命令行說明。 為了讓程序從命令行運行,我必須將 JAR 文件復制到與 .CLASS 文件相同的文件夾中。 然后運行以下命令:
java -classpath ".:sqlite-jdbc-(VERSION).jar" Sample
引號內是多個路徑,在 Unix 下用冒號 ( :
) 分隔;
在 Windows 下用分號 ( ;
) 分隔。 作為路徑之一的點很重要 - 僅命名 JAR 文件是不夠的。 在 Windows 上的完整調用將是:
"%JAVA_HOME%\bin\java.exe" -cp "sqlite-jdbc-(VERSION).jar;." Sample
注意分號而不是冒號。 路徑的順序並不重要, -cp
與-classpath
的作用相同,只是更短。
您可以通過將您的項目轉換為 maven 並將依賴項(來自https://mvnrepository.com )轉換為您的pom.xml
來添加它:
</dependency>
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.34.0</version>
</dependency>
當您處理桌面 JAVA 應用程序時,上述所有解決方案都運行良好。 對於 WebAPP 應用程序,以下解決方案將不起作用。 其實這是你的應用服務器的問題,那就是你需要在你的 WEB-INF/lib 下添加 sqlite-jar,然后只有你才能成功運行你的 webapp。
為此,您可以按照以下步驟操作:
去:
項目-> 屬性-> 部署程序集-> 添加-> 來自文件系統的檔案-> 下一步-> 添加
導航到您的 sqlite-jar 所在的文件夾,選擇它並點擊 OK。
單擊完成。 行。
完成,您現在應該可以運行您的應用程序了。
謝謝
對我有用的一種方法是轉到 JRE 系統庫 -> 右鍵單擊 -> 構建路徑 -> 配置構建路徑 -> 添加外部 jar -> 選擇 jar 並編譯。
首先你可以下載sqlite-jdbc-3.8.11.2然后在你的項目中添加 jar 文件
Window > Show_view > Package Explorer
step-1: right click on referenced libraries
step-2: select Built path
step-3: configure Built path
step-4: Add External jars file
step-5: add sqlite-jdbc-3.8.11.2
step-6: ok
或第二種方法有用
您的項目可以設置類路徑:Lib/sqlite-jdbc-3.8.11.2.jar
1)create a folder "Lib" in your project workspace
2)"Lib" folder paste jar file like "sqlite-jdbc-3.8.11.2.jar"
3)After Double click on your project file i.e. 'MANIFEST.MF'
4)select Runtime(Bottom view panel)
5)plug-in Classpath(Bottom_right position) to add jar file like "Lib/sqlite-jdbc-3.8.11.2.jar"
this working 100% sure..thanks
第1步:
復制 sqlite 庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.