|
3Java金币
代码看着有些乱 因为刚学习 没用MVC 忘大大们见谅我这个 代码 选择页数和每页显示多少数据 好使, 但是
点击首页,上一页,下一页,尾页 就挂了!! 望解答 3Q
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
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>My JSP 'index.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 rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<script type="text/javascript">
function go(num){
document.getElementById("cp").value = num;
document.spform.submit(); //表单提交
}
</script>
<%!
private static final String URL = "list.jsp";
%>
<%!
private static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
private static final String DBURL = "jdbc:mysql://localhost:3306/t_dept";
private static final String DBUSER = "root";
private static final String DBPASS = "root";
%>
<%
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
%>
<%
int currentPage = 1; //表示当前所在页,默认1页
int lineSize = 5; //每页显示的记录数
int allRecorders = 0; //表示全部的记录数
int pageSize = 1; //表示全部的页数(尾页)
int lsData[] = {5,10,15,20,30,50,100};
%>
<%
try{
currentPage = Integer.parseInt(request.getParameter("selcp"));
} catch (Exception e){}
try{
lineSize = Integer.parseInt(request.getParameter("ls"));
} catch (Exception e){}
%>
<%
Class.forName(DBDRIVER);
conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
String sql = "SELECT COUNT(empno) FROM dept";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
if (rs.next()){ //取得全部记录
allRecorders = rs.getInt(1);
}
%>
<center>
<h1>雇员列表</h1>
<%
pageSize = (allRecorders + lineSize - 1) / lineSize; //计算出共分多少页
%>
<%
sql = "SELECT * from dept LIMIT ?,?";
ps = conn.prepareStatement(sql);
ps.setInt(1, (currentPage-1) * lineSize);
ps.setInt(2,lineSize);
rs = ps.executeQuery();
%>
<form name="spform" action=<%=URL %> method="post">
<input type="button" value="首页" <%=currentPage==1?"DISABLED":"" %>/>
<input type="button" value="上一页" <%=currentPage==1?"DISABLED":"" %>/>
<%=currentPage %>
<input type="button" value="下一页" <%=currentPage==pageSize?"DISABLED":"" %>>
<input type="button" value="尾页" <%=currentPage==pageSize?"DISABLED":"" %>/>
跳转到<select name="selcp">
<%
for (int i=1; i<pageSize; i++){
%>
<option value="<%= i%>"<%=i==currentPage?"SELECTED":""%>><%=i %></option>
<%
}
%>
</select>页
每页显示<select name="ls">
<%
for (int i=0; i<lsData.length;++i){
%>
<option value="<%=lsData[i] %>"<%=lsData[i]==lineSize?"SELECTED":"" %>><%=lsData[i] %></option>
<%
}
%>
</select>条
<input type="hidden" id="cp" value="1" />
</form>
<table border="1" width="80%">
<tr>
<th>编号</th>
<th>姓名</th>
<th>职位</th>
<th>雇员日期</th>
<th>工资</th>
<th>奖金</th>
</tr>
<%
while (rs.next()){
int empno = rs.getInt(1);
String ename = rs.getString(2);
String job = rs.getString(3);
String hiredate = rs.getString(4);
double sal = rs.getDouble(5);
double comm = rs.getDouble(6);
%>
<tr>
<td><%=empno %></td>
<td><%=ename%></td>
<td><%=job %></td>
<td><%=hiredate %></td>
<td><%=sal %></td>
<td><%=comm %></td>
</tr>
<%
}
%>
</table>
<%
conn.close();
%>
</center>
</body>
</html>
|
|