初学者,在JSP文件实现表格分页,按下“上一页”“下一页”等按钮之后无法调用go()方法,无法换页,求助!!!
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>小舒同学</title>
<script language="javascript">
function go(num){
System.out.println("****************go了****************");
document.getElementById("cp").value = num ;
document.spform.submit() ; // 表单提交
}
</script>
</head>
<body>
<%
request.setCharacterEncoding("GBK"); //解决中文乱码
%>
<%!
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver"; //定义数据库驱动程序
public static final String DBURL = "jdbc:mysql://localhost:3306/mldn"; //定义数据库连接地址
public static final String DBUSER = "root"; //数据库连接用户名
public static final String DBPASS = "mysqladmin"; //数据库连接密码
%>
<%
Connection conn = null; //声明数据库连接对象
PreparedStatement pstmt = null; //声明数据库操作
ResultSet rs = null; //声明数据库结果集
%>
<%
int currentPage = 1 ; // 为当前所在的页,默认在第1页
int allRecorders = 0 ; // 表示全部的记录数
int pageSize = 1 ; // 表示全部的页数(尾页)
%>
<%
try{
String aaa = request.getParameter("cp");
if(aaa==null||"".equals(aaa)) {
System.out.println("**********空的*********");
currentPage = 1;
} else {
currentPage = Integer.parseInt(aaa) ;
}
} catch(Exception e) {
System.out.println("****************错了****************");
}
%>
<% //sql操作会抛出异常,这里用try。。。catch处理
try {
String sql = "SELECT COUNT(empno) FROM emp"; //查询语句
Class.forName(DBDRIVER); //数据库驱动加载
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS); //取得数据库连接
pstmt = conn.prepareStatement(sql); //实例化数据库操作对象
rs = pstmt.executeQuery(); //执行查询
if(rs.next()){ // 取得全部的记录数
allRecorders = rs.getInt(1) ;
}
%>
<center>
<h1>雇员列表<h1>
<hr>
<%
pageSize = allRecorders/8+1 ;
%>
<%
sql = "SELECT empno,ename,job,sal,hiredate FROM EMP LIMIT ?,8" ;
pstmt = conn.prepareStatement(sql) ;
pstmt.setInt(1,(currentPage-1) * 8) ;
rs = pstmt.executeQuery() ;
%>
<form name="spform" action="#" method="post">
<input type="button" value="首页" onclick="go(1)" <%=currentPage==1?"DISABLED":""%>>
<input type="button" value="上一页" onclick="go(<%=currentPage-1%>)" <%=currentPage==1?"DISABLED":""%>>
<input type="button" value="下一页" onclick="go(<%=currentPage+1%>)" <%=currentPage==pageSize?"DISABLED":""%>>
<input type="button" value="尾页" onclick="go(<%=pageSize%>)" <%=currentPage==pageSize?"DISABLED":""%>>
<input type="hidden" name="cp" value="1">
</form>
<table border="1" width="60%">
<tr>
<td>雇员编号</td>
<td>雇员姓名</td>
<td>雇员工作</td>
<td>雇员工资</td>
<td>雇员日期</td>
</tr>
<%
while(rs.next()) {
int empno = rs.getInt(1); //取得编号
String ename = rs.getString(2); //取得姓名
String job = rs.getString(3); //取得工作
float sal = rs.getFloat(4); //取得工资
java.util.Date data = rs.getDate(5); //取得雇佣日期
%>
<tr>
<td><%=empno%></td>
<td><%=ename%></td>
<td><%=job%></td>
<td><%=sal%></td>
<td><%=data%></td>
</tr>
<%
}
%>
</table>
<%
} catch(Exception e) {
e.printStackTrace();
}
finally{
rs.close();
pstmt.close();
conn.close();
}
%>
</body>
</html>