Beginner JDBC, get the primary key for inserting records, perform bulk operations

Source: Internet
Author: User

First, get Insert record primary key value

Use Statement.return_generated_keys to identify where the statement was created, and then get the Getgeneratedkeys method

Preparedstatement=conn.preparestatement (Sql,statement.return_generated_keys);

Preparedstatement.excuteupdate ();

Resultset=preparedstatement.getgeneratedkeys ();//primary key may have more than one

if (Resultset.next ()) {

return Resultset.getint (1);

}

Second, the implementation of bulk operations

String sql= "INSERT into User (Name,birthday,money) VALUES (?,?,?)";

Preparedstatement=conn.preparestatement (SQL);

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

Preparedstatement.setstring (1, "Batchname" +i);

Preparedstatement.setdate (2,new Date (System.currenttimemillis ()));

Preparedstatement.setfloat (3,100f+i);

Preparedstatement.addbatch ();//(Pack) Join in the batch operation identification, the late unification executes; note not the data in a package, the more the better

}

Int[] Intarray=preparedstatement.excutebatch ();

Third, resultset result set scrolling

Statement statement=conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable);

Resultset.next () Next; result.previous () prev; Result.absolute (5) position to fifth

Beginner JDBC, get the primary key for inserting records, perform bulk operations

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.