JDBC的批处理

(1)Statement完成批处理
(2)PreparedStatement完成批处理


一、Statement完成批处理
(1)步骤:
		//添加批处理
		stm.addBatch(sql);
		stm.addBatch(sql1);
		stm.addBatch(sql2);
		//执行批处理
		stm.executeBatch();
		//清除批处理sql
		stm.clearBatch();

①添加要批量执行的SQL语句
②执行批处理SQL语句
③清除批处理命令

  • 优点:可以向数据库发送多条不同的sql语句。
  • 缺点:sql语句没有预编译。当向数据库发送多条语句相同,但仅参数不同的SQL语句时,需重复写上很多条sql语句。
(2)使用Statement在emp表中批处理sql语句:
package jdbc;

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

/**
 * 类说明:
 * 		使用Statement对象执行批处理
 * @author qianliangguo
 */
public class TestStatementBatch {
	public static void main(String[] args){
		Connection conn = null;
		Statement stm = null;
		try {
			//1.加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			//2.获取连接
			String url = "jdbc:mysql://localhost:3306/mybase?useUnicode=true&characterEncoding=utf-8";
			String user = "root";
			String password = "Hudie";
			conn = DriverManager.getConnection(url, user, password);
			//3.创建Statment对象
			stm = conn.createStatement();
			
			String sql = "insert into emp(ename,job,deptno) value('小哪吒','clerk',20)";
			String sql1 = "insert into emp(ename,job,deptno) value('美人鱼','leader',10)";
			String sql2 = "update emp set ename ='哪吒'where ename='小哪吒'";
			//4.执行sql语句(使用批处理)
			stm.addBatch(sql);//添加批处理
			stm.addBatch(sql1);
			stm.addBatch(sql2);
			stm.executeBatch();//执行批处理
			stm.clearBatch();//清除批处理
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				//释放资源
				stm.close();
				conn.close();	
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
二、PreparedStatement完成批处理
(1):关于PreparedStatement

采用PreparedStatement.addBatch()实现批处理。

  • 优点:发送的是预编译后的SQL语句,执行效率高。
  • 缺点:只能应用在SQL语句相同,但参数不同的批处理中。此种形式的批处理经常用于在同一个表中批量插入数据,或批量更新表的数据。
(2)使用Statement在emp表中批处理sql语句:
package jdbc;

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

/**
 * 类说明:
 * 		使用TestPreparedStatementBatch对象执行批处理
 * @author qianliangguo
 */
public class TestPreparedStatementBatch {
	public static void main(String[] args){
		Connection conn = null;
		PreparedStatement pstm = null;
		try {
			//加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			//获取连接
			String url = "jdbc:mysql://localhost:3306/mybase?useUnicode=true&characterEncoding=utf-8";
			String user = "root";
			String password = "Hudie";
			conn = DriverManager.getConnection(url, user, password);
			String sql = "insert into emp(empno,ename,deptno) values(?,?,?)";
			//创建preparedStatement
			pstm = conn.prepareStatement(sql);
			//pstm绑定数据
			for(int i= 3019;i<4008;i++){
				pstm.setInt(1, i);
				pstm.setString(2,"大鱼"+i);
				pstm.setInt(3,i);
				//添加批处理
				pstm.addBatch();
				//执行批处理,当i是100的倍数时执行一次批处理,然后清除批处理
				if(i%100 == 0){
					pstm.executeBatch();
					pstm.clearBatch();
				}
			}
			//将批处理余下的语句执行完毕
			pstm.executeBatch();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//释放资源
			try {
				pstm.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

上面两种方式都是向表中添加记录,所以并不需要进行封装ResultSet结果集,执行后表格内容如下:
在这里插入图片描述

发布了328 篇原创文章 · 获赞 798 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/weixin_43691058/article/details/103261546
今日推荐