以下是使用Statement对象的批处理的典型步骤序列 -

  • 使用createStatement()方法创建Statement对象。
  • 使用setAutoCommit()将自动提交设置为false
  • 使用addBatch()方法在创建的Statement对象上添加SQL语句到批处理中。
  • 在创建的Statement对象上使用executeBatch()方法执行所有SQL语句。
  • 最后,使用commit()方法提交所有更改。

此示例代码是基于前面章节中完成的环境和数据库设置编写的。

以下代码片段提供了使用Statement对象的批量更新示例,将下面代码保存到文件:BatchingWithStatement.java -

  1. // Import required packages
  2. // See more detail at http://www.yiibai.com/jdbc/
  3. import java.sql.*;
  4. public class BatchingWithStatement {
  5. // JDBC driver name and database URL
  6. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  7. static final String DB_URL = "jdbc:mysql://localhost/EMP";
  8. // Database credentials
  9. static final String USER = "root";
  10. static final String PASS = "123456";
  11. public static void main(String[] args) {
  12. Connection conn = null;
  13. Statement stmt = null;
  14. try{
  15. // Register JDBC driver
  16. Class.forName("com.mysql.jdbc.Driver");
  17. // Open a connection
  18. System.out.println("Connecting to database...");
  19. conn = DriverManager.getConnection(DB_URL,USER,PASS);
  20. // Create statement
  21. System.out.println("Creating statement...");
  22. stmt = conn.createStatement();
  23. // Set auto-commit to false
  24. conn.setAutoCommit(false);
  25. // First, let us select all the records and display them.
  26. printRows( stmt );
  27. // Create SQL statement
  28. String SQL = "INSERT INTO Employees (id, first, last, age) " +
  29. "VALUES(200,'Curry', 'Stephen', 30)";
  30. // Add above SQL statement in the batch.
  31. stmt.addBatch(SQL);
  32. // Create one more SQL statement
  33. SQL = "INSERT INTO Employees (id, first, last, age) " +
  34. "VALUES(201,'Kobe', 'Bryant', 35)";
  35. // Add above SQL statement in the batch.
  36. stmt.addBatch(SQL);
  37. // Create one more SQL statement
  38. SQL = "UPDATE Employees SET age = 35 " +
  39. "WHERE id = 100";
  40. // Add above SQL statement in the batch.
  41. stmt.addBatch(SQL);
  42. // Create an int[] to hold returned values
  43. int[] count = stmt.executeBatch();
  44. //Explicitly commit statements to apply changes
  45. conn.commit();
  46. // Again, let us select all the records and display them.
  47. printRows( stmt );
  48. // Clean-up environment
  49. stmt.close();
  50. conn.close();
  51. }catch(SQLException se){
  52. //Handle errors for JDBC
  53. se.printStackTrace();
  54. }catch(Exception e){
  55. //Handle errors for Class.forName
  56. e.printStackTrace();
  57. }finally{
  58. //finally block used to close resources
  59. try{
  60. if(stmt!=null)
  61. stmt.close();
  62. }catch(SQLException se2){
  63. }// nothing we can do
  64. try{
  65. if(conn!=null)
  66. conn.close();
  67. }catch(SQLException se){
  68. se.printStackTrace();
  69. }//end finally try
  70. }//end try
  71. System.out.println("Goodbye!");
  72. }//end main
  73. public static void printRows(Statement stmt) throws SQLException{
  74. System.out.println("Displaying available rows...");
  75. // Let us select all the records and display them.
  76. String sql = "SELECT id, first, last, age FROM Employees";
  77. ResultSet rs = stmt.executeQuery(sql);
  78. while(rs.next()){
  79. //Retrieve by column name
  80. int id = rs.getInt("id");
  81. int age = rs.getInt("age");
  82. String first = rs.getString("first");
  83. String last = rs.getString("last");
  84. //Display values
  85. System.out.print("ID: " + id);
  86. System.out.print(", Age: " + age);
  87. System.out.print(", First: " + first);
  88. System.out.println(", Last: " + last);
  89. }
  90. System.out.println();
  91. rs.close();
  92. }//end printRows()
  93. }//end JDBCExample
SQL

编译上面代码,如下 -

  1. F:\worksp\jdbc>javac -Djava.ext.dirs=F:\worksp\jdbc\libs BatchingWithStatement.java
Shell

执行上面代码如下所示 -

  1. F:\worksp\jdbc>java -Djava.ext.dirs=F:\worksp\jdbc\libs BatchingWithStatement
  2. Connecting to database...
  3. Thu Jun 01 04:41:05 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
  4. Creating statement...
  5. Displaying available rows...
  6. ID: 100, Age: 28, First: Max, Last: Su
  7. ID: 101, Age: 25, First: Wei, Last: Wang
  8. ID: 102, Age: 35, First: Xueyou, Last: Zhang
  9. ID: 103, Age: 30, First: Jack, Last: Ma
  10. ID: 106, Age: 28, First: Curry, Last: Stephen
  11. ID: 107, Age: 32, First: Kobe, Last: Bryant
  12. Displaying available rows...
  13. ID: 100, Age: 35, First: Max, Last: Su
  14. ID: 101, Age: 25, First: Wei, Last: Wang
  15. ID: 102, Age: 35, First: Xueyou, Last: Zhang
  16. ID: 103, Age: 30, First: Jack, Last: Ma
  17. ID: 106, Age: 28, First: Curry, Last: Stephen
  18. ID: 107, Age: 32, First: Kobe, Last: Bryant
  19. ID: 200, Age: 30, First: Curry, Last: Stephen
  20. ID: 201, Age: 35, First: Kobe, Last: Bryant
  21. Goodbye!
  22. F:\worksp\jdbc>
