Introduction to Java Basic Series 13:JDBC batch processing

Source: Internet
Author: User

With regard to the batch processing of JDBC, this is the concept that emerged after JDBC2.0. batch processing is a one-time execution of multiple SQL commands, such as: INSERT, delete, etc.. If you want to implement a batch operation, you need to use the PreparedStatementAddbatch ()method adds an SQL statement to the batch and then uses theExecuteBatch () method to perform all the previously added commands. The sample code is as follows:

package javase.jdbc;import java.sql.connection;import java.sql.preparedstatement;import  Java.sql.sqlexception;public class jdbcbatchdemo {public static void main (String [] args]  {String sql =  insert into users (USERNAME,PASSWORD,CLASSID)   VALUES (?,?,?) "; Connection connection = jdbcconnection.getconnection ();try {preparedstatement  Pstatement = connection.preparestatement (SQL);for  (int i = 0; i <  10; i++)  {pstatement.setstring (1,  "test-"  + i);p statement.setstring (2,  " root-" + i);p statement.setint (3, i);p Statement.addbatch (); //  Add a piece of data to the batch waiting to be executed together}int  result[] = pstatement.executebatch (); //  Bulk execution System.out.println ("Plugged in  "  +  result.length +  "  data");p statement.close (); Connection.close ();}  catch  (sqlexception e) &NBsp {E.printstacktrace ();}}}

The effect is as follows:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7E/D6/wKioL1cKUpCwdAV1AABEGh-t0PM567.png "title=" 20160407232455_56836.png "alt=" Wkiol1ckupcwdav1aabegh-t0pm567.png "/>

Output:

10 data has been inserted

PS: In fact, this article is followed by a few previous articles on JDBC, so the specific steps to get a JDBC connection are omitted, and interested children's shoes can look at the previous articles in this series, such as:http://www.zifangsky.cn/378.html


This article is from "Zifangsky's personal blog" blog, make sure to keep this source http://983836259.blog.51cto.com/7311475/1762435

Java Basic Series 13:JDBC batch processing introduction

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.