当前位置:首页 > 网络编程 > 软件语言 > J2EE > 一个页面实现增删改查代码

一个页面实现增删改查代码

点击次数:45 次 发布日期:2008-11-11 16:57:53 作者:源代码网
源代码网推荐
广告载入中

源代码网整理以下<%@ page contentType="text/html; charset=gbk"%>
<%@ page import="dhmtxl.db.MobileDb"%>
<%@ page import="dhmtxl.entity.MobileBrand"%>
<%@page import="java.util.*"%>
 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">

源代码网整理以下<title></title>
<link href="/console/common/css/common.css" rel="stylesheet"
 type="text/css">
<script language="javascript">
function find()
{
 document.frm.action="mobileBrandMgr.jsp";
    document.frm.submit();
}
function addItem()
{
 document.frm.action="mobileBrandMgr.jsp?action=add";
    document.frm.submit();
}
function editItem(index,itemId)
{   
    var areaName=document.all.mobileBrandList.rows[index].cells(0).innerText;
    var brandName=document.all.mobileBrandList.rows[index].cells(1).innerText;
    var mobile=document.all.mobileBrandList.rows[index].cells(2).innerText;
    var newOperateStr ="<input name=’S2’ type=’button’ class=’button’ value=’确定’ onClick= javascript:updateData(’"+index+"’,’"+itemId+"’);>";


    document.all.mobileBrandList.rows[index].cells(0).innerHTML= getAreaOptions(areaName);
 document.all.mobileBrandList.rows[index].cells(1).innerHTML= getBrandOptions(brandName);
 document.all.mobileBrandList.rows[index].cells(3).innerHTML= newOperateStr;
}
function updateData(index,itemId){
 document.frm.action="mobileBrandMgr.jsp?action=update&itemId="+itemId;
    document.frm.submit();
}
function resetData(index,areaName,brandName,mobile,itemId){
var operateStr="";
    document.all.mobileBrandList.rows[index].cells(0).innerHTML= areaName;
 document.all.mobileBrandList.rows[index].cells(1).innerHTML= brandName;
 document.all.mobileBrandList.rows[index].cells(2).innerHTML= mobile;
 document.all.mobileBrandList.rows[index].cells(3).innerHTML= operateStr;
}
function deleteItem(id)
{
 document.frm.action="mobileBrandMgr.jsp?action=delete&id="+id;
    document.frm.submit();
}
function deleteBatch()
{
 document.frm.action="mobileBrandMgr.jsp?action=deleteBatch";
    document.frm.submit();
}
function insertBatch()

源代码网整理以下
{
 document.frm.action="mobileBrandMgr.jsp?action=insertBatch";
    document.frm.submit();
}
function getBrandOptions(brandName){
var brands=new Array(7);
var optionStr="<select name=’update_brandId’>";
brands[0]="全球通";
brands[1]="移动公话";
brands[2]="顺风卡";
brands[3]="新顺风卡";
brands[4]="神州行";
brands[5]="大众卡";
brands[6]="动感地带";
for(var i=0;i<brands.length;i++){
  if(brandName.indexOf(brands[i])!=-1)
   optionStr+="<option value=’"+i+"’ selected>"+brands[i]+"</option>";
  else
    optionStr+="<option value=’"+i+"’>"+brands[i]+"</option>";
}
optionStr+="</select>";
return optionStr;
}
function getAreaOptions(areaName){


var optionStr="<select name=’update_areaId’>";
var areas = new Array(5)
areas[0] = "禅城区";
areas[1] = "三水区";
areas[2] = "顺德区";
areas[3] = "南海区";
areas[4] = "高明区";
for(var i=0;i<areas.length;i++){
  if(areaName.indexOf(areas[i])!=-1)
   optionStr+="<option value=’"+i+"’ selected>"+areas[i]+"</option>";
  else
    optionStr+="<option value=’"+i+"’>"+areas[i]+"</option>";
}
optionStr+="</select>";
return optionStr;
}
 
</script>
</head>
<%
 MobileDb mobileDb = new MobileDb();
 MobileBrand mobileBrand = null;
 String action = request.getParameter("action");
 action = action == null ? "" : action;


 if (action.equals("add")) {
  String add_areaId = request.getParameter("add_areaId");
  String add_brandId = request.getParameter("add_brandId");
  String add_mobile = request.getParameter("add_mobile");
  mobileBrand = new MobileBrand();
  mobileBrand.areaId = Integer.parseInt(add_areaId);
  mobileBrand.brandId = Integer.parseInt(add_brandId);
  mobileBrand.mobile = add_mobile;
  mobileDb.insert(mobileBrand);
 }
 //获取查询参数
 String areaId = request.getParameter("areaId");
 areaId = areaId == null ? "" : areaId;
