How to execute multiple SQL statements simultaneously in JSP

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.