文是记录Java对数据库进行事务处理(删除操作),在开始之前先来看下面这样的一个页面图:

                上面这张图片显示的是从数据库中查询出的用户信息,信息中进行了分页处理,然后每行的前面提供了一个复选按钮,可以选中多个进行操作,这里主要是进行删除操作。在执行删除操作之前先先要选中对应的行信息,点击删除进行删除。当进行多条信息删除的时候,需要使用java的事务处理机制对数据库进行删除,也就是说删除的时候如果选中的要删除的说有信息其中一条没有成功删除的话,那么就都不删除。
                他们就像×××了某个组织,他们都很讲义气,要死他们一起死,要活一起活,谁也不会落下。现在是在java中要对数据库实现这一操作,我们可看下面的代码,它实现了对数据库的批量删除操作,并且同生死,代码如下:
[java] view plaincopy
/**
         * 根据用户代码进行批量删除
         * @param userIds
         * @return
         */
    
         public boolean delUsers(String[] userIds){    
                 boolean flag = false;    
                Connection conn = null;    
                PreparedStatement pstmt = null;        
                String sql = "delete from t_user where user_id=?";    
                 try {    
                        conn = DbUtil.getConnection();    
                        conn.setAutoCommit( false);    
                        pstmt = conn.prepareStatement(sql);    
                         for( int i =0 ;i                                pstmt.setString(1,userIds[i].trim());    
                                pstmt.addBatch();                                
                        }    
                        pstmt.executeBatch(); //批量执行    
                        conn.commit(); //提交事务    
                        flag = true;    
                } catch (SQLException e) {    
                         try {    
                                conn.rollback(); //进行事务回滚    
                        } catch (SQLException ex) {    
                        }    
                } finally {    
                        DbUtil.close(pstmt);    
                        DbUtil.close(conn);    
                }    
                 return flag;    
        }    
当然上面是进行批量删除,如果我们只删除一条信息的话也可以使用单独的删除方法,当然上面的方法也是可以完成的,管他的,还是再看一下吧:
[java] view plaincopy
/**
         * 根据用户id进行删除单条信息    
         * @param userId
         * @return
         */
    
         public boolean delUser(String userId){    
                 boolean flag = false;    
                String sql = "delete from t_user where user_id=?";    
                Connection conn = null;    
                PreparedStatement pstmt = null;    
                 try {    
                        conn = DbUtil.getConnection();    
                        pstmt = conn.prepareStatement(sql);    
                        pstmt.setString(1, userId);    
                         if(pstmt.executeUpdate()>0){    
                                flag = true;    
                        }    
                } catch (SQLException e) {    
                        e.printStackTrace();    
                } finally {    
                        DbUtil.close(pstmt);    
                        DbUtil.close(conn);    
                }    
                 return flag;    
        }    
上面是对数据库的操作代码,下面看一下页面中怎样实现的,代码如下:
[html] view plaincopy
"code" 
      class= 
      "java"><%@ page language= 
      "java" contentType= 
      "text/html; charset=UTF-8"     
      
        pageEncoding= "UTF-8"%>    
<%@ page import= "org.ml.drp.sysmgr.domain.*"%>    
<%@ page import= "org.ml.drp.sysmgr.manager.*" %>    
<%@ page import= "org.ml.drp.util.*"%>        
<%@ page import= "java.util.*"%>        
<%    
        String path = request.getContextPath();    
        String basePath = request.getScheme()+ "://"+request.getServerName()+":"+request.getServerPort()+path+"/";    
        //进行用户信息的删除    
        String command = request.getParameter("command");    
        if ("del".equals(command)) {    
                String[] userIds = request.getParameterValues("selectFlag");    
                boolean flag = false;    
                if(userIds.length==1){    
                        flag = UserManager.getInstance().delUser(userIds[0]);    
                }else{    
                        flag = UserManager.getInstance().delUsers(userIds);    
                }    
                if(flag){    
                        out.print("");    
                }else{    
                        out.print("");    
                }    
        }    
            
        //下面为分页显示做准备    
        String currentPageStr = request.getParameter("currentPage");    
        String pageSizeStr = request.getParameter("pageSize");    
        int currentPage = 1; //当前页码    
        int pageSize = 8 ;//每页显示数量    
        if(currentPageStr!=null){    
                currentPage = Integer.parseInt(currentPageStr);    
        }    
        if(pageSizeStr!=null){    
                pageSize = Integer.parseInt(pageSizeStr);    
        }    
        Page p = UserManager.getInstance().findByPage(currentPage,pageSize);    
%>    
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">    
    
            
                "Content-Type"
content="text/html; charset=UTF-8">    
                用户维护    
                        "stylesheet" href="../style/drp.css">    
                    
            
            
        class="body1">    
                
"userform" id="userform">    
                        
    
                                "95%" border="0" cellspacing="0" cellpadding="0"    
                                        height="35" >    
                                            
                                                    
                                            
                                            
                                                    
                                            
                                
