About Dbutils queryrunner View batch DELETE statement batch

Source: Internet
Author: User

Bulk Delete

public void Delbooks (string[] IDs) throws SQLException {queryrunner qr = new Queryrunner (C3p0utils.getdatasource ()); O bject[][] params = new object[ids.length][];//high dimension determines the number of times the SQL statement is executed, and the lower dimension is the given? Assignment for (int i = 0; i < params.length; i++) {params[i] = new object[]{ids[i]};//to "? "Assignment}qr.batch (" Delete from books where id=? ", params);}

Source Code implementation:

 Public int[] Batch (String sql, object[][] params)throwsSQLException {Connection conn= This. Prepareconnection ();return  This. BATCH (conn,true, SQL, params);}Private int[] Batch (Connection conn,BooleanCloseconn, String sql, object[][] params)throwsSQLException {if(conn = =NULL) {Throw NewSQLException ("Null connection");}if(sql = =NULL) {if(Closeconn) {close (conn);}Throw NewSQLException ("Null SQL statement");}if(Params = =NULL) {if(Closeconn) {close (conn);}Throw NewSQLException ("Null parameters. If parameters aren ' t need, pass an empty array. ");} PreparedStatement stmt=NULL;int[] rows =NULL;Try{stmt= This. preparestatement (conn, SQL); for(inti = 0; i < params.length; i++) { This. Fillstatement (stmt, params[i]); Stmt.addbatch ();} Rows=Stmt.executebatch ();} Catch(SQLException e) { This. Rethrow (E, SQL, (object[]) params);} finally{Close (stmt);if(Closeconn) {close (conn);}}returnrows;}

Interpretation: Since the params is a two-dimensional array, assigning values to PreparedStatement uses a for loop and then Preparedstatement.addbatch () to bulk Add, then executes ExecuteBatch ( ) to operate.

This article transferred from: https://www.cnblogs.com/wang-meng/p/5525389.html

About Dbutils queryrunner View batch DELETE statement batch

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.