鏁版嵁搴擄細SQLServer2005
鏈嶅姟鍣細Tomcat6.0
寮€鍙戝伐鍏鳳細MyEclipse6.0
閲囩敤妗嗘灦锛歋truts1.2
璐村嚭瀹屾暣浠g爜 锛?
鍒涘緩SQL鑴氭湰锛?CREATE TABLE [dbo].[batch](
聽[id] [int] IDENTITY(1,1) NOT NULL,
聽[name] [varchar](50) NULL,
聽[password] [varchar](50) NULL,
聽CONSTRAINT [PK_batch] PRIMARY KEY CLUSTERED
(
聽[id] ASC
)WITH (PAD_INDEX聽 = OFF, STATISTICS_NORECOMPUTE聽 = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS聽 = ON, ALLOW_PAGE_LOCKS聽 = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
杩炴帴鏁版嵁搴撶被锛?public class DBConnection {
聽private static DBConnection dbc = null;
聽public static Connection getDBConnection() throws Exception {
聽聽if (dbc == null) {
聽聽聽dbc = new DBConnection();
聽聽}
聽聽return dbc._getConnection();
聽}
聽public Connection _getConnection() throws Exception {
聽聽Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
聽聽return DriverManager.getConnection(
聽聽聽聽"jdbc:sqlserver://localhost:1433;DatabaseName=dbtest", "sa",
聽聽聽聽"sqlserver2005");
聽}
}
閲囩敤DAO宸ュ巶妯″紡锛?
PO绫夥細
public class Batch {
聽private int id;
聽private String name;
聽private String password;
聽public int getId() {
聽聽return id;
聽}
聽public void setId(int id) {
聽聽this.id = id;
聽}
聽public String getName() {
聽聽return name;
聽}
聽public void setName(String name) {
聽聽this.name = name;
聽}
聽public String getPassword() {
聽聽return password;
聽}
聽public void setPassword(String password) {
聽聽this.password = password;
聽}
}
DAO鎺ュ彛锛?
public interface BatchDAO {
聽public List<Batch> queryAll();
聽public void deleteSelect(int[] str);
}
瀹炵幇鎺ュ彛绫夥細
public class BatchDAOImpl implements BatchDAO {
聽Connection con = null;
聽PreparedStatement pst = null;
聽ResultSet rst = null;
聽public void deleteSelect(int[] str) {
聽聽String sql = "delete from batch where id in(";
聽聽if(str.length==1){
聽聽聽sql=sql+str[0]+")";}else{
聽聽聽聽for(int i=0;i<str.length;i++){
聽聽聽聽聽if(i==0){
聽聽聽聽聽聽sql=sql+str[0];
聽聽聽聽聽}
聽聽聽聽聽else if(i==str.length-1){
聽聽聽聽聽聽sql=sql+","+str[i]+")";
聽聽聽聽聽}else{
聽聽聽聽聽聽sql=sql+","+str[i];
聽聽聽聽聽}
聽聽聽聽}
聽聽聽}
聽聽try {
聽聽聽con = DBConnection.getDBConnection();
聽聽聽pst = con.prepareStatement(sql);
聽聽聽pst.execute();
聽聽} catch (Exception e) {
聽聽聽e.printStackTrace();
聽聽} finally {
聽聽聽try {
聽聽聽聽if (pst != null)
聽聽聽聽聽pst.close();
聽聽聽聽if (con != null)
聽聽聽聽聽con.close();
聽聽聽} catch (Exception e) {
聽聽聽聽e.printStackTrace();
聽聽聽}
聽聽}
聽}
聽public List<Batch> queryAll() {
聽聽List<Batch> list = new ArrayList<Batch>();
聽聽String sql = "select * from batch";
聽聽try {
聽聽聽con = DBConnection.getDBConnection();
聽聽聽pst = con.prepareStatement(sql);
聽聽聽rst = pst.executeQuery();
聽聽聽while (rst.next()) {
聽聽聽聽Batch b = new Batch();
聽聽聽聽b.setId(rst.getInt("id"));
聽聽聽聽b.setName(rst.getString("name"));
聽聽聽聽b.setPassword(rst.getString("password"));
聽聽聽聽list.add(b);
聽聽聽}
聽聽} catch (Exception e) {
聽聽聽e.printStackTrace();
聽聽} finally {
聽聽聽try {
聽聽聽聽if (rst != null)
聽聽聽聽聽rst.close();
聽聽聽聽if (pst != null)
聽聽聽聽聽pst.close();
聽聽聽聽if (con != null)
聽聽聽聽聽con.close();
聽聽聽} catch (SQLException e) {
聽聽聽聽e.printStackTrace();
聽聽聽}
聽聽}
聽聽return list;
聽}
}
宸ュ巶绫夥細
public class DAOFactory {
聽public static BatchDAO getBatchDAOImplInstance() {
聽聽return new BatchDAOImpl();
聽}
}
Action:缁ф壙DispatchAction
public class BatchAction extends DispatchAction {
聽
聽
聽public ActionForward queryAll(ActionMapping mapping, ActionForm form,
聽聽聽HttpServletRequest request, HttpServletResponse response) {
聽聽List<Batch> list = DAOFactory.getBatchDAOImplInstance().queryAll();
聽聽request.setAttribute("list", list);
聽聽return mapping.findForward("display");
聽}
聽public ActionForward deleteSelect(ActionMapping mapping, ActionForm form,
聽聽聽HttpServletRequest request, HttpServletResponse response) {
聽聽String[] id = request.getParameterValues("name");
聽聽int[] i = new int[id.length];
聽聽for (int j = 0; j < id.length; j++) {
聽聽聽i[j] = Integer.parseInt(id[j]);
聽聽}
聽聽DAOFactory.getBatchDAOImplInstance().deleteSelect(i);
聽聽return mapping.findForward("delete");
聽}
}
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
聽 <data-sources />
聽 <form-beans />
聽 <global-exceptions />
聽 <global-forwards />
聽 <action-mappings >
聽聽聽 <action
聽聽聽聽聽 input="/index.jsp"
聽聽聽聽聽 parameter="batch"
聽聽聽聽聽 path="/batch"
聽聽聽聽聽 type="com.batch.delete.struts.action.BatchAction"
聽聽聽聽聽 validate="false">
聽聽聽聽聽 <forward name="display" path="/batchList.jsp" />
聽聽聽聽聽 <forward name="delete" path="/batchList.jsp" />
聽聽聽 </action>
聽 </action-mappings>
聽 <message-resources parameter="com.batch.delete.struts.ApplicationResources" />
</struts-config>
浠ヤ笅鏄〉闈㈠唴瀹癸細
<%@ page contentType="text/html;charset=GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
聽<head>
聽聽<title>batch_delete</title>
聽</head>
聽<script type="text/javascript" src="js/check.js"></script>
聽<body>
聽聽<center>
聽聽聽<h2>
聽聽聽聽Batch Delete Database Data Sample Display
聽聽聽</h2>
聽聽聽<hr>
聽聽聽<form action="batch.do?batch=deleteSelect" name="myForm"
聽聽聽聽method="post">
聽聽聽聽<table style="border-collapse: collapse"
聽聽聽聽聽bordercolor="#1E90FF" height="60" cellpadding="3">
聽聽聽聽聽<tr style="background: #87CEFA;">
聽聽聽聽聽聽<td>
聽聽聽聽聽聽聽
聽聽聽聽聽聽</td>
聽聽聽聽聽聽<td>
聽聽聽聽聽聽聽ID
聽聽聽聽聽聽</td>
聽聽聽聽聽聽<td>
聽聽聽聽聽聽聽Name
聽聽聽聽聽聽</td>
聽聽聽聽聽聽<td>
聽聽聽聽聽聽聽Password
聽聽聽聽聽聽</td>
聽聽聽聽聽</tr>
聽聽聽聽聽<logic:iterate id="li" name="list">
聽聽聽聽聽聽<tr>
聽聽聽聽聽聽聽<td>
聽聽聽聽聽聽聽聽<input type="checkbox" name="name" value="${li.id }">
聽聽聽聽聽聽聽</td>
聽聽聽聽聽聽聽<td>
聽聽聽聽聽聽聽聽${li.id }
聽聽聽聽聽聽聽</td>
聽聽聽聽聽聽聽<td>
聽聽聽聽聽聽聽聽${li.name }
聽聽聽聽聽聽聽</td>
聽聽聽聽聽聽聽<td>
聽聽聽聽聽聽聽聽${li.password }
聽聽聽聽聽聽聽</td>
聽聽聽聽聽聽</tr>
聽聽聽聽聽</logic:iterate>
聽聽聽聽聽<tr>
聽聽聽聽聽聽<td colspan="4">
聽聽聽聽聽聽聽<input type="button" value="SelectAll" 慰nclick="checkAll()">
聽聽聽聽聽聽聽
聽聽聽聽聽聽聽<input type="submit" value="DeleteSelect">
聽聽聽聽聽聽聽
聽聽聽聽聽聽聽<input type="reset" value="ResetSelect">
聽聽聽聽聽聽</td>
聽聽聽聽聽</tr>
聽聽聽聽</table>
聽聽聽</form>
聽聽</center>
聽</body>
</html>
JSP椤甸潰寮曞叆涓€涓猨s鏂囦歡锛?
function checkAll() {
聽for (var i = 0; i < myForm.name.length; i++) {
聽聽myForm.name[i].checked=true;
聽}
}