2024-02-08 06:00:05

Java和SQLite

我被单个文件数据库所提供的简洁所吸引。在Java中连接和使用SQLite的驱动程序/连接器库是什么?

我发现了一个包装器库http://www.ch-werner.de/javasqlite,但是还有其他更突出的项目可用吗?


维基上列出了更多的包装:

Java wrapper (around a SWIG interface): http://tk-software.home.comcast.net/ A good tutorial to use JDBC driver for SQLite. (it works at least !) http://www.ci.uchicago.edu/wiki/bin/view/VDS/VDSDevelopment/UsingSQLite Cross-platform JDBC driver which uses embedded native SQLite libraries on Windows, Linux, OS X, and falls back to pure Java implementation on other OSes: https://github.com/xerial/sqlite-jdbc (formerly zentus) Another Java - SWIG wrapper. It only works on Win32. http://rodolfo_3.tripod.com/index.html sqlite-java-shell: 100% pure Java port of the sqlite3 commandline shell built with NestedVM. (This is not a JDBC driver). SQLite JDBC Driver for Mysaifu JVM: SQLite JDBC Driver for Mysaifu JVM and SQLite JNI Library for Windows (x86) and Linux (i386/PowerPC).

我在用SQLite和Java搜索信息时发现了你的问题。我只是想加上我的答案,我也在我的博客上发表了。

我已经用Java编程有一段时间了。我也知道SQLite,但从未使用过……我在其他应用程序中使用过,但从未在我编写的应用程序中使用过。所以这周我的一个项目需要它,它是如此简单的使用!

我找到了一个用于SQLite的Java JDBC驱动程序。只需将JAR文件添加到类路径并导入java.sql.*

他的测试应用程序将创建一个数据库文件,发送一些SQL命令来创建一个表,在表中存储一些数据,并读取数据并在控制台上显示。它将在项目的根目录中创建test.db文件。您可以使用java -cp .:sqlitejdbc-v056.jar Test运行此示例。

package com.rungeek.sqlite;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

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

我知道你特别问了SQLite,但也许HSQL数据库更适合Java。它本身是用Java编写的,运行在JVM中,支持内存表等,所有这些特性都使它非常适合原型设计和单元测试。

有一个新项目SQLJet,它是SQLite的纯Java实现。它还不支持所有的SQLite特性,但是对于一些使用SQLite数据库的Java项目来说可能是一个非常好的选择。

示例代码导致Tomcat中的内存泄漏(在取消部署webapp后,类加载器仍然保留在内存中),这将最终导致内存溢出。解决方法是使用sqlite-jdbc-3.7.8.jar;这是一个快照,所以maven还没有显示。

java -cp .:sqlitejdbc-v056.jar

Java -cp .:sqlitejdbc-v056.jar;测试

注意“。jar”后面的分号,我希望这能帮助到大家,可能会给大家带来很多麻烦

在编译和运行代码时,应该设置类路径选项值。 就像下面这样:

javac -classpath .;sqlitejdbc-v056.jar Text.java

java -classpath .;sqlitejdbc-v056.jar Text

请注意“。”和“分隔”;(赢了,Linux是“:)”)

David Crawshaw项目(sqlitejbc -v056.jar)似乎过时了,最后一次更新是2009年6月20日,来源这里

我推荐Xerials的Crawshaw sqlite包装叉。我用Xerials sqlite-jdbc-3.7.2.jar文件替换了sqlitejdbc-v056.jar,没有任何问题。

使用与伯尼的回答相同的语法,速度更快,并使用最新的sqlite库。

与Zentus的SQLite JDBC有什么不同?

The original Zentus's SQLite JDBC driver http://www.zentus.com/sqlitejdbc/ itself is an excellent utility for using SQLite databases from Java language, and our SQLiteJDBC library also relies on its implementation. However, its pure-java version, which totally translates c/c++ codes of SQLite into Java, is significantly slower compared to its native version, which uses SQLite binaries compiled for each OS (win, mac, linux). To use the native version of sqlite-jdbc, user had to set a path to the native codes (dll, jnilib, so files, which are JNDI C programs) by using command-line arguments, e.g., -Djava.library.path=(path to the dll, jnilib, etc.), or -Dorg.sqlite.lib.path, etc. This process was error-prone and bothersome to tell every user to set these variables. Our SQLiteJDBC library completely does away these inconveniences. Another difference is that we are keeping this SQLiteJDBC libray up-to-date to the newest version of SQLite engine, because we are one of the hottest users of this library. For example, SQLite JDBC is a core component of UTGB (University of Tokyo Genome Browser) Toolkit, which is our utility to create personalized genome browsers.

编辑:像往常一样,当你更新一些东西时,在你的代码中一些模糊的地方会有问题(发生在我身上)。测试测试测试=)

Sqlitejdbc代码可以使用git从https://github.com/crawshaw/sqlitejdbc下载。

# git clone https://github.com/crawshaw/sqlitejdbc.git sqlitejdbc
...
# cd sqlitejdbc
# make

注意:Makefile需要curl二进制文件来下载sqlite库/deps。