tags:

views:

23966

answers:

6
+31  Q: 

Java and SQLite

I'm attracted to the neatness that a single file database provides. What driver/connector library is out there to connect and use SQLite with Java.

I've discovered a wrapper library, http://www.ch-werner.de/javasqlite, but are there other more prominent projects available?

+31  A: 

The wiki lists some more wrappers:

Peter Hoffmann
I recommend the mysaifu version. It works on Sun's JVM for windows (not just mysaifu) and is the only stable wrapper I know of that implements commit hooks.
finnw
I recommend the one from http://www.zentus.com/sqlitejdbc -- it is extremely easy to get started with as it comes packaged as a single JAR including native SQLite binaries for most platforms.
kdt
My addition to this list is sqlite4java - http://code.google.com/p/sqlite4java - it's a wrapper (no JDBC); precompiled for Windows, Mac, Linux. It's simple to use and it enforces some contracts to help the developer avoid misusing SQLite.
sereda
sqlite4java looks interesting, but they also have a great comparison of the various wrappers out there: http://code.google.com/p/sqlite4java/wiki/ComparisonToOtherWrappers
Scott Bennett-McLeish
+5  A: 

G'day,

Just thought I'd say be careful with SQLite in multi-user type installations. SQLite has no row locking for write updates and the DB itself is implemented as a single monolithic file.

Write locking is implemented as file locks across the whole DB.

Just thought I'd mention this because I'd been bitten by it before.

cheers,

Rob

Rob Wells
Yes I'm fully aware of multi-user usage of these smaller DBs. Mainly I'm interested in using it as a simple development and prototyping environment, upon which I'd upgrade to a bigger, beefier engine such as SQL Svr or MySQL.
Scott Bennett-McLeish
+21  A: 

Hey I found your questions while search for information with SQLite and Java. Just thought I add my answer which I also posted on my blog.

I have been coding in Java for a while know. I have also known about SQLite but never used it… Well I have have used it through other applications but never in an app that I coded. So I needed it for a project this week and its so simple use!

First I found the website of David Crawshaw who has a Java JDBC driver for SQLite. Just add his JAR file to your classpath and import java.sql.*

His test app will create a database file, send some SQL commands to create a table, store some data in the table, and read it back and display on console. It will create the test.db file in the root directory of the project.

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();
    }
  }
Bernie Perez
+1,Nice answer :)
Mahesh
+5  A: 

I understand you asked specifically about SQLite, but maybe HSQL database would be a better fit with Java. It is written in Java itself, runs in the JVM, supports in-memory tables etc. and all that features make it quite usable for prototyping and unit-testing.

javashlook
Yes HSQL is a very good choice and I've used it quite extensively in a couple of client apps for good effect. In this instance however, I did indeed want to use SQLite.
Scott Bennett-McLeish
+3  A: 

There is a new project SQLJet that is a pure Java implementation of SQLite. It doesn't support all of the SQLite features yet, but may be a very good option for some of the Java projects that work with SQLite databases.

It looks promising but it appears that it doesn't offer an SQL query ability yet, kind of a deal breaker for me.
Scott Bennett-McLeish
A: 

Hi Scott,

So finally which Java wrapper for SQLite did you use?

vikramsjn
I've moved away from that project for the moment but in the end I stayed with the one I was using [http://www.ch-werner.de/javasqlite/].
Scott Bennett-McLeish
Actually the above posts mention a lot of Java wrappers, and Google search return this www.zentus.com/sqlitejdbc/ as the first result. So I was curious, which one were you using.Thanks for replying.
vikramsjn