About the Preparedstatement.addbatch () method

Source: Internet
Author: User
Tags stmt

Statement and PreparedStatement The difference is not much nonsense, directly said PreparedStatement the most important addbatch () structure of the use.

1. Establish the link, (telephone dialing)

Connection Connection =getconnection ();

2. Do not automatically Commit (melon seeds are not one to eat, all stripped open on the table, and then licked)

Connection.setautocommit (FALSE);

3. Precompiled SQL statements, only compile one time oh, high efficiency ah. (Invent a way to peel the melon seeds, don't always want to peel the melon seeds in the future.)
PreparedStatement statement = connection.preparestatement ("INSERT into TableX VALUES (?,?)");

4. One to peel and then put on the table.

Record 1
Statement.setint (1, 1);
Statement.setstring (2, "Cujo");
Statement.addbatch ();

Record 2
Statement.setint (1, 2);
Statement.setstring (2, "Fred");
Statement.addbatch ();

Record 3
Statement.setint (1, 3);
Statement.setstring (2, "Mark");
Statement.addbatch ();

Execute the above 3 statements in bulk. Swallow it, it's cool.
int [] counts = Statement.executebatch ();

Commit it to swallow, into the belly (DB) inside
Connection.commit ();


Stmt.addbatch ("update TABLE1 set topic =" Midsummer Speech foot Health 1 "where id=" 3407 ");
Stmt.addbatch ("update TABLE1 set topic =" Summer heat stroke prevention meal 1 "where id=" 3408 ");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 11 "," 12 "," 13 "," "", ")");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 12 "," 12 "," 13 "," "", ")");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 13 "," 12 "," 13 "," "", ")");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 14 "," 12 "," 13 "," "", ")");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 15 "," 12 "," 13 "," "", ")");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 16 "," 12 "," 13 "," "", ")");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 17 "," 12 "," 13 "," "", ")");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 18 "," 12 "," 13 "," "", ")");

int [] Updatecounts=stmt.executebatch ();
Cn.commit ();

For example:

    public static void Exectebatch (Connection conn) throws exception{
     & nbsp;  String sql1 = "Delete from student where id = 3";
        String sql2 = "Delete from student where id = 5";
        String sql3 = "Delete from student where id = 6";
        String sql4 = "Delete from student where id = 7";
        PreparedStatement pstmt = conn.preparestatement (SQL1);
        pstmt.addbatch ();
        Pstmt.addbatch (SQL2);
        Pstmt.addbatch (SQL3);
        Pstmt.addbatch (SQL4);
        pstmt.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.