Java與嵌入式數(shù)據(jù)庫SQLite的結(jié)合
最近研究了一下嵌入式數(shù)據(jù)庫,并使用Java與一個(gè)叫做SQLite的輕量級(jí)數(shù)據(jù)庫結(jié)合寫了個(gè)小程序,這個(gè)過程中也獲得了不少經(jīng)驗(yàn),下面來總結(jié)一下。
本來是決定用Flex寫的,因?yàn)樗龀龅慕缑姹容^美觀,但是寫完了界面發(fā)現(xiàn)連接數(shù)據(jù)庫這方面Flex還處于幼兒階段,而且支持的數(shù)據(jù)庫也不多….所以不得不放棄而轉(zhuǎn)向Java了。
首先解釋下為什么用嵌入式數(shù)據(jù)庫,一是程序比較小,數(shù)據(jù)也不多,二是對于用戶比較麻煩,安裝一個(gè)小程序還要安裝一個(gè)數(shù)據(jù)庫軟件。。。其次就是感覺有點(diǎn)大材小用了。
原來也寫了不少數(shù)據(jù)庫變成的小程序,但有的細(xì)節(jié)還是沒去研究,就像preparedStatement的executeUpdate()方法是返回一個(gè)整型數(shù),當(dāng)返回大于0的數(shù),表示更新了 返回值的這么多條記錄,而返回0時(shí)則有兩種情況:
(1) 所執(zhí)行的SQL語句是對數(shù)據(jù)庫管理系統(tǒng)的記錄進(jìn)行操作;并且沒有記錄被更新
(2) 所執(zhí)行的SQL語句是對數(shù)據(jù)庫管理系統(tǒng)的表、視圖等對象進(jìn)行操作的DDL語言,沒有數(shù)據(jù)記錄被直接修改。
下面介紹一下SQLite:
SQLite 是一款輕量級(jí)的、基于文件的嵌入式數(shù)據(jù)庫,2000年就已經(jīng)誕生,經(jīng)過7年多的發(fā)展,直到今天已經(jīng)成為最流行的嵌入式數(shù)據(jù)庫,包括google在內(nèi)的公司 在其桌面軟件中亦使用 SQLite 存儲(chǔ)用戶數(shù)據(jù)。由此可以看出,已經(jīng)沒有任何理由去懷疑SQLite的穩(wěn)定性了。
SQLite的優(yōu)勢在哪呢?
- 免配置,和access一樣,只要把數(shù)據(jù)庫文件通過ftp上傳到服務(wù)器上就可以使用,不需要服務(wù)器的額外支持
- . 備份方便,因?yàn)橹皇且粋€(gè)文件,只要復(fù)制一份該文件,就能備份整個(gè)數(shù)據(jù)庫
- 雖然是輕量級(jí)數(shù)據(jù)庫,但他支持最大 2tb 的單個(gè)庫文件。
- 快,無與倫比的快。經(jīng)過實(shí)際測試,在幾百萬記錄的情況下,SQLite的插入和查詢速度和 mysql 不分上下,快于 sql server,10倍于 access (但這并不意味著它可以替代 sql server 。
這個(gè)程序使用SQLite作為數(shù)據(jù)庫,嵌入在程中,但是在使用之前要下載它的驅(qū)動(dòng)sqlitejdbc-v054.jar。
然后將這個(gè)包導(dǎo)入你的工程,然后導(dǎo)入org.sqlite.JDBC包即可,驅(qū)動(dòng)程序名也是org.sqlite.JDBC,驅(qū)動(dòng)程序地址:jdbc:sqlite:/d:/test.db。其中/d:/test.db表示建立數(shù)據(jù)庫文件的地址和文件名。
最后給出一個(gè)測試程序,簡單易懂:
- 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!!");
- }
- }
好了,謝謝大家賞臉,睡覺時(shí)間到??!
原文鏈接:http://www.kankanews.com/ICkengine/archives/38027.shtml