JSP分页

来源:互联网 发布:js取字符串前十位 编辑:程序博客网 时间:2024/05/28 16:04
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="com.db.*" %>
<%@ page import="java.sql.*" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/main.css">

<script type="text/javascript">
//修改
function modifyInfo(theId, objName)
{
var iTop = (window.screen.height-500)/2;
            var iLeft = (window.screen.width-760)/2;
try{
window.open("bjModify.jsp?theId="+theId+"&objName="+objName, "部件信息修改", "width=760,height=520,top="+iTop+",left="+iLeft+",Resizeable=no,Toolbar=no,Location=no,Direction=no,");
}catch(e)
{
alert(e);
}
}

//删除
function deleteInfo(theId, objName)
{
if(confirm("确认删除?"))
{
var iTop = (window.screen.height-500)/2;
          var iLeft = (window.screen.width-760)/2;
try{
window.open("bjDeleteServer.jsp?theId="+theId+"&objName="+objName, "部件信息删除", "width=760,height=520,top="+iTop+",left="+iLeft+",Resizeable=no,Toolbar=no,Location=no,Direction=no,");
}catch(e)
{
alert(e);
}
}
}
</script>


  </head>
  
   <body>
    <%
    //获取查询参数
  String bjName = request.getParameter("bjName");
  String DeptName1 = request.getParameter("DeptName1");
  String DeptName2 = request.getParameter("DeptName2");
  String DeptName3 = request.getParameter("DeptName3");
  String bjState = request.getParameter("bjState");
  //组织sql语句
String sql = "select * from YC_0120_景观灯 where 1=1 ";
if(bjName!=null && !bjName.equals(""))
{
bjName = new String(bjName.getBytes("ISO8859_1"), "gb2312");
sql += " and ObjName='"+bjName+"'";
}
if(DeptName1!=null && !DeptName1.equals(""))
{
DeptName1 = new String(DeptName1.getBytes("ISO8859_1"), "gb2312");
sql += " and DeptName1='"+DeptName1+"'";
}
if(DeptName2!=null && !DeptName2.equals(""))
{
DeptName2 = new String(DeptName2.getBytes("ISO8859_1"), "gb2312");
sql += " and DeptName2='"+DeptName2+"'";
}
if(DeptName3!=null && !DeptName3.equals(""))
{
DeptName3 = new String(DeptName3.getBytes("ISO8859_1"), "gb2312");
sql += " and DeptName3='"+DeptName3+"'";
}
if(bjState!=null && !bjState.equals(""))
{
bjState = new String(bjState.getBytes("ISO8859_1"), "gb2312");
sql += " and ObjState='"+bjState+"'";
}

System.out.println("查询的sql语句为:"+sql);
    
   int i = 1;
   int count = 0;  //总信息数
   int totalPageCount = 0; //总页数
   int perPageCount = 26; //每页显示信息数
   int currentPage = 1;//当前页数
   String pageId = request.getParameter("page");
   if(pageId != null)
   {
    try{
    currentPage = Integer.parseInt(pageId);
    }catch(Exception e)
    {
    System.out.println("分页出错");
    }
   }
   AccessDB adb = new AccessDB();
   ResultSet rs = adb.executeQuery(sql);
   rs.last();
   count = rs.getRow();
   if(count % perPageCount == 0)
   {
    totalPageCount = count/perPageCount;
   }
   else{
    totalPageCount = count/perPageCount + 1;
   }
   int currentIndex = (currentPage - 1)*perPageCount + 1;//当前页显示的第一条记录
   if(count > 0)
   {
    rs.absolute(currentIndex);
   }
   
   String bjmc = rs.getString("ObjName");
String zgbm = rs.getString("DeptName1");
String qsdw = rs.getString("DeptName2");
String yhdw = rs.getString("DeptName3");
String dywg = rs.getString("BGCode");
String zt = rs.getString("ObjState");
String xsx = rs.getString("ObjUseState");
String cssj = rs.getString("ORDate");
if(cssj !=null && cssj.length()>10){
cssj = cssj.substring(0, 10);
}
String bgsj = rs.getString("CHDate");
if(bgsj !=null && bgsj.length()>10){
bgsj = bgsj.substring(0, 10);
}
String sjly = rs.getString("DataSource");

