Batch test

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


public class e_Batch {

	/**
	 * 批处理:
	 * 注意:
	 * 1.对于大量的批处理,建议使用Statement,因为PrepareStatement的预编译空间有限,
	 *     当数据量特别大时,会发生异常。
	 * 2.conn.setAutoCommit(false); 设为手动提交事务,默认为自动提交(在connection关闭的时候)
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3307/TestJdbc","root","123456");
			conn.setAutoCommit(false); //设为手动提交
			stmt=conn.createStatement();
			for(int i=0;i<20000;i++){
				stmt.addBatch("insert into t_user (username,pwd,regTime) values ('gao"+i+"',6666,now())");
			}
			stmt.executeBatch();
			conn.commit();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

 

Guess you like

Origin blog.csdn.net/qq_41877184/article/details/93398968
Recommended