日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Statement、PreparedStatement、PreparedStatement + 批處理 的區(qū)別

 燮羽 2010-12-19

1.使用Statement對(duì)象

2.預(yù)編譯PreparedStatement

3.使用PreparedStatement + 批處理

為了區(qū)分出這三者之間的效率,下面的事例執(zhí)行過(guò)程都是在數(shù)據(jù)庫(kù)表t1中插入1萬(wàn)條記錄,并記錄出所需的時(shí)間(此時(shí)間與電腦硬件有關(guān))

1.使用Statement對(duì)象

使用范圍:當(dāng)執(zhí)行相似SQL(結(jié)構(gòu)相同,具體值不同)語(yǔ)句的次數(shù)比較少

優(yōu)點(diǎn):語(yǔ)法簡(jiǎn)單

缺點(diǎn):采用硬編碼效率低,安全性較差。

原理:硬編碼,每次執(zhí)行時(shí)相似SQL都會(huì)進(jìn)行編譯   

        

事例執(zhí)行過(guò)程:

   public void exec(Connection conn){

         try {

                     Long beginTime = System.currentTimeMillis();

                           conn.setAutoCommit(false);//設(shè)置手動(dòng)提交

                    Statement st = conn.createStatement();

                            for(int i=0;i<10000;i++){

                       String sql="insert into t1(id) values ("+i+")";

                       st.executeUpdate(sql);

                    }

                           Long endTime = System.currentTimeMillis();

                   System.out.println("Statement用時(shí)"+(endTime-beginTime)/1000+"");//計(jì)算時(shí)間

                   st.close();

                  conn.close();

                } catch (SQLException e) {             

                  e.printStackTrace();

         }

   }

執(zhí)行時(shí)間:Statement用時(shí):31

2.預(yù)編譯PreparedStatement

使用范圍:當(dāng)執(zhí)行相似sql語(yǔ)句的次數(shù)比較多(例如用戶登陸,對(duì)表頻繁操作..)語(yǔ)句一樣,只是具體的值不一樣,被稱(chēng)為動(dòng)態(tài)SQL

優(yōu)點(diǎn):語(yǔ)句只編譯一次,減少編譯次數(shù)。提高了安全性(阻止了SQL注入)

缺點(diǎn): 執(zhí)行非相似SQL語(yǔ)句時(shí),速度較慢。

原理:相似SQL只編譯一次,減少編譯次數(shù)

名詞解釋?zhuān)?/span>

SQL注入:select * from user where username="張三" and password="123" or 1=1;

前面這條語(yǔ)句紅色部分就是利用sql注入,使得這條詞句使終都會(huì)返回一條記錄,從而降低了安全性。

事例執(zhí)行過(guò)程:

      public void exec2(Connection conn){

         try {

                   Long beginTime = System.currentTimeMillis();

                   conn.setAutoCommit(false);//手動(dòng)提交

                   PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");

                   for(int i=0;i<10000;i++){

                       pst.setInt(1, i);

                       pst.execute();   

                   }

                      conn.commit();

                      Long endTime = System.currentTimeMillis();

                      System.out.println("Pst用時(shí):"+(endTime-beginTime)+"");//計(jì)算時(shí)間

                      pst.close();

                      conn.close();

         } catch (SQLException e) {               

                     e.printStackTrace();

         }

    }

執(zhí)行時(shí)間Pst用時(shí):14

3.使用PreparedStatement + 批處理

使用范圍:一次需要更新數(shù)據(jù)庫(kù)表多條記錄

優(yōu)點(diǎn):減少和SQL引擎交互的次數(shù),再次提高效率,相似語(yǔ)句只編譯一次,減少編譯次數(shù)。提高了安全性(阻止了SQL注入)

缺點(diǎn):

原理:

批處理: 減少和SQL引擎交互的次數(shù),一次傳遞給SQL引擎多條SQL。

名詞解釋?zhuān)?/span>

PL/SQL引擎:在oracle中執(zhí)行pl/sql代碼的引擎,在執(zhí)行中發(fā)現(xiàn)標(biāo)準(zhǔn)的sql會(huì)交給sql引擎進(jìn)行處理。

SQL引擎:執(zhí)行標(biāo)準(zhǔn)sql的引擎。

事例執(zhí)行過(guò)程:

public void exec3(Connection conn){

                   try {

                      conn.setAutoCommit(false);

                      Long beginTime = System.currentTimeMillis();

                   PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");

                     

                   for(int i=1;i<=10000;i++){   

                       pst.setInt(1, i);

                      pst.addBatch();//加入批處理,進(jìn)行打包

                       if(i%1000==0){//可以設(shè)置不同的大小50,100,500,1000等等

                        pst.executeBatch();

                        conn.commit();

                       pst.clearBatch();

                       }

                      }

                     pst.executeBatch();

                      Long endTime = System.currentTimeMillis();

                      System.out.println("pst+batch用時(shí)"+(endTime-beginTime)+"毫秒");

                      pst.close();

                      conn.close();

                   } catch (SQLException e) {

                      // TODO Auto-generated catch block

                      e.printStackTrace();

                   }

}

執(zhí)行時(shí)間pst+batch用時(shí)485毫秒

摘自:http://www./topic/368990

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(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)論公約

    類(lèi)似文章 更多