JDBC Batch processing ExecuteBatch

Source: Internet
Author: User

JDBC runs the SQL declaration. There are two processing interfaces. A preparedstatement,statement, general program JDBC How many still compare PreparedStatement

Just run the batch, PreparedStatement less statement

PS = conn.preparestatement (SQL);

for (int i = 0;i<10;i++) {

Ps.setstring (1, "1");

PreparedStatement Batch processing mode one

Ps.addbatch ();

}

PreparedStatement Batch processing mode two

Ps.addbatch ("Static SQL");

Ps.executebatch ();

This is a normal operation.

But putting the preparedstatement in it doesn't work. Below:

for (int i = 0;i<10;i++) {

PS = conn.preparestatement (SQL);

Ps.setstring (1, "1");

Ps.addbatch ();

}

Ps.executebatch ();

statement is suitable for loop assignment to SQL, below the code:

Statement st = Conn.createstatement ();

for (int i = 0;i<10;i++) {

St.addbatch ("Static SQL .....");

}

St.executebatch ();

This is the full statement of the normal operation.

Summary: The cause of this is

Statement st = Conn.createstatement (); It's not possible to put SQL statements here.

PS = conn.preparestatement (SQL); This must be the initial SQL statement

The JDBC batch cannot increment the SELECT statement, otherwise it throws an exception:

Java.sql.BatchUpdateException:Can not issue SELECT via Executeupdate ().
At Com.mysql.jdbc.StatementImpl.executeBatch (statementimpl.java:1007)

JDBC Batch processing ExecuteBatch

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.