1 /**2 * Delete users in bulk3 * 4 * Complete deletion with one statement5 * Submit only once6 * 7 * Use statement to fight the string8 * Delete from T_user where user_id in (' AAAA ', ' afff ', ' eeee ')9 * @paramUserIDsTen */ One Public voidDeluser (string[] userids) { AStringBuilder Sbstr =NewStringBuilder (); - for(inti=0; i<userids.length; i++) { -Sbstr.append ("'") the . Append (Userids[i]) -. Append ("'") -. Append (","); - } +String sql = "Delete from T_user where user_id in (" + sbstr.substring (0, Sbstr.length ()-1) + ")"; -System.out.println ("Usermanager.deluser ()-->>" +sql); +Connection conn =NULL; AStatement stmt =NULL; at Try { -conn =dbutil.getconnection (); -stmt =conn.createstatement (); - stmt.executeupdate (SQL); -}Catch(SQLException e) { - e.printstacktrace (); in}finally { - Dbutil.close (stmt); to Dbutil.close (conn); + } -}
1 /**2 * Delete users in bulk3 * 4 * Complete deletion with one statement5 * Submit only once6 * 7 * Using the PreparedStatement placeholder method8 * 9 * Delete from T_user where user_id in (?,?,?)Ten * @paramUserIDs One */ A Public voidDeluser (string[] userids) { -StringBuilder Sbstr =NewStringBuilder (); - for(inti=0; i<userids.length; i++) { theSbstr.append ("?")); - if(I < (userids.length-1)) { -Sbstr.append (","); - } + } -String sql = "Delete from T_user where user_id in (" + sbstr.tostring () + ")"; +System.out.println ("Usermanager.deluser ()-->>" +sql); AConnection conn =NULL; atPreparedStatement pstmt =NULL; - Try { -conn =dbutil.getconnection (); -PSTMT =conn.preparestatement (SQL); - for(inti=0; i<userids.length; i++) { -Pstmt.setstring (i + 1), Userids[i]); in } - pstmt.executeupdate (); to}Catch(SQLException e) { + e.printstacktrace (); -}finally { the Dbutil.close (pstmt); * Dbutil.close (conn); $ }Panax Notoginseng}
JDBC Note 3 The database is PreparedStatement and censored through the
Summary: Constantly improve their programming efficiency! ~~~~~
"DRP"-"SQL" bulk Delete