class="p1" height="18" nowrap>    
                                                            
                                                
"522" class="p1" height="17" nowrap>    
                                               "../p_w_picpaths/mark_arrow_02.gif" width="14" height="14">    
                                                            
                                                        系统管理>>用户维护    
                                                
    
                                
"100%" align="center" size=0>    
                        
    
                        "95%" height="20" border="0" align="center"    
                                cellspacing="0" id="toolbar" style="height: 25;background-color: #696969">    
                                    
                                            
                                            
                                    
                        
"49%" class="rd19">    
                                                "#0033FF">查询列表    
                                        
"27%" nowrap class="rd16">    
                                                
"right">
    
                                        
    
                        "95%" border="1" cellspacing="0" cellpadding="0"    
                                align="center"class="table1">    
                                "50px">    
                                            
                                            
                                            
                                            
                                            
                                            
                                            
                                    
                                <%    
                                        List userList = p.getList();    
                                        Iterator iter = userList.iterator();    
                                        while(iter.hasNext()){    
                                                User user = (User)iter.next();    
                                                %>    
                                                
    
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            
                                                            
                                                    
                                                <%    
                                        }    
                                %>    
                        
"50" class="rd6">    
                                                    
                                        
"119" class="rd6" >    
                                                用户代码    
                                        
"152" class="rd6">    
                                                用户名称    
                                        
"152" class="rd6">    
                                                密码    
                                        
"166" class="rd6">    
                                                联系电话    
                                        
"150" class="rd6">    
                                                Email    
                                        
"153" class="rd6">    
                                                创建日期    
                                        
class="rd8" >    
                                                                    
                                                        
class="rd8">    
                                                                <%=user.getUserId() %>    
      "hidden" value="<%=user.getUserId() %>" name="userId">    
                                                        
class="rd8">    
                                                                <%=user.getUserName() %>    
  "hidden" value="<%=user.getUserName() %>" name="userName">    
                                                        
class="rd8">    
                                                                <%=user.getPassword() %>    
                 "hidden" value="<%=user.getPassword() %>" name="password">    
                                                        
class="rd8">    
                                                                <%=user.getContactTel() %>    
                        "hidden" value="<%=user.getContactTel() %>" name="contactTel">    
                                                        
class="rd8">    
                                                                <%=user.getEmail() %>    
                      "hidden" value="<%=user.getEmail() %>" name="email">    
                                                        
class="rd8">    
                                                                <%=user.getCreateDate() %>    
               "hidden" value="<%=user.getCreateDate() %>" name="userId">    
                                                        
    
                        "95%" height="30" border="0" align="center"    
                                cellpadding="0" cellspacing="0" style="height: 25;background-color: #696969">    
                                    
                                            
                                            
                                    
                        
class="rd19" height="2">    
                                                
"left">    
                                                        "#0033FF"> 共    
                                                        "#FF0000"> <%=p.getPageCount() %>    
                                                        "#0033FF">页            
                                                        "#0033FF">当前第    
                                                        "#FF0000"><%=p.getCurrentPage() %>    
                                                        "#0033FF">页    
                                                
    
                                        
class="rd19">    
                                                
"right">    
                                                        "btnTopPage" class="button1" type="button"    
                                                                id="btnTopPage" value="|<< " title="首页"    
                                           onClick="topPage(<%=1 %>)"<%=currentPage==1?"DISABLED":""%>>        
                                                        "btnPreviousPage" class="button1" type="button"    
                                                                id="btnPreviousPage" value=" <    " title="上页"    
              onClick="previousPage(<%=currentPage-1 %>)" <%=currentPage==1?"DISABLED":""%>>        
                                                        "btnNextPage" class="button1" type="button"    
        id="btnNextPage" value="    > " title="下页" onClick="nextPage(<%=currentPage+1 %>)" <%=currentPage==p.getPageCount()?"DISABLED":""%>>        
                                                        "btnBottomPage" class="button1" type="button"    
                                                                id="btnBottomPage" value=" >>|" title="尾页"    
          onClick="bottomPage(<%=p.getPageCount() %>)"<%=currentPage==p.getPageCount()?"DISABLED":""%>>        
                    "btnAdd" type="button" class="button1" id="btnAdd"    
                                                                value="添加" onClick="addUser()">    
                                                        "btnDelete" class="button1" type="button"    
                                                                id="btnDelete" value="删除" onClick="deleteUser()">    
                                                        "btnModify" class="button1" type="button"    
                                                                id="btnModify" value="修改" onClick="modifyUser()">    
                                                
    
                                        
    
                        

    
                                "hidden" name="currentPage" id="currentPage" value="1">    
                        

    
                
    
        
    
    

    

上面只是给出数据库删除操作的那一部分java代码,如果需要看看数据库连接以及分页的实现的话可以看前面写过的几篇文章,里面有详细的代码说明的。