Shell
 

JDBC Statement对象执行批量处理实例的更多相关文章

  1. JDBC PrepareStatement对象执行批量处理实例

    以下是使用PrepareStatement对象进行批处理的典型步骤顺序 - 使用占位符创建SQL语句. 使用prepareStatement()方法创建PrepareStatement对象. 使用se ...

  2. 使用Statement对象执行静态sql语句

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java ...

  3. statement 对象执行sql语句

    statement对象执行sql语句    关于Statement.它是Java执行数据库操作的一个重要步骤,可以执行一些简单的SQL语句,从而完成对数据库的操作.它有两个子接口,分别是Prepare ...

  4. jdbc中的Statement对象和Preparedstatement对象的区别,以及通过jdbc操作调用存储过程

    一. java.sql.*   和  javax.sql.*的包的类结构 |- Driver接口: 表示java驱动程序接口.所有的具体的数据库厂商要来实现此接口. |- connect(url, p ...

  5. JDBC的Statement对象

    以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/statements.html: 一旦获得了数据库的连接,就可以和数据库进行交互.JDBC的Statem ...

  6. Statement对象

    Statement 对象 创建 Statement 对象 在你准备使用 Statement 对象执行 SQL 语句之前,你需要使用 Connection 对象的 createStatement() 方 ...

  7. java中Statement 对象

    1.创建Statement对象建立了到特定数据库的连接之后,就可用该连接发送 SQL 语句.Statement 对象用 Connection 的方法 createStatement 创建,如下列代码段 ...

  8. JDBC课程2--实现Statement(用于执行SQL语句)--使用自定义的JDBCTools的工具类静态方法,包括insert/update/delete三合一

    /**JDBC课程2--实现Statement(用于执行SQL语句) * 1.Statement :用于执行SQL语句的对象: * 1): 通过Connection 的createStatement( ...

  9. Spring JDBC Framework详解——批量JDBC操作、ORM映射

    转自:https://blog.csdn.net/yuyulover/article/details/5826948 一.spring JDBC 概述 Spring 提供了一个强有力的模板类JdbcT ...

随机推荐

  1. Debugging the Java HotSpot VM

    Open Heart Surgery: Analyzing and Debugging the Java HotSpot VM at the OS Level https://www.youtube. ...

  2. C++11 类型推导auto

    在C++11之前,auto关键字用来指定存储期.在新标准中,它的功能变为类型推断.auto现在成了一个类型的占位符,通知编译器去根据初始化代码推断所声明变量的真实类型.使用auto会拖慢c++效率吗? ...

  3. Python args **kwargs作用

    python当函数的参数不确定时,可以使用*args和**kwargs,*args用于捕获所有no keyword参数,它是一个tuple.**kwargs捕获所有keyword参数,它是一个dict ...

  4. NGUI的UISprite动态染色的一种方法

    本文主要参考iwinterice 的 NGUI的UISprite动态染色的一种解决方案 文章. 参考参考,就是既参详又拷贝,-,-||| 相关理论依据,还请去移步 NGUI的UISprite动态染色的 ...

  5. SAP NetWeaver BW 7.3介绍

    (摘自SAP 官方 EIM300 SAP NetWeaver BW 7.3 特色功能.前景展望与路线图)

  6. java 多线程 23 : Timer

    前言 定时/计划功能在Java应用的各个领域都使用得非常多,比方说Web层面,可能一个项目要定时采集话单.定时更新某些缓存.定时清理一批不活跃用户等等.定时计划任务功能在Java中主要使用的就是Tim ...

  7. 虚拟机三种网络模式详解(Bridge,Nat,Host-only)

    虚拟机网络模式 无论是vmware,virtual box,virtual pc等虚拟机软件,一般来说,虚拟机有三种网络模式: 1.桥接 2.NAT 3.Host-Only 初学者看到虚拟机有三种网络 ...

  8. JAVA-JSP内置对象之request获得参数的参数值(一个值)

    相关资料:<21天学通Java Web开发> 获得参数的参数值(一个值) RequestForm3.jsp <%@ page language="java" co ...

  9. 基于jQuery Tooltips悬停提示效果

    基于jQuery Tooltips悬停提示效果.这是一款基于jquery.tooltipster插件实现的jQuery Tooltips Hover effect特效.效果图如下: 在线预览   源码 ...

  10. 如何在线程中获取spring 管理的bean

    转载自:https://my.oschina.net/skyline520/blog/181158?fromerr=GjtR6Wec spring xml中定义 <!--spring 工具类-- ...