<%@ page contentType="text/html;charset=gb2312"%><%@ page import="java.sql.*"%>分页显示 人员列表
<% // 进行乱码处理 request.setCharacterEncoding("GB2312"); %> <%! final String jspUrl = "list_person.jsp"; %> <% // 定义如下分页变量 // 1、定义每页要显示的记录数 int lineSize = 10; // 2、定义当前页 int currentPage = 1; // 计算出总页数 int pageSize = 0; // 总记录数 int allRecorders = 0; // 加入查询关键字变量 String keyWord = ""; %> <% // 接收查询关键字 keyWord = request.getParameter("kw"); // 接收传过来的当前页 try { currentPage = Integer.parseInt(request.getParameter("cp")); } catch(Exception e) { } %> <% final String DBDRIVER = "org.gjt.mm.mysql.Driver"; final String DBURL = "jdbc:mysql://localhost/test"; final String DBUSER = "root"; final String DBPASSWORD = "depravedAngel"; Connection conn = null; %> <% try { Class.forName(DBDRIVER); conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD); String sql = null; PreparedStatement pstmt = null; ResultSet rs = null; if(keyWord == null || "".equals(keyWord)) { sql = "SELECT COUNT(*) FROM person"; } else { sql = "SELECT COUNT(*) FROM person WHERE uid LIKE ? OR name LIKE ?"; } pstmt = conn.prepareStatement(sql); if(!(keyWord == null || "".equals(keyWord))) { pstmt.setString(1,"%" + keyWord + "%"); pstmt.setString(2,"%" + keyWord + "%"); } rs = pstmt.executeQuery(); if(rs.next()){ allRecorders = rs.getInt(1); } rs.close(); pstmt.close(); // 计算总页数 pageSize = (allRecorders+lineSize-1)/lineSize; if(keyWord == null || "".equals(keyWord)) { sql = "SELECT id,uid,name,password FROM person limit " + (currentPage-1)*lineSize + "," + lineSize; } else { sql = "SELECT id,uid,name,password FROM person WHERE uid LIKE ? OR name LIKE ? limit " + (currentPage-1)*lineSize + "," + lineSize; } pstmt = conn.prepareStatement(sql); if(!(keyWord == null || "".equals(keyWord))) { pstmt.setString(1,"%" + keyWord + "%"); pstmt.setString(2,"%" + keyWord + "%"); } rs = pstmt.executeQuery(); %>