多种方式进行数据库的批量删除(删除用户信息为例)

文讲述的是多种方式进行数据库的批量删除(以删除用户信息为例)


方式一:

[java]  view plain copy
  1. /** 
  2.      * 批量删除用户 
  3.      *  
  4.      * 使用事务处理方式进行删除  
  5.      *  
  6.      * @param userIds 
  7.      * @return 
  8.      */  
  9.     public boolean delUsers(String[] userIds){  
  10.         boolean flag = false;  
  11.         Connection conn = null;  
  12.         PreparedStatement pstmt = null;    
  13.         String sql = "delete from t_user where user_id=?";  
  14.         try {  
  15.             conn = DbUtil.getConnection();  
  16.             conn.setAutoCommit(false);  
  17.             pstmt = conn.prepareStatement(sql);  
  18.             for(int i =0 ;i<userIds.length;i++){   
  19.                 pstmt.setString(1,userIds[i].trim());  
  20.                 pstmt.addBatch();                 
  21.             }   
  22.             pstmt.executeBatch(); //批量执行   
  23.             conn.commit();//提交事务  
  24.             flag = true;  
  25.         } catch (SQLException e) {  
  26.             try {  
  27.                 conn.rollback(); //进行事务回滚  
  28.             } catch (SQLException ex) {   
  29.             }   
  30.         }finally {  
  31.             DbUtil.close(pstmt);  
  32.             DbUtil.close(conn);  
  33.         }   
  34.         return flag;  
  35.     }  
方式二:
[java]  view plain copy
  1. /** 
  2.     * 根据用户id进行删除单条信息 
  3.     * @param userId 
  4.     * @return 
  5.     */  
  6.    public boolean delUser(String userId){  
  7.        boolean flag = false;  
  8.        String sql = "delete from t_user where user_id=?";  
  9.        Connection conn = null;  
  10.        PreparedStatement pstmt = null;  
  11.        try {  
  12.            conn = DbUtil.getConnection();  
  13.            pstmt = conn.prepareStatement(sql);  
  14.            pstmt.setString(1, userId);  
  15.            if(pstmt.executeUpdate()>0){  
  16.                flag = true;  
  17.            }  
  18.        } catch (SQLException e) {  
  19.            e.printStackTrace();  
  20.        }finally {  
  21.            DbUtil.close(pstmt);  
  22.            DbUtil.close(conn);  
  23.        }  
  24.        return flag;  
  25.    }  
  26.      
  27.    /** 
  28.     * 批量删除用户 
  29.     * 
  30.     * 直接使用for循环进行删除 
  31.     * 
  32.     * @param userIds 
  33.     * @return 
  34.     */  
  35.    public boolean delUsers(String[] userIds){  
  36.        boolean flag = false;  
  37.        for (int i = 0; i < userIds.length; i++) {  
  38.            flag = flag & delUser(userIds[i]);  
  39.        }  
  40.        return flag;  
  41.    }  

方式三:

 

[java]  view plain copy
  1. * 批量删除用户  
  2.  *   
  3.  * 采用一条语句完成删除,只提交一次  
  4.  *   
  5.  * 采用Statement拼串方式  
  6.  * delete from t_user where user_id in ('aaaa''afff''eeee')  
  7.  * @param userIds  
  8.  *//*  
  9. public void delUsers(String[] userIds) {  
  10.     StringBuilder sbStr = new StringBuilder();  
  11.     for (int i=0; i<userIds.length; i++) {  
  12.         sbStr.append("'")  
  13.         .append(userIds[i])  
  14.         .append("'")  
  15.         .append(",");  
  16.     }  
  17.     String sql = "delete from t_user where user_id in (" + sbStr.substring(0, sbStr.length() - 1) + ")";  
  18.     System.out.println("UserManager.delUser() -->>" + sql);  
  19.     Connection conn = null;  
  20.     Statement stmt = null;  
  21.     try {  
  22.         conn = DbUtil.getConnection();  
  23.         stmt = conn.createStatement();  
  24.         stmt.executeUpdate(sql);  
  25.     }catch(SQLException e) {  
  26.         e.printStackTrace();  
  27.     }finally {  
  28.         DbUtil.close(stmt);  
  29.         DbUtil.close(conn);  
  30.     }  
  31. }  

方式四:
[java]  view plain copy
  1. /** 
  2.      * 批量删除用户 
  3.      *  
  4.      * 采用一条语句完成删除,只提交一次  
  5.      * 采用PreparedStatement占位符方式  
  6.      * @param userIds 
  7.      */  
  8.     public void delUsers(String[] userIds) {  
  9.         StringBuilder sbStr = new StringBuilder();  
  10.         for (int i=0; i<userIds.length; i++) {  
  11.             sbStr.append("?");  
  12.             if (i < (userIds.length - 1)) {  
  13.                 sbStr.append(",");  
  14.             }  
  15.         }  
  16.         String sql = "delete from t_user where user_id in (" + sbStr.toString()  +  ")";   
  17.         Connection conn = null;  
  18.         PreparedStatement pstmt = null;  
  19.         try {  
  20.             conn = DbUtil.getConnection();  
  21.             pstmt = conn.prepareStatement(sql);  
  22.             for (int i=0; i<userIds.length; i++) {  
  23.                 pstmt.setString(i + 1, userIds[i]);  
  24.             }  
  25.             pstmt.executeUpdate();  
  26.         }catch(SQLException e) {  
  27.             e.printStackTrace();  
  28.         }finally {  
  29.             DbUtil.close(pstmt);  
  30.             DbUtil.close(conn);  
  31.         }  
  32.     }     

以上提供了四种方式进行批量删除数据库信息,其中第二种方式不推荐使用,其他三种方式个人觉得都很不错,可以借鉴使用。

以上几种方式的实现通过学习动力节点_王勇_Java项目视频_DRP完整版之后的结果,在这儿感谢王勇老师,很喜欢您的视频讲解。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值