Transaction and batch batching in JDBC

JDBC transaction processing:

Transaction processing generally sets the transaction commit to false before the transaction starts
, and submits the transaction
demo after all DML statements are executed :

package com.xzlf.jdbc;

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

/**
 * 测试事务的基本用法
 * @author xzlf
 *
 */
public class Demo06 {

	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement ps1 = null;
		PreparedStatement ps2 = null;
		try {
			// 加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			// 建立连接
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
			// JDBC中默认是true,自动提交事务
			conn.setAutoCommit(false);
			ps1 = conn.prepareStatement("insert into t_user (username, pwd, regTime) values(?, ?, ?)");
			ps1.setObject(1, "张三");
			ps1.setObject(2, "123456");
			ps1.setObject(3, new Date(System.currentTimeMillis()));
			ps1.execute();
			System.out.println("插入一个用户,张三");
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			ps2 = conn.prepareStatement("insert into t_user (username, pwd, regTime) values(?, ?, ?)");
			ps2.setObject(1, "李四");
			ps2.setObject(2, "123456");
			ps2.setObject(3, new Date(System.currentTimeMillis()));
			ps2.execute();
			System.out.println("插入一个用户,李四");
			conn.commit();
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(ps1 != null) {
					ps1.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(ps2 != null) {
					ps2.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

JDBC batch

When JDBC performs batchc operations:
1. Set the transaction auto-commit to false
2. Generally use the Statement interface. The PreparedStatement may be pre-compiled and may be
demo:

package com.xzlf.jdbc;

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

/**
 * batch批处理基本用法
 * @author xzlf
 *
 */
public class Demo05 {

	public static void main(String[] args) {
		Connection conn = null;
		Statement stat = null;
		ResultSet rs = null;
		try {
			// 1、加载驱动类
			Class.forName("com.mysql.jdbc.Driver");
			///2、建立连接
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc", "root", "123456");
			conn.setAutoCommit(false);
			
			stat = conn.createStatement();
			long start = System.currentTimeMillis();
			for(int i = 0; i < 20000; i++) {
				stat.addBatch("insert into t_user(username, pwd, regTime) values('zs" + i +  "', 123456, now())");
			}
			stat.executeBatch();
			conn.commit();// 提交事务
			long end = System.currentTimeMillis();
			System.out.println("插入20000条数据,耗时:" + (end - start) + " ms");
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(rs != null) {
					rs.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(stat != null) {
					stat.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

Guess you like

Origin www.cnblogs.com/xzlf/p/12735548.html