PreparedStatement vs Statement

來源:互聯網
上載者:User

標籤:style   color   os   io   使用   java   ar   for   資料   

二者異同:

代碼的可讀性和可維護性. 

PreparedStatement 能最大可能提高效能:
  •      DBServer會對先行編譯語句提供效能最佳化。因為先行編譯語句有可能被重複調用,所以語句在被DBServer的編譯器編譯後的執行代碼被緩衝下來,那麼下次調用時只要是相同的先行編譯語句就不需要編譯,只要將參數直接傳入編譯過的語句執行代碼中就會得到執行。
  •    在statement語句中,即使是相同操作但因為資料內容不一樣,所以整個語句本身不能匹配,沒有緩衝語句的意義.事實是沒有資料庫會對普通語句編譯後的執行代碼緩衝.這樣每執行一次都要對傳入的語句編譯一次.  
  •    (語法檢查,語義檢查,翻譯成二進位命令,緩衝)

PreparedStatement 可以防止 SQL 注入 。


代碼:

package com.atguigu.java;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.Statement;import org.junit.Test;//大量操作:主要指的是批量插入//分別使用PreparedStatement和Statement向Oracle資料庫的某個表中插入100000條資料,//對比二者的效率public class TestJDBC1 {// 使用PreparedStatement執行插入操作2,結合addBatch() executeBatch() clearBatch()//結果顯示:插入10萬條資料花費時間不到1秒 ,推薦使用此方法,@Test public void test4() {Connection conn = null;PreparedStatement ps = null;try {conn = JDBCUtils.getConnection();long start = System.currentTimeMillis();String sql = "insert into dept values(?,?)";ps = conn.prepareStatement(sql);for (int i = 0; i < 100000; i++) {ps.setInt(1, i + 1);ps.setString(2, "dept_" + (i + 1));//1."攢sql"ps.addBatch();//2.執行if((i + 1) % 250 == 0){//利用統計學可以找到一個效率最高的峰值,這裡我們選用250能被100000整除ps.executeBatch();//3.清空ps.clearBatch();}}long end = System.currentTimeMillis();System.out.println("花費的時間為:" + (end - start));// 47052-801} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {JDBCUtils.close(null, ps, conn);}}// 使用Statement執行插入操作2,結合addBatch() executeBatch() clearBatch()//結果發現效率並沒有提升@Testpublic void test3() {Connection conn = null;Statement st = null;try {conn = JDBCUtils.getConnection();long start = System.currentTimeMillis();st = conn.createStatement();for (int i = 0; i < 100000; i++) {String sql = "insert into dept values(" + (i + 1) + ",'dept_"+ (i + 1) + "')";// 1.“攢”sqlst.addBatch(sql);if ((i + 1) % 250 == 0) {//2.st.executeBatch();//3.st.clearBatch();}}long end = System.currentTimeMillis();System.out.println("花費的時間為:" + (end - start));// 98278-98957} catch (Exception e) {e.printStackTrace();} finally {JDBCUtils.close(null, st, conn);}}// 使用PreparedStatement執行插入操作1:明顯能提高效率,但是還能改進@Testpublic void test2() {Connection conn = null;PreparedStatement ps = null;try {conn = JDBCUtils.getConnection();long start = System.currentTimeMillis();String sql = "insert into dept values(?,?)";ps = conn.prepareStatement(sql);for (int i = 0; i < 100000; i++) {ps.setInt(1, i + 1);ps.setString(2, "dept_" + (i + 1));ps.execute();}long end = System.currentTimeMillis();System.out.println("花費的時間為:" + (end - start));// 47052} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {JDBCUtils.close(null, ps, conn);}}// 使用Statement執行插入操作1//發現效率很低,原因是每次插入時都要對sql語句進行文法校正,然後執行每一條SQL語句@Testpublic void test1() {Connection conn = null;Statement st = null;try {conn = JDBCUtils.getConnection();long start = System.currentTimeMillis();st = conn.createStatement();for (int i = 0; i < 100000; i++) {String sql = "insert into dept values(" + (i + 1) + ",'dept_"+ (i + 1) + "')";st.execute(sql);}long end = System.currentTimeMillis();System.out.println("花費的時間為:" + (end - start));// 98278} catch (Exception e) {e.printStackTrace();} finally {JDBCUtils.close(null, st, conn);}}@Testpublic void test0() throws Exception {Connection conn = JDBCUtils.getConnection();System.out.println(conn);}}


PreparedStatement vs Statement

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.