我想使用Java读取SQLite db文件。你能告诉我怎么读吗?
答案 0 :(得分:6)
查看David Crawshaw的网站,其中Java JDBC driver为SQLite。只需将他的JAR file添加到类路径并导入java.sql。*
他的测试应用程序将创建一个数据库文件,发送一些SQL命令来创建一个表,在表中存储一些数据,然后将其读回并在控制台上显示。它将在项目的根目录中创建test.db文件。
public class Test {
public static void main(String[] args) throws Exception {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db");
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();
}
}
答案 1 :(得分:3)
SQLite的Java JDBC驱动程序位于David Crawshaw站点http://www.zentus.com/sqlitejdbc/将jar文件添加到类路径并导入java.sql。*,你应该很好。