Batch Processing of JDBC statements improves processing speed

Source: Internet
Author: User


Sometimes JDBC is not running fast enough, which allows some programmers to use database-related stored procedures. As an alternative, you can try to use the batch processing feature of Statement to see if all SQL statements can be executed at the same time to increase the speed.
The simplest form of a stored procedure is to include a series of SQL statements. Putting these statements together facilitates management in the same place can also increase the speed. The Statement class can contain a series of SQL statements. Therefore, you can execute all the statements in the same database transaction instead of a series of calls to the database.
The batch processing function involves the following two methods:
· AddBatch (String) Method
· ExecuteBatch Method
If you are using Statement, the addBatch method can accept a common SQL Statement, or if you are using PreparedStatement, you can not add anything to it. The executeBatch method executes the SQL statements and returns an array of int values. This array contains the number of rows of data affected by each statement. If a SELECT statement or another SQL statement that returns a ResultSet is put into batch processing, a SQLException exception occurs.
A simple example of java. SQL. Statement can be:
Statement stmt = conn. createStatement ();
Stmt. insert ("delete from Users ");
Stmt. insert ("insert into Users VALUES (" rod ", 37," circle ")");
Stmt. insert ("insert into Users VALUES (" jane ", 33," triangle ")");
Stmt. insert ("insert into Users VALUES (" freddy ", 29," square ")");
Int [] counts = stmt.exe cuteBatch ();
PreparedStatement is somewhat different. It can only process part of the SQL syntax, but it can have many parameters. Therefore, you can rewrite part of the preceding example to get the following result:
// Note that there is no DELETE statement
PreparedStatement stmt = conn. prepareStatement (
"Insert into Users VALUES (?,?,?) "
);



User [] users = ...;
For (int I = 0; I <users. length; I ++ ){
Stmt. setInt (1, users [I]. getName ());
Stmt. setInt (2, users [I]. getAge ());
Stmt. setInt (3, users [I]. getShape ());
Stmt. addBatch ();
}
Int [] counts = stmt.exe cuteBatch ();
If you do not know how many times your statement will run, this is a good way to process the SQL code. Without batch processing, if 50 users are added, the performance will be affected. If a person writes a script to add 10 thousand users, the program may become bad. The batch processing function can improve the performance and make the code more readable in the subsequent situations.

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.