[jsp]代码库
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="java.sql.*"%>
<html>
<head>
<title>jsp(SUN企业级应用的首选)通过程序来控制分页</title>
<script type="text/javascript">
function openPage(currentpage) {
document.getElementById("cp").value = currentpage;
document.getElementById("frm").submit();
}
function changePage() {
document.getElementById("cp").value = document.getElementById("selNumber").value ;
document.getElementById("frm").submit();
}
</script>
</head>
<body>
<%
int pageSize = 10 ;
int currentPage = 1 ;
int allRecorder = 0 ;
int pageNumber ;
try {
currentPage = Integer.parseInt(request.getParameter("cp")) ;
} catch(Exception ex ) {
}
//关于数据库连接部分的处理根据自己的数据库来指定
final String DBDriver = "org.gjt.mm.MySQL(和PHP搭配之最佳组合).Driver";
final String DbUrl = "jdbc:MySQL(和PHP搭配之最佳组合)://localhost/test";
final String DBUser = "root";
final String DBPSW = "root";
Connection conn = null;
try {
Class.forName(DBDriver);
conn = DriverManager.getConnection(DbUrl,DBUser,DBPSW);
String sql = "select count(id) from person";
PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
if(rs.next()) {
allRecorder = rs.getInt(1);
}
pageNumber = (allRecorder+pageSize-1)/pageSize ; //得到尾页数
rs.close();
pst.close();
sql = "select id,uid,name,password from person";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
%>
<form action="test.jsp(SUN企业级应用的首选)" method="get" id="frm">
<input type="button" value="首页" <%=currentPage==1?"disabled":""%> />
<input type="button" value="上页" <%=currentPage==1?"disabled":""%>/>
<input type="button" value="下页" <%=currentPage==pageNumber?"disabled":""%>/>
<input type="button" value="尾页" <%=currentPage==pageNumber?"disabled":""%>/>
<font color="red"><%=currentPage%>/<%=pageNumber%></font>
<input type="hidden" value="" name="cp">
<select id="selNumber" onchange="changePage();">
<%
for(int temp=1;temp<=pageNumber;temp++) {
%>
<option value="<%=temp%>" <%=currentPage==temp?"selected":""%> ><%=temp%></option>
<%
}
%>
</select>
</form>
<table border=1 width=80%>
<tr>
<td>编号</td>
<td>登陆名称</td>
<td>姓名</td>
<td>密码</td>
<td colspan=2>操作</td>
</tr>
<%
for(int temp = 0;temp<pageSize*(currentPage-1);temp++) {
rs.next(); //需要空出去部分代码不显示
}
boolean hasData = false;
for(int temp=0 ;temp<pageSize;temp++) {
if(rs.next()) {
hasData = true ;
%>
<tr>
<td><%=rs.getInt(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td >修改</td>
<td >删除</td>
</tr>
<%
}
}
if(!hasData) {%>
<tr><td colspan=6> 数据不存在</td></tr>
<%
}
%>
</table>
<%
rs.close();
pst.close();
conn.close();
}catch(Exception ex) {
out.print("数据错误");
}
%>
</body>
</html>