How to execute multiple SQL statements simultaneously in JSP

Source: Internet
Author: User

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.

Related Article

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.