使用 Eclipse (Java) 连接到 SQLite 数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2834247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:24:46  来源:igfitidea点击:

Connect to SQLite Database using Eclipse (Java)

javaeclipsesqlite

提问by bnabilos

I'm trying to connect to SQLite database with Ecplise but I have some errors. This is my Java code and the errors that I get on output. Please see if you can help me. Thank you in advance.

我正在尝试使用 Ecplise 连接到 SQLite 数据库,但出现了一些错误。这是我的 Java 代码和我在输出中得到的错误。请看看你能不能帮我。先感谢您。


package jdb;
import java.sql.*;
public class Test {
  public static void main(String[] args) throws Exception {
    Class.forName("org.sqlite.JDBC");
    Connection conn = DriverManager.getConnection("jdbc:sqlite:/Applications/MAMP/db/sqlite/test.sqlite");
    Statement stat = conn.createStatement();
    stat.executeUpdate("drop table if exists people;");
    stat.executeUpdate("create table people (name, occupation);");
    PreparedStatement prep = conn.prepareStatement("insert into people values (?, ?);");

prep.setString(1, "Gandhi");
prep.setString(2, "politics");
prep.addBatch();
prep.setString(1, "Turing");
prep.setString(2, "computers");
prep.addBatch();
prep.setString(1, "Wittgenstein");
prep.setString(2, "smartypants");
prep.addBatch();

conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);

ResultSet rs = stat.executeQuery("select * from people;");
while (rs.next()) {
  System.out.println("name = " + rs.getString("name"));
  System.out.println("job = " + rs.getString("occupation"));
}
rs.close();
conn.close();

} }

ans that what I get in Ecplise :

这就是我在 Ecplise 中得到的:


Exception in thread "main" java.lang.ClassNotFoundException: org.sqlite.JDBC
    at java.net.URLClassLoader.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at jdb.Test.main(Test.java:7)

Thank you

谢谢

采纳答案by Taneli Waltari

Make sure you have sqlitejdbc-v056.jar in your Eclipse classpath

确保你的 Eclipse 类路径中有 sqlitejdbc-v056.jar

回答by anudeep

While can retrieve the database in this way also but their is simple way to set the database like download the SQLlite database browser then u can easily upgrade the data. if u gonna go in this way then i will get back to u soon.

虽然也可以通过这种方式检索数据库,但它们是设置数据库的简单方法,例如下载 SQLlite 数据库浏览器,然后您可以轻松升级数据。如果你要走这条路,那么我很快就会回复你。