当前位置:首页 > 开发 > 数据库 > 正文

JDBC的批处理操作三种方式

发表于: 2015-02-10   作者:_wy_   来源:转载   浏览:
摘要: 1.JDBC批处理应用场合:(待解决)     2.JDBC批处理操作方式: SQL批处理是JDBC性能优化的重要武器,经本人研究总结,批处理的用法有三种。   package lavasoft.jdbctest; import lavasoft.common.DBToolkit; import java.sql.Connect

1.JDBC批处理应用场合:(待解决)

 

 

2.JDBC批处理操作方式:
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)
 

本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/238651

JDBC的批处理操作三种方式

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

推荐文章
编辑推荐
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号