String brandId = request.getParameter("brandId");
 brandId = brandId == null ? "" : brandId;
 String mobile = request.getParameter("mobile");
 mobile = mobile == null ? "" : mobile;
 String term = "";
 String strParm = "";


 //组合查询条件
 if (!areaId.equals("")) {
  term += " and area_id=" + areaId;
 }
 if (!brandId.equals("")) {
  term += " and brand_id=" + brandId;
 }
 if (!mobile.equals("")) {
  term += " and mobile like ’" + mobile + "%’";
 }
 strParm += "&areaId=" + areaId;
 strParm += "&brandId=" + brandId;
 strParm += "&mobile=" + mobile;
 //////////////分页处理/////////////
 ServletContext context = getServletContext();
 int pageSize = 10;
 String pageSizeStr = context.getInitParameter("pageSize");
 if (pageSizeStr != null) {
  pageSize = Integer.parseInt(pageSizeStr);
 }
 int pageNum = 1;
 String pageStr = request.getParameter("pageNum");
 if (pageStr != null) {


  pageNum = Integer.parseInt(pageStr);
 }
 int startRow = (pageNum - 1) * pageSize + 1;
 int endRow = (pageNum) * pageSize;
 int rowCount = 0;
 int i = startRow - 1;
 if (action.equals("delete")) {
  String id = request.getParameter("id");
  id = id == null ? "0" : id;
  mobileDb.deleteById(Integer.parseInt(id));
  response.sendRedirect("mobileBrandMgr.jsp?pageNum=" + pageNum
  + strParm);
 
 
 }
 if (action.equals("update")) {
  String itemId = request.getParameter("itemId");
  String update_brandId = request.getParameter("update_brandId");
  String update_areaId = request.getParameter("update_areaId");
mobileDb.update(Integer.parseInt(itemId),Integer.parseInt(update_brandId),Integer.parseInt(update_areaId));
        response.sendRedirect("mobileBrandMgr.jsp?pageNum=" + pageNum+ strParm);
 }
 
 
 if (action.equals("deleteBatch")) {
  String delete_brandId = request.getParameter("delete_brandId");
  String delete_areaId = request.getParameter("delete_areaId");
  mobileDb.deleteByareaIdAndBrandId(Integer.parseInt(delete_areaId),Integer.parseInt(delete_brandId));
        response.sendRedirect("mobileBrandMgr.jsp?pageNum=" + pageNum+ strParm);
 }
 
 if (action.equals("insertBatch")) {
  String insertBatch_areaId=request.getParameter("insertBatch_areaId");
  String insertBatch_brandId=request.getParameter("insertBatch_brandId");
  String mobiles=request.getParameter("mobiles");
  String mobileArray[]=mobiles.trim().split(",");
  for(int b=0;b<mobileArray.length;b++){
   mobileBrand=new MobileBrand();
   mobileBrand.areaId=Integer.parseInt(insertBatch_areaId); 软件开发网 www.mscto.com
   mobileBrand.brandId=Integer.parseInt(insertBatch_brandId);
   mobileBrand.mobile=mobileArray[b].trim();
   mobileDb.insert(mobileBrand);
   }
    response.sendRedirect("mobileBrandMgr.jsp?pageNum=" + pageNum+ strParm);
 }
 
 //查询操作
 String areas[] = { "禅城区", "三水区", "顺德区", "南海区", "高明区" };
 String brands[] = { "全球通", "移动公话", "顺风卡", "新顺风卡", "神州行", "大众卡",
   "动感地带" };
 Collection col = mobileDb.findByTerm(term, startRow, endRow);
 rowCount = mobileDb.findByTerm(term, 1, -1).size();
 int pageCount = rowCount % pageSize == 0 ? rowCount / pageSize
   : (rowCount / pageSize + 1);
%>
<body leftmargin="4" topmargin="4">
<form name="frm" method="post" action=""><!-- 搜索条件 -->

<table width="100%" border="0" cellspacing="0" cellpadding="0"
class="td" bordercolordark=white align="center" height="30">
 <tr>
  <td colspan="6" align="center" valign="middle">
  <div align="left"><img
   src="/console/common/button/zhuangshi1.jpg" width="14" height="14"
   hspace="10" align="absmiddle"><strong>号码列表管理</strong></div>
  </td>
 </tr>
