Java中怎样实现批量删除操作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java中怎样实现批量删除操作相关的知识,希望对你有一定的参考价值。
本文是记录Java中实现批量删除操纵(Java对数据库进行事务处置),在开始之前先来看上面这样的一个页面图:
上面这张图片表现的是从数据库中查询出的出租信息,信息中进行了分页处置,然后每行的后面提供了一个复选按钮和对应的一个删除操纵,可以选中多个进行操纵,这里主要是进行删除操纵。在执行删除操纵之前先要选中对应的行信息,点击删除选中按钮进行删除。当进行多条信息删除的时候,需要使用java的事务处置机制对数据库进行删除,也就是说删除的时候如果选中的要删除的说有信息其中一条没有成功删除的话,那么就都不删除。
现在是在java中对数据库实现这一操纵,我们可看上面的代码,它实现了对数据库的批量删除操纵,代码如下:
public Connection con=null;
public PreparedStatement pstmt=null;
/**
* 失掉连接对象
*/
public void getConnection()
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/zufang?user=root&password=root&useUnicode=true&characterEncoding=GB2312";
try
Class.forName(driver);
con=DriverManager.getConnection(url,"root","root");
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SQLException e)
e.printStackTrace();
/** * 批量删除信息表中的信息 * @param sql * @param param * @return */ public boolean updateBatchDel(String sql,String[] param) boolean flag = false; getConnection(); try con.setAutoCommit(false); pstmt = con.prepareStatement(sql); for(int i =0 ;i<param.length;i++) pstmt.setString(1,param[i].trim()); pstmt.addBatch(); pstmt.executeBatch(); //批量执行 con.commit();//提交事务 flag = true; catch (SQLException e) try con.rollback(); //进行事务回滚 catch (SQLException ex) ex.printStackTrace(); finally closeAll(null,pstmt,con); return flag;
当然上面是进行批量删除,如果我们只删除一条信息的话也可以使用独自的删除方法,即是:点击删除,当然上面的方法也是可以完成的,还是再看一下吧:
/**
* 删除某条求租表中的信息
* @param id 删除信息的id
* @return 如果删除成功,返回true;否则返回false
*/
public boolean delQiuZu(String id)
boolean flag=false;
String sql="delete from qiuzhu where id=?";
String[] param=id;
flag=bd.updateDate(sql, param);
return flag;
控制器servlet中的处置操纵代码如下:
package com.sxt.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sxt.biz.ChuZuBiz;
import com.sxt.biz.PageBiz;
import com.sxt.biz.QiuZuBiz;
public class OutDateQiuzuServlet extends HttpServlet
QiuZuBiz qzb=new QiuZuBiz();
PageBiz pb=new PageBiz();
int pagesize=10;
boolean flag=true;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
int countpage=pb.getOutDatePageCountQiuzu(pagesize);
request.setAttribute("countpage", countpage);
String nowpage=request.getParameter("nowpage");
String id=request.getParameter("id");
PrintWriter out = response.getWriter();
String command = request.getParameter("command");
if ("del".equals(command))
String[] qiuzuIds = request.getParameterValues("selectFlag");
boolean flag = qzb.delQiuzuMany(qiuzuIds);
if(flag)
out.print("<script>alert('删除成功!');</script>");
else
out.print("<script>alert('删除失败!');</script>");
if(nowpage==null)
nowpage="1";
if(Integer.valueOf(nowpage)<=0)
nowpage="1";
if(Integer.valueOf(nowpage)>countpage)
nowpage=countpage+"";
if(id!=null)
flag=qzb.delQiuZu(id);
request.setAttribute("currentpage", nowpage);
List list=qzb.getOutDateQiuZuInfo(Integer.valueOf(nowpage), pagesize);
request.setAttribute("list1", list);
if(flag)
request.getRequestDispatcher("admin/OutDateQiuzu.jsp").forward(request, response);
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
doGet(request,response);
上面是对数据库的操纵代码,上面看一下页面中怎样实现的,代码如下:
每日一道理
灯,带有一种明亮的光,每当深夜来临,是它陪伴着你,如此默默无闻。它是平凡的,外表华丽与否,那都是一样的,珍珠点缀,水晶加饰的灯它只能用以装饰,来满足人们的虚荣心,比起这,普普通通的日光灯是幸运的,因为它照明的本性没有改变,如同生活中的一部分人平平凡凡却实实在在。
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="#" prefix="c" %>
<html>
<head>
<title>house</title>
<script type="text/javascript">
//删除用户控制
function deleteSelect()
var select = document.getElementsByName("selectFlag");
var flag = false;
for (var i=0; i<select.length; i++)
if (select[i].checked)
flag = true;
break;
if (!flag)
alert("请选择需要删除的过期求租信息!");
return;
if (window.confirm("确认要删除过期的求租信息吗?"))
with (document.getElementById("userform"))
action="OutDateQiuzuServlet?command=del";
method="post";
submit();
//全选/反选操纵
function checkAll(ifAll)
var select = document.getElementsByName("selectFlag");
for(var i = 0;i<select.length;i++)
select[i].checked = ifAll.checked;
</script>
</head>
<link rel="stylesheet" href="./skin/css/lianjie.css" type="text/css" />
<body>
<form name="userform" action="ChuzuServlet" method="get">
<table width="1000" height="80" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="40" align="center" bgcolor="#F1F1F1"><font color="#FF0000"><b>已过期的求租信息</b></font></td>
</tr>
<tr>
<td align="left">
<input name="btnDelete" class="button1" type="button"
id="btnDelete" value="删除选中" onClick="deleteSelect()">
</td>
</tr>
</table>
<table width="1000" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC" style="word-break:break-all;">
<tr align="center">
<td width="15%" height="25" bgcolor="#F1F1F1"><font size="3"> <input type="checkbox" name="ifAll" title="全选/反选" onClick="checkAll(this)" checked="checked"/></font></td>
<td width="10%" bgcolor="#F1F1F1"><font size="3">期望区域</font></td>
<td width="15%" bgcolor="#F1F1F1"><font size="3">装修水平</font></td>
<td width="10%" bgcolor="#F1F1F1"><font size="3">房型</font></td>
<td width="10%" bgcolor="#F1F1F1"><font size="3">面积(平米)</font></td>
<td width="10%" bgcolor="#F1F1F1"><font size="3">价格(元)</font></td>
<td width="10%" bgcolor="#F1F1F1"><font size="3">添加日期</font></td>
<td width="10%" bgcolor="#F1F1F1"><font size="3">有效天数</font></td>
<td width="10%" bgcolor="#F1F1F1"><font size="3">残余天数</font></td>
</tr>
<c:choose>
<c:when test="$empty list1">
<tr><td colspan="8" align="center"><font color="red">还没有过期的求租信息!</font></td></tr>
</c:when>
<c:otherwise>
<c:forEach var="qiuzu" items="$list1" >
<tr>
<td height="25" align="center" bgcolor="#FFFFFF"><input type="checkbox" name="selectFlag" value="$qiuzu.id" checked="checked"/></font>
<a href="javascript:if(confirm('确定要删除这条过期的求租信息吗?'))location.href='OutDateQiuzuServlet?id=$qiuzu.id'" style="COLOR: #0000ff;font-size:14px; TEXT-DECORATION:none;"><font size="2">删除</font></a></td>
<td align="center" bgcolor="#FFFFFF"><font size="2">$qiuzu.qwqy</font></td>
<td align="center" bgcolor="#FFFFFF"><font size="2">$qiuzu.zxcd</font></td>
<td align="center" bgcolor="#FFFFFF"><font size="2">$qiuzu.hx</font></td>
<td align="center" bgcolor="#FFFFFF"><font size="2">$qiuzu.jzmj</font></td>
<td align="center" bgcolor="#FFFFFF"><font size="2">$qiuzu.zj</font></td>
<td align="center" bgcolor="#FFFFFF"><font size="2">$qiuzu.addDate</font></td>
<td align="center" bgcolor="#FFFFFF"><font size="2">$qiuzu.yxts</font></td>
<td align="center" bgcolor="#FFFFFF"><font size="2" color="red">$qiuzu.syts</font></td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
</p>
<table width="300" align="center" >
<tr>
<td align="center"><font size="2">共$countpage页</font></td>
<td align="center"><font size="2">$currentpage/$countpage页</font></td>
<td align="center"><a href="OutDateQiuzuServlet?nowpage=$1"><font size="2">首页</font></a></td>
<td align="center"><a href="OutDateQiuzuServlet?nowpage=$currentpage-1"><font size="2">上一页</font></a></td>
<td align="center"><a href="OutDateQiuzuServlet?nowpage=$currentpage+1"><font size="2">下一页</font></a></td>
<td align="center"><a href="OutDateQiuzuServlet?nowpage=$countpage"><font size="2">尾页</font></a></td>
</tr>
</table>
</form>
</body>
</html>
进行编写编程代码就能实现批量删除操作。
具体代码如下:
[java] <SPAN style="WHITE-SPACE: pre"> </SPAN>public Connection con=null;
public PreparedStatement pstmt=null;
/**
* 得到连接对象
*/
public void getConnection()
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/zufang?
user=root&password=root&useUnicode=true&characterEncoding=GB2312";
try
Class.forName(driver);
con=DriverManager.getConnection(url,"root","root");
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SQLException e)
e.printStackTrace();
public Connection con=null;
public PreparedStatement pstmt=null;
/**
* 得到连接对象
*/
public void getConnection()
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/zufang?
user=root&password=root&useUnicode=true&characterEncoding=GB2312";
try
Class.forName(driver);
con=DriverManager.getConnection(url,"root","root");
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SQLException e)
e.printStackTrace();
[java] <SPAN style="WHITE-SPACE: pre"> </SPAN>/**
* 批量删除信息表中的信息
* @param sql
* @param param
* @return
*/
public boolean updateBatchDel(String sql,String[] param)
boolean flag = false;
getConnection();
try
con.setAutoCommit(false);
pstmt = con.prepareStatement(sql);
for(int i =0 ;i<param.length;i++)
pstmt.setString(1,param[i].trim());
pstmt.addBatch();
pstmt.executeBatch(); //批量执行
con.commit();//提交事务
flag = true;
catch (SQLException e)
try
con.rollback(); //进行事务回滚
catch (SQLException ex)
ex.printStackTrace();
finally
closeAll(null,pstmt,con);
return flag;
/**
* 批量删除信息表中的信息
* @param sql
* @param param
* @return
*/
public boolean updateBatchDel(String sql,String[] param)
boolean flag = false;
getConnection();
try
con.setAutoCommit(false);
pstmt = con.prepareStatement(sql);
for(int i =0 ;i<param.length;i++)
pstmt.setString(1,param[i].trim());
pstmt.addBatch();
pstmt.executeBatch(); //批量执行
con.commit();//提交事务
flag = true;
catch (SQLException e)
try
con.rollback(); //进行事务回滚
catch (SQLException ex)
ex.printStackTrace();
finally
closeAll(null,pstmt,con);
return flag;
上面是进行批量删除的编程码。
参考技术B 看下面的代码,它实现了对数据库的批量删除操作,代码如下:[java] public Connection con=null;
public PreparedStatement pstmt=null;
/**
* 得到连接对象
*/
public void getConnection()
String driver="com.mysql.jdbc.Driver";
String
url="jdbc:mysql://localhost:3306/zufang?user=root&password=root&useUnicode=true&characterEncoding=GB2312";
try
Class.forName(driver);
con=DriverManager.getConnection(url,"root","root");
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SQLException e)
e.printStackTrace();
public Connection con=null;
public PreparedStatement pstmt=null;
/**
* 得到连接对象
*/
public void getConnection()
String driver="com.mysql.jdbc.Driver";
String
url="jdbc:mysql://localhost:3306/zufang?user=root&password=root&useUnicode=true&characterEncoding=GB2312";
try
Class.forName(driver);
con=DriverManager.getConnection(url,"root","root");
catch (ClassNotFoundException e)
e.printStackTrace();
catch (SQLException e)
e.printStackTrace();
[java] /**
* 批量删除信息表中的信息
* @param sql
* @param param
* @return
*/
public boolean updateBatchDel(String sql,String[] param)
boolean flag = false;
getConnection();
try
con.setAutoCommit(false);
pstmt = con.prepareStatement(sql);
for(int i =0 ;i
pstmt.setString(1,param[i].trim());
pstmt.addBatch();
pstmt.executeBatch(); //批量执行
con.commit();//提交事务
flag = true;
catch (SQLException e)
try
con.rollback(); //进行事务回滚
catch (SQLException ex)
ex.printStackTrace();
finally
closeAll(null,pstmt,con);
return flag;
/**
* 批量删除信息表中的信息
* @param sql
* @param param
* @return
*/
public boolean updateBatchDel(String sql,String[] param)
boolean flag = false;
getConnection();
try
con.setAutoCommit(false);
pstmt = con.prepareStatement(sql);
for(int i =0 ;i
pstmt.setString(1,param[i].trim());
pstmt.addBatch();
pstmt.executeBatch(); //批量执行
con.commit();//提交事务
flag = true;
catch (SQLException e)
try
con.rollback(); //进行事务回滚
catch (SQLException ex)
ex.printStackTrace();
finally
closeAll(null,pstmt,con);
return flag;
当然上面是进行批量删除,如果我们只删除一条信息的话也可以使用单独的删除方法,即是:点击删除,当然上面的方法也是可以完成的,还是再看一下吧:
[java] /**
* 删除某条求租表中的信息
* @param id 删除信息的id
* @return 如果删除成功,返回true;否则返回false
*/
public boolean delQiuZu(String id)
boolean flag=false;
String sql="delete from qiuzhu where id=?";
String[] param=id;
flag=bd.updateDate(sql, param);
return flag;
/**
* 删除某条求租表中的信息
* @param id 删除信息的id
* @return 如果删除成功,返回true;否则返回false
*/
public boolean delQiuZu(String id)
boolean flag=false;
String sql="delete from qiuzhu where id=?";
String[] param=id;
flag=bd.updateDate(sql, param);
return flag;
控制器servlet中的处理操作代码如下:
[java] package com.sxt.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sxt.biz.ChuZuBiz;
import com.sxt.biz.PageBiz;
import com.sxt.biz.QiuZuBiz;
public class OutDateQiuzuServlet extends HttpServlet
QiuZuBiz qzb=new QiuZuBiz();
PageBiz pb=new PageBiz();
int pagesize=10;
boolean flag=true;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
int countpage=pb.getOutDatePageCountQiuzu(pagesize);
request.setAttribute("countpage", countpage);
String nowpage=request.getParameter("nowpage");
String id=request.getParameter("id");
PrintWriter ut = response.getWriter();
String command = request.getParameter("command");
if ("del".equals(command))
String[] qiuzuIds = request.getParameterValues("selectFlag");
boolean flag = qzb.delQiuzuMany(qiuzuIds);
if(flag)
out.print("alert('删除成功!');");
else
out.print("alert('删除失败!');");
if(nowpage==null)
nowpage="1";
if(Integer.valueOf(nowpage)<=0)
nowpage="1";
if(Integer.valueOf(nowpage)>countpage)
nowpage=countpage+"";
if(id!=null)
flag=qzb.delQiuZu(id);
request.setAttribute("currentpage", nowpage);
List list=qzb.getOutDateQiuZuInfo(Integer.valueOf(nowpage), pagesize);
request.setAttribute("list1", list);
if(flag)
request.getRequestDispatcher("admin/OutDateQiuzu.jsp").forward(request, response);
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
doGet(request,response);
package com.sxt.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sxt.biz.ChuZuBiz;
import com.sxt.biz.PageBiz;
import com.sxt.biz.QiuZuBiz;
public class OutDateQiuzuServlet extends HttpServlet
QiuZuBiz qzb=new QiuZuBiz();
PageBiz pb=new PageBiz();
int pagesize=10;
boolean flag=true;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
int countpage=pb.getOutDatePageCountQiuzu(pagesize);
request.setAttribute("countpage", countpage);
String nowpage=request.getParameter("nowpage");
String id=request.getParameter("id");
PrintWriter ut = response.getWriter();
String command = request.getParameter("command");
if ("del".equals(command))
String[] qiuzuIds = request.getParameterValues("selectFlag");
boolean flag = qzb.delQiuzuMany(qiuzuIds);
if(flag)
out.print("alert('删除成功!');");
else
out.print("alert('删除失败!');");
if(nowpage==null)
nowpage="1";
if(Integer.valueOf(nowpage)<=0)
nowpage="1";
if(Integer.valueOf(nowpage)>countpage)
nowpage=countpage+"";
if(id!=null)
flag=qzb.delQiuZu(id);
request.setAttribute("currentpage", nowpage);
List list=qzb.getOutDateQiuZuInfo(Integer.valueOf(nowpage), pagesize);
request.setAttribute("list1", list);
if(flag)
request.getRequestDispatcher("admin/OutDateQiuzu.jsp").forward(request, response);
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
doGet(request,response);
上面是对数据库的操作代码,下面看一下页面中怎样实现的,代码如下:
[html] <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
//删除用户控制
function deleteSelect()
var select = document.getElementsByName("selectFlag");
var flag = false;
for (var i=0; i
if (select[i].checked)
flag = true;
break;
if (!flag)
alert("请选择需要删除的过期求租信息!");
return;
if (window.confirm("确认要删除过期的求租信息吗?"))
with (document.getElementById("userform"))
action="OutDateQiuzuServlet?command=del";
method="post";
submit();
//全选/反选操作
function checkAll(ifAll)
var select = document.getElementsByName("selectFlag");
for(var i = 0;i
select[i].checked = ifAll.checked;
以上是关于Java中怎样实现批量删除操作的主要内容,如果未能解决你的问题,请参考以下文章