//获取ID
int theId = rs.getInt("objectId");
    %>
    
    <body>
   <table cellspacing="1" cellpadding="0" bgcolor="#cccccc" width="100%" border="0">
<tr height="30">
<td bgcolor="#f7f7f7" align="center"><strong>序号</strong></td>
<td bgcolor="#f7f7f7" align="center"><strong>部件名称</strong></td>
<td bgcolor="#f7f7f7" align="center"><strong>主管部门</strong></td>
<td bgcolor="#f7f7f7" align="center"><strong>权属单位</strong></td>
<td bgcolor="#f7f7f7" align="center"><strong>养护单位</strong></td>
<td bgcolor="#f7f7f7" align="center"><strong>单元网格</strong></td>
<td bgcolor="#f7f7f7" align="center"><strong>状态</strong></td>
<td bgcolor="#f7f7f7" align="center"><strong>现势性</strong></td>
<td bgcolor="#f7f7f7" align="center"><strong>初始时间</strong></td>
<td bgcolor="#f7f7f7" align="center"><strong>变更时间</strong></td>
<td bgcolor="#f7f7f7" align="center"><strong>数据来源</strong></td>
<td bgcolor="#f7f7f7" align="center"><strong>操作</strong></td>
</tr>
<%
while(rs.next())
{
%>
<tr align = "center">
<td bgcolor="#ffffff" align="center" align="center"><%=i %></td>
<td bgcolor="#ffffff" align="center" align="center"><%=bjmc %></td>
<td bgcolor="#ffffff" align="center" align="center"><%=zgbm %></td>
<td bgcolor="#ffffff" align="center" align="center"><%=qsdw %></td>
<td bgcolor="#ffffff" align="center" align="center"><%=yhdw %></td>
<td bgcolor="#ffffff" align="center" align="center"><%=dywg %></td>
<td bgcolor="#ffffff" align="center" align="center"><%=zt %></td>
<td bgcolor="#ffffff" align="center" align="center"><%=xsx %></td>
<td bgcolor="#ffffff" align="center" align="center"><%=cssj %></td>
<td bgcolor="#ffffff" align="center" align="center"><%=bgsj %></td>
<td bgcolor="#ffffff" align="center" align="center"><%=sjly %></td>
<td bgcolor="#ffffff" align="center" align="center">
<a href="javascript:void(0);" onclick="modifyInfo('<%=theId %>','<%=bjmc %>');" >修改</a>
<a href="javascript:void(0);" onclick="deleteInfo('<%=theId %>','<%=bjmc %>');" >删除</a>
</td>
<%
i++;
if(i > perPageCount-1) break;
}
%>
</tr>
  </table>
 
<table width="90%" border=0>
<tr align="right" valign="top">
<td colspan ="4" align = "right">
<%
String firstLink,lastLink,preLink,nextLink;
firstLink ="bjResult.jsp?page=1";
lastLink ="bjResult.jsp?page="+totalPageCount;
if(currentPage>1)
{
  preLink = "bjResult.jsp?page="+(currentPage-1);
}
else
{
  preLink = firstLink;
}
if(currentPage<=totalPageCount-1)
{
  nextLink ="bjResult.jsp?page="+(currentPage+1);
}
else
{
  nextLink = lastLink;
}
%>
<form method ="post" action="bjResult.jsp">
  <p align = "center">
  共<font color="#0000FF"><%=count%></font>条
  第<font color="#0000FF"><%=currentPage%></font>页/共
  <font color="#0000FF"><%=totalPageCount%></font>页|
  <a href=<%=firstLink%>>首页</a> |
  <a href=<%=preLink%>>上一页</a> |
  <a href=<%=nextLink%>>下一页</a>
  <a href=<%=lastLink%>>尾页</a> |
  转到<input type "text" name = page size = 2><input type="submit" name=submit value=GO>
</p>
  </form>
  </td>
  </tr>
 </table>
  </body>
</html>