Batch processing JDBC statements to improve processing speed _jsp programming

Source: Internet
Author: User
Tags stmt

Sometimes JDBC does not run fast enough, which allows some programmers to use database-related stored procedures. As an alternative, try using the statement batch feature to see if you can execute all the SQL at the same time to improve speed.
The simplest form of a stored procedure is the process of including a series of SQL statements that can be used together to manage in the same place and improve speed. The Statement class can contain a series of SQL statements, thus allowing all of those statements to be executed in the same database transaction instead of executing a series of calls to the database.
Using the bulk processing feature involves the following two methods:
· Addbatch (String) method
· ExecuteBatch method
If you are using statement then Addbatch method can accept a normal SQL statement, or if you are using PreparedStatement, then you can add nothing to it. The ExecuteBatch method executes those SQL statements and returns an array of int values that contains the number of rows of data affected by each statement. Placing a SELECT statement or other SQL statement that returns a resultset into the batch process results in a SqlException exception.
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", Panax Notoginseng, "Circle"));
Stmt.insert ("INSERT INTO Users VALUES" ("Jane", "Triangle") ");
Stmt.insert ("INSERT INTO Users VALUES" ("Freddy", "N", "Square") ");
Int[] counts = Stmt.executebatch ();
PreparedStatement is a bit different, it can only handle some of the SQL syntax, but there are many parameters, so rewrite the example above to get the following result:
Note There are no DELETE statements here
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.executebatch ();
If you don't know how many times your statement will run, this is a good way to handle the SQL code. Without the use of bulk processing, if you add 50 users, performance has an impact, and if someone writes a script to add 10,000 users, the program can become very bad. Adding batch functionality can help improve performance, and in the latter case the code is more readable.

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.