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

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

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

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

// Import required packages
// See more detail at http://www.yiibai.com/jdbc/ import java.sql.*; public class BatchingWithStatement {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/EMP"; // Database credentials
static final String USER = "root";
static final String PASS = "123456"; public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver"); // Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS); // Create statement
System.out.println("Creating statement...");
stmt = conn.createStatement(); // Set auto-commit to false
conn.setAutoCommit(false); // First, let us select all the records and display them.
printRows( stmt ); // Create SQL statement
String SQL = "INSERT INTO Employees (id, first, last, age) " +
"VALUES(200,'Curry', 'Stephen', 30)";
// Add above SQL statement in the batch.
stmt.addBatch(SQL); // Create one more SQL statement
SQL = "INSERT INTO Employees (id, first, last, age) " +
"VALUES(201,'Kobe', 'Bryant', 35)";
// Add above SQL statement in the batch.
stmt.addBatch(SQL); // Create one more SQL statement
SQL = "UPDATE Employees SET age = 35 " +
"WHERE id = 100";
// Add above SQL statement in the batch.
stmt.addBatch(SQL); // Create an int[] to hold returned values
int[] count = stmt.executeBatch(); //Explicitly commit statements to apply changes
conn.commit(); // Again, let us select all the records and display them.
printRows( stmt ); // Clean-up environment
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main public static void printRows(Statement stmt) throws SQLException{
System.out.println("Displaying available rows...");
// Let us select all the records and display them.
String sql = "SELECT id, first, last, age FROM Employees";
ResultSet rs = stmt.executeQuery(sql); while(rs.next()){
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last"); //Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
System.out.println();
rs.close();
}//end printRows()
}//end JDBCExample
SQL

编译上面代码,如下 -

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

执行上面代码如下所示 -

F:\worksp\jdbc>java -Djava.ext.dirs=F:\worksp\jdbc\libs BatchingWithStatement
Connecting to database...
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.
Creating statement...
Displaying available rows...
ID: 100, Age: 28, First: Max, Last: Su
ID: 101, Age: 25, First: Wei, Last: Wang
ID: 102, Age: 35, First: Xueyou, Last: Zhang
ID: 103, Age: 30, First: Jack, Last: Ma
ID: 106, Age: 28, First: Curry, Last: Stephen
ID: 107, Age: 32, First: Kobe, Last: Bryant Displaying available rows...
ID: 100, Age: 35, First: Max, Last: Su
ID: 101, Age: 25, First: Wei, Last: Wang
ID: 102, Age: 35, First: Xueyou, Last: Zhang
ID: 103, Age: 30, First: Jack, Last: Ma
ID: 106, Age: 28, First: Curry, Last: Stephen
ID: 107, Age: 32, First: Kobe, Last: Bryant
ID: 200, Age: 30, First: Curry, Last: Stephen
ID: 201, Age: 35, First: Kobe, Last: Bryant Goodbye! 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. elastic search internal

    Realtime Search with Lucene http://2010.berlinbuzzwords.de/sites/2010.berlinbuzzwords.de/files/busch ...

  2. vue中创建全局单文件组件/命令

    1.在 vue中如果我们使用基于vue.js编写的插件,我们可以使用Vue.use() 如在main.js中: 2.添加全局命令,让每个vue单文件组件都可以使用到: 第一步:最好建一个全局的命令文件 ...

  3. 大数据 Hive 简介

    第一部分:Hive简介 什么是Hive •Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供类SQL查询功能. •本质是将SQL转换为MapReduce程序 ...

  4. Android Retrofit2 数据解析

    在弄数据解析这块,浪费了很长的时间,最开始一直觉得传过来用对象接收的,类型是json,往那个方式去想了.搞了很久. 后来看了别人写的才发觉,真是很简单,感谢 https://www.jianshu.c ...

  5. Php.ini 文件位置在哪里,怎么找到 php.ini

    Php.ini 文件不是经常用到的,突然有一天,你需要修改它了,却不知道他躲在哪里,怎么破? 一般情况下,它会呆在php的安装目录里. 方法/步骤   1 在你自己的网站目录里,新建一个php文件,写 ...

  6. ResponderChain note

    http://ww3.sinaimg.cn/large/6b288462gw1evl4h40tfxj20sg0lc77k.jpg

  7. 【Unity笔记】根骨骼动画/运动(Root Motion)

    根骨骼动画:当动画中角色发生位移后,动作坐标原点跟随角色移动.例如一个向前跳跃的动画,如果在场景中重复该动画,能够看到角色一路往前跳跃,位置一直在前进.适用于有位移的放技能动作等. 非根骨骼动画:当动 ...

  8. iis部署webservice问题集合

    一.添加网站 具体步骤:打开控制面板,选择管理工具,打开管理工具. 打开管理工具后,打开第二个internet信息服务(iis)管理器. 打开后的界面如下: 右击网站,添加网站后,弹出“添加网站”选项 ...

  9. C语言 · 和最大子序列

    算法提高 和最大子序列   时间限制:1.0s   内存限制:512.0MB      问题描述 对于一个给定的长度为N的整数序列A,它的“子序列”的定义是:A中非空的一段连续的元素(整数).你要完成 ...

  10. Ubuntu 安装 vnc server

    安装原因,因为需要有桌面操作, 服务器上配置 vnc 即可实现. 在 ubuntu 14.04 上已经实现. 安装先关软件 sudo apt-get update sudo apt-get insta ...