"DRP"-"SQL" bulk Delete

Source: Internet
Author: User

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

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.