</table>
<table width="100%">
 <tr>
  <td height="30">地区: <select name="areaId">
   <option value="">全部</option>
   <option value="1" <%=areaId.equals("1")?"selected":""%>>禅城区</option>

   <option value="2" <%=areaId.equals("2")?"selected":""%>>三水区</option>
   <option value="3" <%=areaId.equals("3")?"selected":""%>>顺德区</option>
   <option value="4" <%=areaId.equals("4")?"selected":""%>>南海区</option>
   <option value="5" <%=areaId.equals("5")?"selected":""%>>高明区</option>
  </select> 品牌: <select name="brandId">
   <option value="">全部</option>
   <option value="1" <%=brandId.equals("1")?"selected":""%>>全球通</option>
   <option value="2" <%=brandId.equals("2")?"selected":""%>>移动公话</option>


   <option value="3" <%=brandId.equals("3")?"selected":""%>>顺风卡</option>
   <option value="4" <%=brandId.equals("4")?"selected":""%>>新顺风卡</option>
   <option value="5" <%=brandId.equals("5")?"selected":""%>>神州行</option>
   <option value="6" <%=brandId.equals("6")?"selected":""%>>大众卡</option>
   <option value="7" <%=brandId.equals("7")?"selected":""%>>动感地带</option>
  </select> 号码: <input name="mobile" type="text" class="input"
   value="<%=mobile%>" size="16"> <img
   src="/console/common/button/search_p.jpg" alt="查找用户" width="51"


  height="23" border="0" align="absmiddle" onclick="javascript:find();"></td>
 </tr>
</table>
<br>
<br>
<!-- 号码段信息列表 -->
<table width="100%" border="0" cellspacing="1" cellpadding="0"
 class="table" bordercolordark=white align="center" id="mobileBrandList">
 <tr>
  <td width="15%" align="center" valign="middle" class="td_title">区域</td>
  <td width="15%" align="center" valign="middle" class="td_title">品牌</td>
  <td width="10%" align="center" valign="middle" class="td_title">号码</td>
  <td width="10%" align="center" valign="middle" class="td_title">修改</td>

 </tr>
 
 <%
  if(col.size()>0){
  Iterator it = col.iterator();
  mobileBrand = new MobileBrand();
  int rowIndex = 0;
  while (it.hasNext()) {
   mobileBrand = (MobileBrand) it.next();
   rowIndex++;
 %>
 <tr class="<%=(i%2 == 1)?"td1":"td2"%>"
  onmousemove="javascript:finout(true)"
  onMouseOut="javascript:finout(false)" id="<%=mobileBrand.id %>">
  <td align="center" valign="middle" height="7"><%=areas[mobileBrand.areaId - 1]%>
  </td>
  <td align="center" valign="middle" height="7"><%=brands[mobileBrand.brandId - 1]%>
  </td>
  <td align="center" valign="middle" height="7"><%=mobileBrand.mobile%>
  </td>
  <td align="center" valign="middle" height="7"><input
   name="Submit2" type="button" class="button" value="删除"
   onClick="javascript:deleteItem(’<%=mobileBrand.id %>’);"> <input
   name="Submit2" type="button" class="button" value="编辑"
   onClick="javascript:editItem(’<%=rowIndex %>’,’<%=mobileBrand.id %>’);">
  </td>
 </tr>
 <%
 }
 }

源代码网整理以下 else{
 %>
 <tr><td colspan="4">对不起!该查询条件下没有数据!</td></tr>
 <%
 }
  %>
</table>
<table border="0" cellspacing="0" cellpadding="0" align="center"
 width="100%">
 <tr>
  <td height="24">
  <div align="right" class="td">第(<%=pageNum%>/<%=pageCount == 0 ? 1 : pageCount%>)页,共
  (<%=rowCount%>) 条 <%
  if (pageNum > 1) {
  %> <a href="mobileBrandMgr.jsp?pageNum=1<%=strParm%>">首页</a> <a
   href="mobileBrandMgr.jsp?pageNum=<%=(pageNum>1?pageNum-1:1)%><%=strParm%>">上页</a>
  <%
  }
  %> <%
 if (pageNum < pageCount) {
 %> <a
   href="mobileBrandMgr.jsp?pageNum=<%=(pageNum<pageCount?pageNum+1:pageCount)%><%=strParm%>">下页</a>


  <a href="mobileBrandMgr.jsp?pageNum=<%=pageCount%><%=strParm%>">末页</a>
  </div>
  <%
  }
  %>
  </td>
 </tr>
