天道不一定酬所有勤
但是,天道只酬勤

【转】 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);  
                }  
        }  
}  
(全文完)
欢迎关注HollisChuang微信公众账号
打赏

如未加特殊说明,此网站文章均为原创,转载必须注明出处。HollisChuang's Blog » 【转】 jdbc的批量操作

分享到:更多 ()

HollisChuang's Blog

联系我关于我