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