java實現sql語句批處理

來源:互聯網
上載者:User

標籤:exe   ntb   etc   div   _for   except   參數   效率   value   

Statement實現批處理:

優點:能夠處理多種不同結構的sql語句

缺點:不能預先處理,執行效率較差。對於參數不同的同一條sql語句需要多次調用addBatch()

[java] view plain copy
  1. package com.itheima.batch;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.Statement;  
  5.   
  6. import org.junit.Test;  
  7.   
  8. import com.itheima.util.DBUtil;  
  9.   
  10.   
  11. public class StatementBatch {  
  12.   
  13.     /*  mysql資料庫: 
  14.         create database batch; 
  15.         use batch; 
  16.         create table mybatch( 
  17.             id int primary key auto_increment, 
  18.             name varchar(50) 
  19.         ); 
  20.         insert into mybatch values (null, ‘1‘); 
  21.         insert into mybatch values (null, ‘2‘); 
  22.         insert into mybatch values (null, ‘3‘); 
  23.         insert into mybatch values (null, ‘4‘); 
  24.      */  
  25.     @Test  
  26.     public void statementBatch() {  
  27.         Connection conn = null;  
  28.         Statement stat = null;  
  29.         try{  
  30.             conn = DBUtil.getConn();  
  31.             stat = conn.createStatement();  
  32.             stat.addBatch("create database batch");  
  33.             stat.addBatch("use batch");  
  34.             stat.addBatch("create table mybatch( id int primary key auto_increment, name varchar(50) )");  
  35.             stat.addBatch("insert into mybatch values (null, ‘1‘)");  
  36.             stat.addBatch("insert into mybatch values (null, ‘2‘)");  
  37.             stat.addBatch("insert into mybatch values (null, ‘3‘)");  
  38.             stat.addBatch("insert into mybatch values (null, ‘4‘)");  
  39.             stat.executeBatch();  
  40.         } catch (Exception e) {  
  41.             e.printStackTrace();  
  42.         } finally {  
  43.             DBUtil.close(conn, stat, null);  
  44.         }  
  45.     }  
  46. }  

PreparedStatement實現批處理:

優點:能夠預先處理,執行效率高;參數不同的同一條sql語句執行簡便

缺點:只能批處理參數不同的同一條sql語句

[java] view plain copy
  1. package com.itheima.batch;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.PreparedStatement;  
  5.   
  6. import org.junit.Test;  
  7.   
  8. import com.itheima.util.DBUtil;  
  9.   
  10. public class PrepBatch {  
  11.   
  12.     @Test  
  13.     public void prepBatch() {  
  14.           
  15.         Connection conn = null;  
  16.         PreparedStatement ps = null;  
  17.         try{  
  18.             conn = DBUtil.getConn();  
  19.             ps = conn.prepareStatement("insert into mybatch value(null, ?)");  
  20.             for(int i = 0; i < 1000; i++) {  
  21.                 ps.setString(1, "name" + i);  
  22.                 ps.addBatch();  
  23.             }  
  24.             ps.executeBatch();  
  25.         } catch (Exception e) {  
  26.             e.printStackTrace();  
  27.         } finally {  
  28.             DBUtil.close(conn, ps, null);  
  29.         }  
  30.     }  
  31. } 

java實現sql語句批處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.