CopyCode The Code is as follows: con. setautocommit (true); // set it to true. Each executeupdate is executed immediately.
SQL = "insert into Table1 (lable1) values ('001 ')";
Rs = stmt.exe cuteupdate (SQL );
SQL = "insert into Table2 (lable2) values ('002 ')";
Rs = stmt.exe cuteupdate (SQL );
This is a basic SQL insert statement that inserts two values into two tables respectively, the above Program has been able to meet this requirement, but it is not recommended to write this statement in actual operations, for the following reasons:
1. The program is executed in sequence. If the first statement is written into the database and an unpredictable error occurs next to the statement, the Import fails.
this situation is not allowed, if one of the errors occurs, it should not be executed.
2. It is normal to insert two data records consecutively. But if we expand this problem, We can insert 1000 data records consecutively.
, every time it is automatically committed, this is a waste of server performance.
therefore, if we need to execute multiple SQL statements at the same time, we should change the program to:
con. setautocommit (false); // if it is set to false, each executeupdate will not be submitted immediately, but will wait for commit ();
SQL = "insert into Table1 (lable1) values ('001') ";
rs = stmt.exe cuteupdate (SQL);
SQL =" insert into Table2 (lable2) values ('002 ')";
rs = stmt.exe cuteupdate (SQL);
con. commit ();
first, set setauocommit () to false, not automatically executed. Then, the database is normally written into the database. When all the statements that need to be written into the database are pre-executed, and then commit () is in progress. What is the difference between this and the code above?
1. If either of the two statements has a problem, neither of the two statements will be executed, however, you can also use catch to get the error prompt
2. If you need to submit 1000 records at the same time, we can perform a Commit () every 100 records (); in this way, you only need to execute 10 times to submit the operation speed, which will be significantly improved.