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