JDBC的批处理操作三种方式

2 篇文章 0 订阅

原文地址 :http://lavasoft.blog.51cto.com/62575/238651/



SQL批处理是JDBC性能优化的重要武器,经本人研究总结,批处理的用法有三种。
 
package lavasoft.jdbctest; 

import lavasoft.common.DBToolkit; 

import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.SQLException; 
import java.sql.Statement; 

/** 
* JDBC的批量操作三种方式 

* @author leizhimin 2009-12-4 14:42:11 
*/
 
public  class BatchExeSQLTest { 

         public  static  void main(String[] args) { 
                exeBatchStaticSQL(); 
        } 

         /** 
         * 批量执行预定义模式的SQL 
         */
 
         public  static  void exeBatchParparedSQL() { 
                Connection conn =  null
                 try { 
                        conn = DBToolkit.getConnection(); 
                        String sql =  "insert into testdb.book (kind, name) values (?,?)"
                        PreparedStatement pstmt = conn.prepareStatement(sql); 
                        pstmt.setString(1,  "java"); 
                        pstmt.setString(2,  "jjjj"); 
                        pstmt.addBatch();                      //添加一次预定义参数 
                        pstmt.setString(1,  "ccc"); 
                        pstmt.setString(2,  "dddd"); 
                        pstmt.addBatch();                      //再添加一次预定义参数 
                         //批量执行预定义SQL 
                        pstmt.executeBatch(); 
                }  catch (SQLException e) { 
                        e.printStackTrace(); 
                }  finally { 
                        DBToolkit.closeConnection(conn); 
                } 
        } 

         /** 
         * 批量执行混合模式的SQL、有预定义的,还有静态的 
         */
 
         public  static  void exeBatchMixedSQL() { 
                Connection conn =  null
                 try { 
                        conn = DBToolkit.getConnection(); 
                        String sql =  "insert into testdb.book (kind, name) values (?,?)"
                        PreparedStatement pstmt = conn.prepareStatement(sql); 
                        pstmt.setString(1,  "java"); 
                        pstmt.setString(2,  "jjjj"); 
                        pstmt.addBatch();     //添加一次预定义参数 
                        pstmt.setString(1,  "ccc"); 
                        pstmt.setString(2,  "dddd"); 
                        pstmt.addBatch();     //再添加一次预定义参数 
                         //添加一次静态SQL 
                        pstmt.addBatch( "update testdb.book set kind = 'JAVA' where kind='java'"); 
                         //批量执行预定义SQL 
                        pstmt.executeBatch(); 
                }  catch (SQLException e) { 
                        e.printStackTrace(); 
                }  finally { 
                        DBToolkit.closeConnection(conn); 
                } 
        } 

         /** 
         * 执行批量静态的SQL 
         */
 
         public  static  void exeBatchStaticSQL() { 
                Connection conn =  null
                 try { 
                        conn = DBToolkit.getConnection(); 
                        Statement stmt = conn.createStatement(); 
                         //连续添加多条静态SQL 
                        stmt.addBatch( "insert into testdb.book (kind, name) values ('java', 'java in aciton')"); 
                        stmt.addBatch( "insert into testdb.book (kind, name) values ('c', 'c in aciton')"); 
                        stmt.addBatch( "delete from testdb.book where kind ='C#'"); 
                        stmt.addBatch( "update testdb.book set kind = 'JAVA' where kind='java'"); 
//                        stmt.addBatch("select count(*) from testdb.book");                //批量执行不支持Select语句 
                         //执行批量执行 
                        stmt.executeBatch(); 
                }  catch (SQLException e) { 
                        e.printStackTrace(); 
                }  finally { 
                        DBToolkit.closeConnection(conn); 
                } 
        } 
}
 
注意:JDBC的批处理不能加入select语句,否则会抛异常:
java.sql.BatchUpdateException: Can not issue SELECT via executeUpdate(). 
  at com.mysql.jdbc.StatementImpl.executeBatch(StatementImpl.java:1007)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值