二者異同:
代碼的可讀性和可維護性.
PreparedStatement 能最大可能提高性能: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 block e.printStackTrace(); } finally { JDBCUtils.close(null, ps, conn); } } // 使用Statement執行插入操作2,結合addBatch() executeBatch() clearBatch() //結果發現效率並沒有提升 @Test public 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.“攢”sql st.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:明顯能提高效率,但是還能改進 @Test public 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 block e.printStackTrace(); } finally { JDBCUtils.close(null, ps, conn); } } // 使用Statement執行插入操作1 //發現效率很低,原因是每次插入時都要對sql語句進行語法校驗,然後執行每一條SQL語句 @Test public 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); } } @Test public void test0() throws Exception { Connection conn = JDBCUtils.getConnection(); System.out.println(conn); } }