[SQLiteJDBC] Configuration

AJ Weber aweber at comcast.net
Wed Jan 13 11:11:50 EST 2010


Here's some code.  Put this anywhere that it'll get executed ONCE, before 
you need access to the database:

    private Connection conn = null; //this is good as a class-level variable 
(not in the method you're putting the rest of this code in)

//snippet for your main entry-point or anytime ONCE, before you try to call 
openDatabase().  You can even put it in a static { } block of the class.
        try {
            Class.forName("org.sqlite.JDBC");
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


//HERE ARE TWO HELPER METHODS TO CALL FROM YOUR "main" entry point
//these use a Logger (log4j in my case, but could use java's), so 
comment-out those lines or replace them with your own logging cmds
//NOTE: This is a little more complex than it needs to be, because I'm 
enabling the shared_cache feature, because I run multi-threaded (but it 
won't hurt you).

    protected boolean openDatabase(final String dbPath)
    {
        final String jdbcPrefix = "jdbc:sqlite:";
        boolean bRet = false;

        Properties ac = new Properties();
        ac.put("shared_cache", "true");

        try {
            if (conn != null)
                return (!conn.isClosed());

            if ((dbPath == null) || (dbPath.length() ==0))
                return bRet;

            String openStr = jdbcPrefix + dbPath;

            Driver jdbcDriver = DriverManager.getDriver(openStr);
            if (jdbcDriver == null) {
                logCat.error("Could not find driver for database");
                return false;
            }
            conn = jdbcDriver.connect(openStr, ac);
            //conn = DriverManager.getConnection(openStr, ac);

            bRet = !conn.isClosed();

            if (bRet) {
                //close/reopen to workaround SqliteJDBC "bug" in shared 
cache enablement
                conn.close();
                conn = jdbcDriver.connect(openStr, ac);
                conn.setAutoCommit(false);
            }
        }
        catch (SQLException e) {
            logCat.error("Could not open sqlite db", e);
            return false;
        }

        return bRet;
    }


    public void closeDatabase()
    {
        try {
            if ((conn != null) && (!conn.isClosed()))
                conn.close();
        }
        catch (SQLException e) {
            logCat.error("Could not close sqlite database properly", e);
        }
    }


----- Original Message ----- 
From: "Andy Wilbourn" <awilbourn at gmail.com>
To: <sqlitejdbc at lists.hcoop.net>
Sent: Wednesday, January 13, 2010 10:14 AM
Subject: [SQLiteJDBC] Configuration


>I am very new to Java as a whole. I have managed to be successful with the
> language when I have the IDE working. So I am asking if someone can tell 
> me
> how to configure a project to work with the JDBC driver. I have tried
> Eclipse and NetBeans, but both I get an error that the class name cannot 
> be
> resolved.
>
>
>
> I believe it is just come simple thing I have not done to get the code to
> even compile it complains for the Class.forName("org.sqlite.JDBC"); line.
>
>
>
> Thanks.
>
> _______________________________________________
> SQLiteJDBC mailing list
> SQLiteJDBC at lists.hcoop.net
> https://lists.hcoop.net/listinfo/sqlitejdbc
> 




More information about the SQLiteJDBC mailing list