Statement and PreparedStatement batch update

Statement and PreparedStatement batch update

 

PreparedStatement:

1) addBatch() adds a set of parameters to the PreparedStatement object.

2) executeBatch() submits a batch of parameters to the database for execution. If all commands are executed successfully, it returns an array of update counts.

 

 

Statement:

1) The addBatch(String sql) method will add a sql statement to the batch cache.

2) executeBatch() executes all sql statements in the batch cache.

 

Note: When batch update is used in PreparedStatement, you must set the parameters first and then use the addBatch() method to add to the cache. Only change, delete, or insert statements can be used in bulk updates.

 

Statement batch processing and transaction codes are as follows:
package com.ambow.day20.jdbc.JDBCTestCommitAndRollback;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import com.ambow.day19.jdbc.util.JDBCConAndClo;
/*
 *1, first set Auto commit to false, don't let it commit automatically
 *2, make a manual submission (commit)
 *3, after the submission is completed, reply to the scene to restore Auto commit to true,
 *4, when an exception occurs and executes the SQLException in the catch, remember to rollback (rollback);
 * */
public class StatementCommitAndRollbackTest {
	public static void main(String args[]) {
		Connection con = null;
		Statement stm = null;
		try {
			con = JDBCConAndClo.getConnectionBao();
			stm = con.createStatement();
			con.setAutoCommit(false);
			// If there is no exception, continue to execute until the try statement is completed, otherwise jump to the catch statement
			stm.addBatch("insert into student values(23,'tangbao','高数',100)");
			stm.addBatch("insert into student values(24,'王定','c#',98)");
			stm.addBatch("insert into student values(25,'Kingdom Cloud','java',90)");
			stm.addBatch("insert into student values(26,'slip out','English',89)");
			stm.addBatch("insert into student values(27,'wqde','java',63)");
			/*
			 * int[] executeBatch() throws
			 * SQLException submits a batch of commands to the database for execution, and returns an array of update counts if all commands are executed successfully.
			 */
			stm.executeBatch();
			System.out.println("Insert successfully!");
			// commit: If all the insertion operations are successfully executed, it will end normally
			con.commit ();
			System.out.println("Submitted successfully!");
			con.setAutoCommit(true);

		} catch (SQLException e) {
			e.printStackTrace ();
			try {
	//rollback: If an exception occurs, all completed operations in the database will be undone, and the transaction will be rolled back to the start state
				if (!con.isClosed()) {
					con.rollback();
					System.out.println("Submission failed, rollback!");
					con.setAutoCommit(true);
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			} finally {
				JDBCConAndClo.closeStatement(stm);
				JDBCConAndClo.closeConnection(con);
			}
		}
	}
}
PreparedStatement batch processing and transaction codes are as follows:
package com.ambow.day20.jdbc.JDBCTestCommitAndRollback;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.ambow.day19.jdbc.util.JDBCConAndClo;

/*
 * PreparedStatement:
 1.addBatch() adds a set of parameters to the PreparedStatement object
 2.executeBatch() submits a batch of parameters to the database for execution. If all commands are executed successfully, it returns an array of update counts.
 *
 */
public class PreparedStatementCommitAndRollbackTest {
	public static void main(String args[]) {
		Connection con = null;
		PreparedStatement pstm = null;

		try {
			// 1. Establish a connection to the database
			con = JDBCConAndClo.getConnectionBao();
			// 2. Execute the sql statement
			// 1). First create a PreparedStatement statement (send slq request):
			pstm = con.prepareStatement("insert into student values(?,?,?,?)");
			con.setAutoCommit(false);//1, first set Auto commit to false, do not let it commit automatically
			// 2) Set sql statement 1
			pstm.setInt(1, 33);
			pstm.setString(2,"wangqin");
			pstm.setString(3, "c++");
			pstm.setDouble(4, 78.5);
			// 3) Add a set of parameters to the batch command for this PreparedStatement object.
			pstm.addBatch();
			// 2) Set sql statement 2
			pstm.setInt(1, 34);
			pstm.setString(2,"wuytun");
			pstm.setString(3, "c");
			pstm.setDouble(4, 77);
			// 3) Add a set of parameters to the batch command for this PreparedStatement object.
			pstm.addBatch();
			// 2) Set sql statement 3
			pstm.setInt(1, 31);
			pstm.setString(2,"tetet");
			pstm.setString(3, "c++");
			pstm.setDouble(4, 90);
			// 3) Add a set of parameters to the batch command for this PreparedStatement object.
			pstm.addBatch();
			// 2) Set sql statement 4
			pstm.setInt(1, 32);
			pstm.setString (2, "liug");
			pstm.setString(3, "c");
			pstm.setDouble(4, 50);
			// 3) Add a set of parameters to the batch command for this PreparedStatement object.
			pstm.addBatch();
			// 4) Submit a batch of parameters to the database for execution, if all commands are executed successfully, return an array of update counts.
			pstm.executeBatch();
			System.out.println("Insert successfully!");
			// If all insert operations are successfully executed, end normally
			con.commit();//2, make a manual submission (commit)
			System.out.println("Submitted successfully!");
			con.setAutoCommit(true);//3, after the submission is completed, reply to the scene to restore Auto commit to true,

		} catch (SQLException e) {
			e.printStackTrace ();
			try {
				// If an exception occurs, undo all completed operations in the database, and roll back to the transaction start state
				if(!con.isClosed()){
					con.rollback();//4, when an exception occurs and executes the SQLException in the catch, remember to rollback (rollback);
					System.out.println("Insert failed, rollback!");
					con.setAutoCommit(true);
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}finally{
			JDBCConAndClo.closePreparedStatement(pstm);
			JDBCConAndClo.closeConnection(con);
		}
	}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326522770&siteId=291194637
Recommended