您的位置:首页 > Web前端 > JavaScript

基于书本jsp分页技术,修改的数组分页

2014-12-10 15:36 218 查看
1. 数据库部分

create database student;
create table info;
CREATE TABLE `student`.`info` (
`studentNumber` INT NOT NULL,
`studentName` VARCHAR(45) NOT NULL,
`studentSex` VARCHAR(45) NOT NULL,
`studentAge` INT NOT NULL,
`studentWeight` INT NOT NULL,
PRIMARY KEY (`studentNumber`));
INSERT INTO `student`.`info` (`studentNumber`, `studentName`, `studentSex`, `studentAge`, `studentWeight`) VALUES ('201301001', '张一', '男', '18', '50');
INSERT INTO `student`.`info` (`studentNumber`, `studentName`, `studentSex`, `studentAge`, `studentWeight`) VALUES ('201301002', '张二', '女', '17', '51');
INSERT INTO `student`.`info` (`studentNumber`, `studentName`, `studentSex`, `studentAge`, `studentWeight`) VALUES ('201301003', '张三', '男', '19', '52');
INSERT INTO `student`.`info` (`studentNumber`, `studentName`, `studentSex`, `studentAge`, `studentWeight`) VALUES ('201301004', '张四', '女', '20', '53');
INSERT INTO `student`.`info` (`studentNumber`, `studentName`, `studentSex`, `studentAge`, `studentWeight`) VALUES ('201301005', '张五', '男', '18', '54');
INSERT INTO `student`.`info` (`studentNumber`, `studentName`, `studentSex`, `studentAge`, `studentWeight`) VALUES ('201301006', '张六', '女', '16', '55');
2. 配置mysql数据库文件

加载:lib文件mysql-connection.jar

3. jsp原本页面

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>分页显示实例</title>
<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">
</head>

<body>
<center>
分页显示记录内容
<hr>
<table border="1" bgcolor="cccfff" align="center" >
<tr>
<td>学号</td>
<td>姓名</td>
<td>性别</td>
<td>年龄</td>
<td>体重</td>
</tr>
<%
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root", "12332100");
int intPageSize;	//一页显示的记录总数
int intRowCount;	//记录总数
int intPage;		//总页数
int intPageCount;		//带显示页码
String strPage;
int i;
intPageSize = 2;	//设置一页显示的记录数
//取得带显示的页码
strPage = request.getParameter("page");
if(strPage==null){
//表明在QueryString中没有Page这一个参数,此时显示第一页数据
intPage = 1;
} else {
intPage=java.lang.Integer.parseInt(strPage);
if(intPage<1)
intPage = 1;
}
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql = "select * from info";
ResultSet rs = stmt.executeQuery(sql);
rs.last();
intRowCount = rs.getRow();
intPageCount = (intRowCount+intPageSize-1
4000
)/intPageSize;
if(intPage > intPageCount)
intPage = intPageCount;
if(intPageCount>0) {
rs.absolute((intPage-1)*intPageSize+1);
i=0;
while(i<intPageSize&&!rs.isAfterLast()) {
%>
<tr>
<td><%=rs.getString("studentNumber") %></td>
<td><%=rs.getString("studentName") %></td>
<td><%=rs.getString("studentSex") %></td>
<td><%=rs.getString("studentAge") %></td>
<td><%=rs.getString("studentWeight") %></td>
</tr>
<%
rs.next();
i++;
}
}
%>
</table>
<div align="center">
第<%=intPage %>页 共<%=intPageCount %>页
<%
if(intPage<intPageCount) {
%>
<a href="pageBreak.jsp?page=<%=intPage+1%>">下一页</a>
<%
}
if(intPage>1) {
%>
<a href="pageBreak.jsp?page=<%=intPage-1%>">上一页</a>
<%
}
rs.close();
stmt.close();
conn.close();
%>
</div>
</center>
</body>
</html>


4.修改后数组显示页面

<%@ page language="java"
import="java.util.*,com.it.dao.*,com.it.javabean.*"
pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'nashuiguanli.jsp' starting page</title>

<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 href="../css/reset.css" rel="stylesheet" type="text/css">
<link href="../css/search.css" rel="stylesheet" type="text/css">

</head>

<body>
<div>
<table border="1" cellspacing="0" cellpadding="0" width="99%" style="margin:0 auto">
<tr>
<th width="150px" >公司名称</th>
<th nowrap>公司地址</th>
<th width="65px">公司法人</th>
<th width="100px">法人身份证</th>
<th width="60px">公司财务</th>
<th width="100px">财务身份证</th>
<th width="100px">注册资本(万)</th>
<th width="40px">删除</th>
<th width="40px">修改</th>
</tr>
<tr>
<td align="center" colspan="9" ><a href="altercom.jsp">添加新企业</a></td>
</tr>
<%
ArrayList companylist = (ArrayList) session
.getAttribute("companylist");
if (companylist == null || companylist.size() == 0) {
%>
<div align="center">
<br>仍然未添加公司
</div>
<%
} else {
int intPageSize;	//一页显示的记录总数
int intRowCount;	//记录总数
int intPage;		//带显示页码
int intPageCount;		//总页数
String strPage;
intPageSize = 2;
strPage = request.getParameter("page");
if(strPage==null){
//表明在QueryString中没有Page这一个参数,此时显示第一页数据
intPage = 1;
} else {
intPage=java.lang.Integer.parseInt(strPage);
if(intPage<1)
intPage = 1;
}
intRowCount = companylist.size();
System.out.println(intRowCount);//9
intPageCount = (intRowCount+intPageSize-1)/intPageSize;//计算总的记录数
if(intPage > intPageCount)//带显示页码大于总页码
intPage = intPageCount;
if(intPageCount>0) {//总页码数大于0
//companylist.get((intPage-1)*intPageSize+1);
int i=(intPage-1)*intPageSize;//8
int n=(intPage-1)*intPageSize+2;//10
int m=(intPage-1)*intPageSize-1;//9
//for  (int i=(intPage-1)*intPageSize;i <= companylist.size()-1;i++){//(int i = companylist.size() - 1; i >= 0; i--)
while(i<n&&i<intRowCount) {
Company company = (Company) companylist.get(i);//获取对象,得到对象的数据结构
%>
<tr>
<td><%=company.getComname()%></td>
<td><%=company.getComaddress()%></td>
<td><%=company.getFarenname()%></td>
<td><%=company.getFarenid()%></td>
<td><%=company.getCaiwuname()%></td>
<td><%=company.getCaiwuid()%></td>
<td>5000</td>
<td><a href="../CompanyServlet?action=deleteCompany&comname=<%=company.getComname()%>">删除</a></td>
<td><a href="../CompanyServlet?action=searchOneCompany&comname=<%=company.getComname()%>">修改</a>
</td>
</tr>
<%
i++;
}
}

%>
</table>
<div align = "center">
第 <%=intPage %>页 共 <%=intPageCount %>页
<%
if(intPage<intPageCount) {
%>
<a href = "searchallcom.jsp?page=<%=intPage+1%> " >下一页 </a >
<%
}
if(intPage>1) {
%>
<a href = "searchallcom.jsp?page=<%=intPage-1%> " >上一页 </a >
<%
}
}
%>
</div >

</div>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: