<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ page import="java.sql.*"%><html><head>
<title>My JSP 'Jdbc2.jsp' starting page</title>
</head>
<body><center><h1>人员列表</h1><hr /><br /><%!final String jspurl = "Jdbc2.jsp";%><%int linesize = 15;int currentpage = 1;int alldata = 100;int pagesize = 0;pagesize = (alldata + linesize - 1) / linesize;%>总页数为<%=pagesize%>页<%try {
currentpage = Integer.parseInt(request.getParameter("cp"));} catch (Exception e) {}%><%String driverName = "com.mysql.jdbc.Driver";String userName = "root";String userPasswd = "";String dbName = "lin_0917";String tableName = "person";String url = "jdbc:mysql://localhost/" + dbName + "?user="+ userName + "&password=" + userPasswd;%><%try {Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql2 = "select count(ID) from person";
ResultSet rs2 = statement.executeQuery(sql2);
if (rs2.next()) {alldata = rs2.getInt(1);}rs2.close();String sql = "select ID,NAME,PASSWORD from person";ResultSet rs = statement.executeQuery(sql);ResultSetMetaData rmeta = rs.getMetaData();%>数据库中记录总数为:<%=alldata%>当前页:<%=currentpage%>/<%=pagesize%><script type="text/javascript">function openpage(curpage){document.form1.cp.value = curpage;document.form1.submit();}function jumpto(){document.form1.cp.value = document.form1.jump.value;document.form1.submit();}</script><form action="<%=jspurl%>" name="form1"><input type="button" value="上一页"onclick="openpage(<%=currentpage - 1%>)"<%=currentpage == 1 ? "disabled" : ""%>><input type="button" value="首页 " onclick="openpage(1)"<%=currentpage == 1 ? "disabled" : ""%>><input type="button" value="尾页" onclick="openpage(<%=pagesize%>)"<%=currentpage == pagesize ? "disabled" : ""%>><input type="button" value="下一页"onclick="openpage(<%=currentpage + 1%>)"<%=currentpage == pagesize ? "disabled" : ""%>><input type="hidden" name="cp" value="">跳到第<select name="jump" onchange="jumpto()"><%for (int i = 1; i <= pagesize; i++) {%><option value="<%=i%>" <%=currentpage == i ? "selected" : ""%>><%=i%></option><%}%>
</select>页</form>
<table border="1" width="80%">
<tr>
<td>编号</td><td>登录名称</td><td>密码</td><td colspan="2">操作</td>
</tr>
<%int i = 0;for (int x = 0; x < (currentpage - 1) * linesize; x++) {rs.next();}for (int x = 0; x < linesize; x++) {
if (rs.next()) {i++;int id = rs.getInt(1);String name = rs.getString(2);String password = rs.getString(3);%><tr>
<td><%=id%></td><td><%=name%></td><td><%=password%></td><td>更新</td><td>删除</td></tr><%}}rs.close();
statement.close();
connection.close();if (i == 0) {%><tr>
<td colspan="5">数据库中没有数据</td>
</tr><%}%>
</table>
<%} catch (Exception e) {%><h2>系统出错!!!</h2><%}%></center></body></html>
效果: 效果: