查询分页与CheckBox全选提交

1/5/2008来源:Java教程人气:7707

最近项目需要,因为时间问题我用最简单的方法做了一个查询分页+CheckBox的全选提交,我觉得非常适合小项目和类似邮件列表的功能.下面贴出我的代码,希望朋友们多给我提毛病.
主要代码都写在jsp里.下面是一个apply_note.jsp:
<%@page contentType="text/Html;charset=GBK" language="java"%>
<%@ page import="java.sql.*"%>
<%@ page import="com.deepdo.common.connect.ConSql"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" PRefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<style type=text/CSS>
td,th,p,span,div,a,A {font-size:12px;text-decoration:none}
.blak a {color:#000; text-decoration:none}
.blak a:hover {color:#f00; text-decoration:underline}
</style>
<body>
<jsp:include page="/include/deepdo.jsp" flush="true"/>
<table width="700" border="0" align="center" cellpadding="0" cellspacing="0"  height="450">
  <tr>
 <td valign="top" class="bg_td1" height="450" width="160">
 <jsp:include page="/include/navigation.jsp" flush="true"/>
 </td>
 <td valign="top" class="bg_td1" height="450" width="540">
 <table width="590" border="0" align="center" cellpadding="2" cellspacing="2" bgcolor="#FAF9F5">
          <tr>
            <td background="resume/images/color.jpg" class="font4"> <img src="http://www.QQread.com/java/resume/images/dot.gif">
              工作申请记录</td>
          </tr>
        </table>
        <table width="590" border="0" align="center" cellpadding="4" cellspacing="2">
        <%
  //变量声明
  Connection sqlCon; //数据库连接对象
  Statement sqlStmt; //SQL语句对象
  ResultSet sqlRst; //结果集对象
  String strCon; //数据库连接字符串
  String strSQL; //SQL语句
  int intPageSize; //一页显示的记录数
  int intRowCount; //记录总数
  int intPageCount; //总页数
  int intPage; //待显示页码
  String strPage;
  int i;
  //设置一页显示的记录数
  intPageSize = 10;
  //取得待显示页码
  strPage = request.getParameter("page");
  if(strPage==null){//表明在QueryString中没有page这一个参数,此时显示第一页数据
  intPage = 1;
  }
  else{//将字符串转换成整型
  intPage = java.lang.Integer.parseInt(strPage);
  if(intPage<1) intPage = 1;
  }
  //获取一个TOMCAT配置的连接池
  sqlCon = ConSql.getCon();
  //创建一个可以滚动的只读的SQL语句对象
  sqlStmt = sqlCon.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
  //预备SQL语句
  strSQL = "select * from apply_note where userid="+request.getAttribute("userid");
  //执行SQL语句并获取结果集
  sqlRst = sqlStmt.executeQuery(strSQL);
  //获取记录总数
  sqlRst.last();
  intRowCount = sqlRst.getRow();
  //记算总页数
  intPageCount = (intRowCount+intPageSize-1) / intPageSize;
  //调整待显示的页码
  if(intPage>intPageCount) intPage = intPageCount;
  %>
  <tr>
  <td align="center" bgcolor="#efefef" width="150" height="27">已经申请过的职位</td>
  <td align="center" bgcolor="#efefef" width="140" height="27">公司名称</td>
  <td align="center" bgcolor="#efefef" width="75" height="27">申请时间</td>
  <td align="center" bgcolor="#efefef" width="25" height="27"><input type=CheckBox name="selectAll" onClick="selectAll();"></td>
  </tr>
  <%
  if(intPageCount>0){
  //将记录指针定位到待显示页的第一条记录上
  sqlRst.absolute((intPage-1) * intPageSize + 1);
  //显示数据
  i = 0;
  while(i<intPageSize && !sqlRst.isAfterLast()){
  %>
  <form name ="noteForm" action="noteRemove.do" method="post">
  <tr>
  <td><a href=<%=sqlRst.getString("url")%> target="_blank"><%=sqlRst.getString("job")%></a></td>
  <td><%=sqlRst.getString("unit")%></td>
  <td><%=sqlRst.getString("date_time")%></td>
  <td width="26" align="center"><input type=CheckBox name="id" value=<%=sqlRst.getString("id")%>></td>
  </tr>
  <%
  sqlRst.next();
  i++;
  }
  }
  %>
  <tr>
  <td>
  <a href="note.do?page=1">首页</a>&nbsp;&nbsp;
  <%if(intPage>1){%><a href="note.do?page=<%=intPage-1%>">上一页</a><%}
  else {%><a href="#" title="已经是第一页">上一页</a><%}%>&nbsp;&nbsp;
  <%if(intPage<intPageCount){%><a href="note.do?page=<%=intPage+1%>">下一页</a><%}
  else {%><a href="#" title="已经是最后一页">下一页</a><%}%>&nbsp;&nbsp;
  <a href=note.do?page=<%=intPageCount%>>末页</a>
  </td>
  <td>
  共 <%=intPageCount%> 页申请记录&nbsp;&nbsp;
  当前为第 <%=intPage%> 页&nbsp;&nbsp;
  </td>
  <td>&nbsp;</td>
  <td align="center"><input type="submit" value="删除" onClick="manageSubmit();"></td>
  <input type="hidden" name="checkvalue" value="">
  </tr>
  </form>
  <%
  //关闭结果集
  sqlRst.close();
  //关闭SQL语句对象
  sqlStmt.close();
  //关闭数据库
  sqlCon.close();
  %> 
        </table>
    </td>
  </tr>
</table>
<jsp:include page="/include/tail.html" flush="true"/>
</body>
<script language="Javascript">
function manageSubmit(){
 var ChkEls=document.getElementsByName("Id");
 var checkedIds="";
 for(var i=0;i<ChkEls.length;i++){
  if(ChkEls.item(i).type != "checkbox")continue;
  var oEl = ChkEls.item(i);
  if(oEl.checked) {
   checkedIds = checkedIds + (oEl.value + ",");
  }
 }
 document.noteForm.checkvalue.value = checkedIds;
 if ( checkedIds == "" ) return false;
 return true;
}