现将测试代码奉上,尽管我尽量限制代码的长度
先说下开发步骤;DB脚本,测试获得的数据是否正确的显示在一个table中,添加那些该显示那些不该显示,添加了首叶上叶下叶尾叶的功能,添加当前位置标签比如4/5,添加直接跳转到某页的功能,处理掉一些bug。。
1。先写数据库端测试脚本:
--删除表
drop table person;
--建立表
create table person (
id int auto_increment not null Prima(最完善的虚拟主机管理系统)ry key ,
uid varchar(32),
name varchar(32),
password varchar(20)
);
--插入数据?不支持中文,必须指定表内投影,插入多于30条记录用于测试分页
insert into person(uid,name,password) values(uid1,testname,123456);
insert into person(uid,name,password) values(uid1,testname,123456);
insert into person(uid,name,password) values(uid1,testname,123456);
insert into person(uid,name,password) values(uid1,testname,123456);
insert into person(uid,name,password) values(uid1,testname,123456);
insert into person(uid,name,password) values(uid1,testname,123456);
insert into person(uid,name,password) values(uid1,testname,123456);
insert into person(uid,name,password) values(uid1,testname,123456);
insert into person(uid,name,password) values(uid1,testname,123456);
--事务提交
2。整个程序部分。
<%@ 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="首页" onclick="openPage(1);" <%=currentPage==1?"disabled":""%> />