</table>
<br>
<br>
<!-- 增加操作 -->
<table width="100%" border="0" cellspacing="0" cellpadding="0"
 class="td" bordercolordark=white align="center" height="30">
 <tr>
  <td colspan="6" align="center" valign="middle">
  <div align="left"><img
   src="/console/common/button/zhuangshi1.jpg" width="14" height="14"
   hspace="10" align="absmiddle"><strong>增加号段信息</strong></div>
  </td>
 </tr>
</table>
<table width="100%">

 <tr>
  <td height="30">地区: <select name="add_areaId">
   <option value="1">禅城区</option>
   <option value="2">三水区</option>
   <option value="3">顺德区</option>
   <option value="4">南海区</option>
   <option value="5">高明区</option>
  </select> 品牌: <select name="add_brandId">
 <option value="1">全球通</option>
   <option value="2">移动公话</option>
   <option value="3">顺风卡</option>
   <option value="4">新顺风卡</option>
   <option value="5">神州行</option>
   <option value="6">大众卡</option>
   <option value="7">动感地带</option>
软件开发网 www.mscto.com

  </select> 号码: <input name="add_mobile" type="text" class="input" value=""
   size="16"> <input name="Submit2" type="button" class="button"
   value="添 加" onClick="addItem()"></td>
 </tr>
</table>
<br>
<br>
<!-- 批量删除 -->
<table width="100%" border="0" cellspacing="0" cellpadding="0"
 class="td" bordercolordark=white align="center" height="30">
 <tr>
  <td colspan="6" align="center" valign="middle">
  <div align="left"><img
   src="/console/common/button/zhuangshi1.jpg" width="14" height="14"
   hspace="10" align="absmiddle"><strong>批量删除号段信息</strong></div>
  </td>
 </tr>
</table>
<table width="100%">
 <tr>
  <td height="30">地区: <select name="delete_areaId">
  <option value="0">所有地区</option>
   <option value="1">禅城区</option>
   <option value="2">三水区</option>
   <option value="3">顺德区</option>
   <option value="4">南海区</option>
   <option value="5">高明区</option>
  </select> 品牌: <select name="delete_brandId">
   <option value="0">所有名牌</option>
   <option value="1">全球通</option>
   <option value="2">移动公话</option>
   <option value="3">顺风卡</option>
   <option value="4">新顺风卡</option>
   <option value="5">神州行</option>
 <option value="6">大众卡</option>
   <option value="7">动感地带</option>
  </select> <input name="Submit2" type="button" class="button" value="删 除"
   onClick="deleteBatch()"></td>
 </tr>
</table>
<br>
<br>
<!-- 批量增加 -->
<table width="100%" border="0" cellspacing="0" cellpadding="0"
 class="td" bordercolordark=white align="center" height="30">
 <tr>
  <td colspan="6" align="center" valign="middle">
  <div align="left"><img
   src="/console/common/button/zhuangshi1.jpg" width="14" height="14"
   hspace="10" align="absmiddle"><strong>批量增加号段信息</strong></div>
  </td>
 </tr>
</table>
<table width="100%" height="32" border="0" cellpadding="0"
 cellspacing="0" id="beforeUpload">
 <tr>
  <td>区域: <select name="insertBatch_areaId">
   <option value="1">禅城区</option>
   <option value="2">三水区</option>
   <option value="3">顺德区</option>
   <option value="4">南海区</option>
   <option value="5">高明区</option>
  </select> 品牌: <select name="insertBatch_brandId">
   <option value="1">全球通</option>
   <option value="2">移动公话</option>
   <option value="3">顺风卡</option>
   <option value="4">新顺风卡</option>
   <option value="5">神州行</option>
   <option value="6">大众卡</option>
   <option value="7">动感地带</option>
  </select></td>
 </tr>
 <tr>
  <td><textarea name="mobiles" style="font-size: 9pt" cols="80"
   rows="8" type="_moz">
      </textarea></td>
 </tr>
 <tr>
  <td><input name="Submit2" type="button" class="button" value="添加"
 onclick="insertBatch()"></td>
 </tr>
</table>
</form>
<script language="javascript" src="/console/common/js/trbg.js"></script>
</body>
</html>


源代码网推荐

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华