12

我试图让 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 文件添加到您的构建路径或类路径中,我相信我已经做到了,但没有任何解决问题的方法。任何帮助,将不胜感激。谢谢。

4

7 回答 7

11

感谢用户 phew 的帮助/想法。

我错过了Xerial 网站上关于示例程序的明显命令行说明。为了让程序从命令行运行,我必须将 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,只是更短。

于 2013-08-09T15:26:37.277 回答
4

您可以通过将您的项目转换为 maven 并将依赖项(来自https://mvnrepository.com)转换为您的pom.xmlas 来添加它:

</dependency>
    <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
    <dependency>
        <groupId>org.xerial</groupId>
        <artifactId>sqlite-jdbc</artifactId>
        <version>3.34.0</version>
    </dependency>
于 2021-02-04T15:59:14.703 回答
2

当您处理桌面 JAVA 应用程序时,上述所有解决方案都运行良好。对于 WebAPP 应用程序,上述解决方案将不起作用。实际上这是您的应用服务器的问题,即您需要在 WEB-INF/lib 下添加 sqlite-jar,然后只有您才能成功运行您的 webapp。

为此,您可以按照以下步骤操作:

去:

项目-> 属性-> 部署程序集-> 添加-> 来自文件系统的存档-> 下一步-> 添加

导航到您拥有 sqlite-jar 的文件夹,选择它并点击 OK。

单击完成。好的。

完成,您现在应该可以运行您的应用程序了。

谢谢

于 2017-07-13T22:26:16.323 回答
1

对我有用的一种方法是转到 JRE 系统库 -> 右键单击​​ -> 构建路径 -> 配置构建路径 -> 添加外部 jars -> 选择 jar 并编译。

于 2015-02-03T16:13:45.717 回答
1

首先你可以下载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
于 2017-01-12T10:55:31.393 回答
0

如前所述,将 jar 文件添加到您的项目中。

对于Android Studio / IntelliJ,您只需这样做:

文件 > 项目结构 >

在此处输入图像描述

新模块 >

在此处输入图像描述

导入 .JAR/.AAR 包

在此处输入图像描述

现在选择/添加您的 .jar 文件,让 IntelliJ 完成剩下的工作。

于 2017-04-05T14:44:24.480 回答
0

第 1 步:

复制 sqlite 库。


第 2 步:

将库粘贴到“WebContent/WEB-INF/lib”目录中,您也可以通过在 eclipse 中选择“lib”文件夹并按 Ctrl +来完成 V

第3步:

重新启动服务器,希望问题应该得到解决


于 2018-07-25T15:33:11.740 回答