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

时间:2010-05-14 12:52:29

标签: java eclipse sqlite

我正在尝试使用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();

} }

我在Ecplise中获得的内容:

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();

谢谢

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

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