Java與嵌入式數據庫SQLite的結合

      最近研究了一下嵌入式數據庫,並使用Java與一個叫作SQLite的輕量級數據庫結合寫了個小程序,這個過程當中也得到了很多經驗,下面來總結一下。 java

 

      原本是決定用Flex寫的,由於它作出的界面比較美觀,可是寫完了界面發現鏈接數據庫這方面Flex還處於幼兒階段,並且支持的數據庫也很少....因此不得不放棄而轉向Java了。 mysql

 

      首先解釋下爲何用嵌入式數據庫,一是程序比較小,數據也很少,二是對於用戶比較麻煩,安裝一個小程序還要安裝一個數據庫軟件。。。其次就是感受有點大材小用了。 sql

 

      原來也寫了很多數據庫變成的小程序,但有的細節仍是沒去研究,就像preparedStatement的executeUpdate()方法是返回一個整型數,當返回大於0的數,表示更新了 返回值的這麼多條記錄,而返回0時則有兩種狀況: 數據庫

(1)  所執行的SQL語句是對數據庫管理系統的記錄進行操做;而且沒有記錄被更新 小程序

(2)  所執行的SQL語句是對數據庫管理系統的表、視圖等對象進行操做的DDL語言,沒有數據記錄被直接修改。 服務器

    

 

下面介紹一下SQLite: 測試

      SQLite 是一款輕量級的、基於文件的嵌入式數據庫,2000年就已經誕生,通過7年多的發展,直到今天已經成爲最流行的嵌入式數據庫,包括google在內的公司在其桌面軟件中亦使用 SQLite 存儲用戶數據。由此能夠看出,已經沒有任何理由去懷疑SQLite的穩定性了。 google

 

SQLite的優點在哪呢? spa

1. 免配置,和access同樣,只要把數據庫文件經過ftp上傳到服務器上就可使用,不須要服務器的額外支持 code

2. 備份方便,由於只是一個文件,只要複製一份該文件,就能備份整個數據庫

3. 雖然是輕量級數據庫,但他支持最大 2tb 的單個庫文件。

4. 快,無與倫比的快。通過實際測試,在幾百萬記錄的狀況下,SQLite的插入和查詢速度和 mysql 不分上下,快於 sql server,10倍於 access (但這並不意味着它能夠替代 sql server )

 

      這個程序使用SQLite做爲數據庫,嵌入在程中,可是在使用以前要下載它的驅動sqlitejdbc-v054.jar。

而後將這個包導入你的工程,而後導入org.sqlite.JDBC包便可,驅動程序名也是org.sqlite.JDBC,驅動程序地址:jdbc:sqlite:/d:/test.db。其中/d:/test.db表示創建數據庫文件的地址和文件名。

 

最後給出一個測試程序,簡單易懂:

package sqlitetest;
import java.sql.*;
//import SQLite.*;
import org.sqlite.JDBC;
public class TestConn {
    void test(){
        Connection conn = null;
        Statement stmt = null;
        ResultSet rset = null;
        System.out.println(new java.util.Date());
        try {  Class.forName("org.sqlite.JDBC");
        conn = DriverManager.getConnection( "jdbc:sqlite:/d:/test.db");
        conn.setAutoCommit(false);
        stmt = conn.createStatement();
        stmt.executeUpdate("create table hehe(id number, name varchar(32))");
        System.out.println("建表hehe成功!");
        for (int i=0; i<10000; i++) {
            System.out.print("插入條目i/n");
            System.out.println(stmt.executeUpdate("INSERT INTO hehe VALUES(" + i + ", '我愛中國" + i + "')"));
        }
        conn.commit();


        System.out.println("不建索引查詢:");
        System.out.println(new java.util.Date());
        rset = stmt.executeQuery("SELECT id, name FROM hehe where id>5");
        while (rset.next()){
            System.out.println(rset.getInt("id"));
            System.out.println(rset.getString("name"));
        }
        if (rset!=null){
            rset.close(); rset = null;
        }
        System.out.println(new java.util.Date());




        System.out.println("建索引:");
        System.out.println(new java.util.Date());
        stmt.executeUpdate("CREATE INDEX hehe_idx on hehe(id)");
        stmt.executeUpdate("CREATE INDEX hehe_idx2 on hehe(name)");
        conn.commit();
        System.out.println(new java.util.Date());
        System.out.println("建索引後的查詢:");
        System.out.println(new java.util.Date());
        rset = stmt.executeQuery("SELECT id, name FROM hehe where id > 5 ");
        while (rset.next()){
            System.out.println(rset.getInt("id"));
            System.out.println(rset.getString("name"));
        }
        System.out.println(new java.util.Date());
        stmt.executeUpdate("drop table hehe");
        System.out.println("刪除表hehe成功!");
        conn.commit();
        System.out.println(new java.util.Date());
        } catch(ClassNotFoundException cnfe)
        {
            System.out.println("Can´t find class for driver: " + cnfe.getMessage());
            System.exit(-1);
        } catch (SQLException e){
            System.out.println("SQLException :" + e.getMessage());
            System.exit(-1); }
        finally {
            try {
                if (rset!=null) rset.close();
                stmt.close();
                conn.close();
            } catch (SQLException e) { System.out.println("SQLException in finally :" + e.getMessage());
            System.exit(-1);} } }

public static void main(String[] args) {
    TestConn conn = new TestConn();
    conn.test();
    System.out.print("Success!!");
}
}

好了,謝謝你們賞臉,睡覺時間到!!

相關文章
相關標籤/搜索