<%@page import="java.sql.DriverManager"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.Connection"%> <%@page import="com.contrl.*"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> //导入JSTL <% 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>   <%       int PageSize = 3; //设置页面大小       //获得当前页       String tCurrentPage = null;        tCurrentPage = request.getParameter("currentPage");       //设置当前页       int currentPage = 0;       if (tCurrentPage != null) {        currentPage = Integer.parseInt(tCurrentPage.trim());        }        System.out.println("currentPage = " + currentPage + " PageSize =  " + PageSize);       //设置起始坐标       int startIndex = currentPage*PageSize;       //放入request       request.setAttribute("currentPage",currentPage);              //利用JDBC获得所有用户       List<User> users = new ArrayList<User>();              String driverName = "com.mysql.jdbc.Driver";       String url = "jdbc:mysql://localhost:3306/test";       String username = "root";       String password = "1";              Class.forName(driverName);       Connection conn = null;       PreparedStatement pst = null;       ResultSet rs = null;       conn = DriverManager.getConnection(url, username, password);       //基于MySQL的limit分页显示第一个参数起始位置(从0开始), 第二个参数为页面大小       String sql = "select * from users limit ?,?";       pst = conn.prepareStatement(sql);       pst.setInt(1,startIndex);       pst.setInt(2,PageSize);              rs = pst.executeQuery();       User user = null;       while (rs.next()) {        user = new User();        user.setId(rs.getInt("id"));        String tName = rs.getString("name");        //System.out.println("T = " +  tName);        String name = new String(tName.getBytes("ISO-8859-1"),"utf-8");        System.out.println("name = " + name);        user.setName(tName);        //System.out.println(user.getId() + " " + user.getName());        users.add(user);       }       //将所有的用户放入request中       request.setAttribute("users", users);              //获得总页数       int allPage = 0;       sql = "select count(*) from users";       pst = conn.prepareStatement(sql);       rs = pst.executeQuery();       if (rs.next()) {        allPage = (Integer.parseInt(rs.getString(1)) + PageSize - 1) / PageSize;       }       System.out.println("allPage = " + allPage);       //设置最大页数       request.setAttribute("allPage", allPage);      %>         <body>            <table border="1" align="center">       <tr> <th>ID</th>   <th>姓名</th> </tr>       <c:forEach items="${users}" var="user">        <tr>         <td> ${user.id} </td>                  <td> ${} </td>               </tr>       </c:forEach>      </table>            <div align="center">       <a href="?currentPage=0">首页</a>       <c:if test="${currentPage == 0}"> 上一页</c:if>       <c:if test="${ currentPage >= 1}">   <a href="?currentPage=${currentPage - 1 }"> 上一页 </a>  </c:if>              <c:if test="${currentPage == allPage - 1}"> 下一页</c:if>       <c:if test="${ currentPage < allPage - 1}">   <a href="?currentPage=${ currentPage + 1 }"> 下一页 </a>  </c:if>              <a href="?currentPage=${ allPage - 1 }">末页</a>              <b> ${allPage }</b>      </div>                     </body> </html>



在这这部分代码时最让我头疼的就是中文乱码问题了。

最后的解决办法是:


① 首先把MySQL的服务停掉 在运行窗口输入:net stop mysql ② 把服务器和客户端的字符集改成自己想用的字符集:GB2312或是utf8等…… 具体操作为:打开mysql安装目录下的my.ini; 找到default-character-set,将其改为自己想用的字符集:GB2312或是utf8等……,要注意的是这里有两个default-character-set,用ctrl+f定位在文件最前面输入default就会找到,都要改过来; ③ 重启MySQL服务器,在运行窗口输入:net start mysql ④ 最重要的是一点是,到这里我们已经能够解决乱码问题了,可问题是我们依然还会出现乱码问题,这是因为我们现在的表被创建的时候用的是默认的字符集(latin1),所以这时候我们要把表删除,然后重建就可以了



我是在windos下弄得,改成utf-8的话,控制台会出现中文乱码而且不能插入中文,但是Navicat没问题,后来改成GB2312就好了。


核心部分还是MySQL的limit?,?分页, 第一个表示分页的开始坐标, 第二个参数表示页面大小。