乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Java與嵌入式數(shù)據(jù)庫SQLite的結(jié)合

       集微筆記 2013-09-04

      最近研究了一下嵌入式數(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ù)也不多,二是對(duì)于用戶比較麻煩,安裝一個(gè)小程序還要安裝一個(gè)數(shù)據(jù)庫軟件。。。其次就是感覺有點(diǎn)大材小用了。

      原來也寫了不少數(shù)據(jù)庫變成的小程序,但有的細(xì)節(jié)還是沒去研究,就像preparedStatement的executeUpdate()方法是返回一個(gè)整型數(shù),當(dāng)返回大于0的數(shù),表示更新了 返回值的這么多條記錄,而返回0時(shí)則有兩種情況:

      (1)  所執(zhí)行的SQL語句是對(duì)數(shù)據(jù)庫管理系統(tǒng)的記錄進(jìn)行操作;并且沒有記錄被更新

      (2)  所執(zhí)行的SQL語句是對(duì)數(shù)據(jù)庫管理系統(tǒng)的表、視圖等對(duì)象進(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)勢(shì)在哪呢?

      1.   免配置,和access一樣,只要把數(shù)據(jù)庫文件通過ftp上傳到服務(wù)器上就可以使用,不需要服務(wù)器的額外支持
      2. . 備份方便,因?yàn)橹皇且粋€(gè)文件,只要復(fù)制一份該文件,就能備份整個(gè)數(shù)據(jù)庫
      3.   雖然是輕量級(jí)數(shù)據(jù)庫,但他支持最大 2tb 的單個(gè)庫文件。
      4.   快,無與倫比的快。經(jīng)過實(shí)際測(cè)試,在幾百萬記錄的情況下,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è)測(cè)試程序,簡(jiǎn)單易懂:

      1. package sqlitetest; 
      2.  
      3. import java.sql.*; 
      4.  
      5. //import SQLite.*; 
      6.  
      7. import org.sqlite.JDBC; 
      8.  
      9. public class TestConn { 
      10.  
      11.     void test(){ 
      12.  
      13.         Connection conn = null
      14.  
      15.         Statement stmt = null
      16.  
      17.         ResultSet rset = null
      18.  
      19.         System.out.println(new java.util.Date()); 
      20.  
      21.         try {  Class.forName("org.sqlite.JDBC"); 
      22.  
      23.         conn = DriverManager.getConnection( "jdbc:sqlite:/d:/test.db"); 
      24.  
      25.         conn.setAutoCommit(false); 
      26.  
      27.         stmt = conn.createStatement(); 
      28.  
      29.         stmt.executeUpdate("create table hehe(id number, name varchar(32))"); 
      30.  
      31.         System.out.println("建表hehe成功!"); 
      32.  
      33.         for (int i=0; i<10000; i++) { 
      34.  
      35.             System.out.print("插入條目i/n"); 
      36.  
      37.             System.out.println(stmt.executeUpdate("INSERT INTO hehe VALUES(" + i + ", '我愛中國(guó)" + i + "')")); 
      38.  
      39.         } 
      40.  
      41.         conn.commit(); 
      42.  
      43.  
      44.  
      45.  
      46.  
      47.         System.out.println("不建索引查詢:"); 
      48.  
      49.         System.out.println(new java.util.Date()); 
      50.  
      51.         rset = stmt.executeQuery("SELECT id, name FROM hehe where id>5"); 
      52.  
      53.         while (rset.next()){ 
      54.  
      55.             System.out.println(rset.getInt("id")); 
      56.  
      57.             System.out.println(rset.getString("name")); 
      58.  
      59.         } 
      60.  
      61.         if (rset!=null){ 
      62.  
      63.             rset.close(); rset = null
      64.  
      65.         } 
      66.  
      67.         System.out.println(new java.util.Date()); 
      68.  
      69.         System.out.println("建索引:"); 
      70.  
      71.         System.out.println(new java.util.Date()); 
      72.  
      73.         stmt.executeUpdate("CREATE INDEX hehe_idx on hehe(id)"); 
      74.  
      75.         stmt.executeUpdate("CREATE INDEX hehe_idx2 on hehe(name)"); 
      76.  
      77.         conn.commit(); 
      78.  
      79.         System.out.println(new java.util.Date()); 
      80.  
      81.         System.out.println("建索引后的查詢:"); 
      82.  
      83.         System.out.println(new java.util.Date()); 
      84.  
      85.         rset = stmt.executeQuery("SELECT id, name FROM hehe where id > 5 "); 
      86.  
      87.         while (rset.next()){ 
      88.  
      89.             System.out.println(rset.getInt("id")); 
      90.  
      91.             System.out.println(rset.getString("name")); 
      92.  
      93.         } 
      94.  
      95.         System.out.println(new java.util.Date()); 
      96.  
      97.         stmt.executeUpdate("drop table hehe"); 
      98.  
      99.         System.out.println("刪除表hehe成功!"); 
      100.  
      101.         conn.commit(); 
      102.  
      103.         System.out.println(new java.util.Date()); 
      104.  
      105.         } catch(ClassNotFoundException cnfe) 
      106.  
      107.         { 
      108.  
      109.             System.out.println("Can?t find class for driver: " + cnfe.getMessage()); 
      110.  
      111.             System.exit(-1); 
      112.  
      113.         } catch (SQLException e){ 
      114.  
      115.             System.out.println("SQLException :" + e.getMessage()); 
      116.  
      117.             System.exit(-1); } 
      118.  
      119.         finally { 
      120.  
      121.             try { 
      122.  
      123.                 if (rset!=null) rset.close(); 
      124.  
      125.                 stmt.close(); 
      126.  
      127.                 conn.close(); 
      128.  
      129.             } catch (SQLException e) { System.out.println("SQLException in finally :" + e.getMessage()); 
      130.  
      131.             System.exit(-1);} } } 
      132.  
      133.  
      134. public static void main(String[] args) { 
      135.  
      136.     TestConn conn = new TestConn(); 
      137.  
      138.     conn.test(); 
      139.  
      140.     System.out.print("Success!!"); 
      141.  
      142.  

      好了,謝謝大家賞臉,睡覺時(shí)間到??!

      原文鏈接:http://www./ICkengine/archives/38027.shtml

      【編輯推薦】

      【責(zé)任編輯:chensf TEL:(010)68476606】

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多