Copy codeThe Code is as follows:
Con. setAutoCommit (true); // if it is set to true, 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, which inserts two values into two tables respectively. The above program can meet this requirement, but it is not recommended to write this statement in actual operations. The reason is as follows:
1. The program is executed in sequence. If the first statement is imported into the database, an unpredictable error occurs next to the statement, and the Import fails,
In this case, it is not allowed. If one of the errors fails, it should not be executed.
2. It is normal to insert two data records in a row and submit the data twice in a row. However, if we expand this problem, We can insert 1000 data records in a row.
Data entries are automatically committed each time, which is a waste of server performance.
Therefore, if we need to execute multiple SQL statements at the same time, we should change the program:
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 any of the two statements has a problem, neither of them will be executed, but catch can also be used to obtain error prompts.
2. If we need to submit 1000 records at the same time, we can perform a commit () for each 100 records. In this way, we only need to execute 10 commits to improve the running speed significantly.