一,使用jsp将数据库内的数据。分页显示到页面
1,创建一个servelt处理后台数据,和数据库的数据
private static final long serUid=1L; //加载驱动 static final String jdbc="com.mysql.jdbc.Driver"; //要连接的数据库url static final String db_url="jdbc:mysql://localhost:3306/test"; //数据库用户名 static final String user="db"; //数据库密码 static final String pass="1743721"; int currentPage=1; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection conn=null; Statement stmt=null; //显示的数据的格式 response.setContentType("text/html;charset=UTF-8"); PrintWriter out =response.getWriter(); try { Class.forName(jdbc); conn=DriverManager.getConnection(db_url,user,pass); stmt=conn.createStatement(); String sql=null; sql="select * from t_message where 1=1 order by s_date desc"; int pageSize=5; int d=3; String re=null;
//接收url中的参数 re=request.getQueryString(); //System.out.print(re); if(re!=null){ String[] ee=re.split("="); if("cupg".equals(ee[0])){ re=ee[1]; int r=Integer.valueOf(re).intValue(); d=r; currentPage=r; }else{ re=ee[1]; int bt=Integer.valueOf(re).intValue(); currentPage=bt; if(bt<=3){ d=3; }else{ d=bt; } } } if(currentPage<=0){ d=3; currentPage=1; } int start=(currentPage-1)*pageSize;
//获取数据进行分页处理 String limitsql="select * from("+sql+")pp limit "+start+","+pageSize; ResultSet rs=stmt.executeQuery(limitsql); List<User> userlist=new ArrayList<User>(); User user=null; while(rs.next()){
//创建一个对象存入数据 user=new User(); user.setMessage_id(rs.getInt("message_id")); user.setTitle(rs.getString("title")); user.setName(rs.getString("name")); user.setS_date(rs.getString("s_date")); user.setS_des(rs.getString("s_des")); user.setL_des(rs.getString("l_des")); userlist.add(user); } request.setAttribute("nubtn",d); request.setAttribute("cupg", currentPage); request.setAttribute("args", userlist); request.getRequestDispatcher("/qh/message.jsp").forward(request, response); //关闭通道 rs.close(); conn.close(); stmt.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
2,在页面接收并处理得到的数据
<div class="column"> <h1 class="column-tit"><span><a href="#" title="首页">首页</a> > <a href="#" title="留言回复">留言回复</a></span>留言回复</h1> <div class="mailbox mt20"> <% ArrayList userlist=(ArrayList)request.getAttribute("args");
//遍历的到的数据 for(int i=0;i<userlist.size();i++){ User user=(User)userlist.get(i); %> <div class="online-content clearfix"> <div class="icon fl"> <img src="../images/online.png" alt=""> </div> <div class="question fr"> <p>留言主题:<%=user.getTitle()%> </p> <p class="name"><span>网友:<%=user.getName()%></span><span><%=user.getS_date()%></span><span><a target="_blank" href="#">我要留言</a></span></p> <div class="message-i "> <p><strong>留言详情:</strong><%=user.getS_des()%></p> </div> <div class="answer"> <p><strong>执法监察总队回复:</strong><%=user.getL_des()%></p> </div> </div> </div> <% } %> <div class="page">
//分页按钮的处理 <%int currentPage=(Integer)request.getAttribute("cupg"); int cugg=3; Integer d=0; int nubtn=(Integer)request.getAttribute("nubtn"); if(nubtn>=3)cugg=nubtn; String a=request.getQueryString(); if(a!=null){ String[] ee=a.split("="); a=ee[1]; d=Integer.parseInt(a); } %> <div style="display: none;" id="yc"><%=a%></div> <ul id="mu"><li class="num"><a href="/MysqlTest/servlet/GetMysql?cupg=1">首页</a></li> <li class="num"><a href="/MysqlTest/servlet/GetMysql?cupg=<%=currentPage-1%>">上一页</a></li> <li class="num page-active"><a href="/MysqlTest/servlet/GetMysql?nubtn=<%=cugg-2%>"><%=cugg-2%></a></li> <li class="num" style=""><a href="/MysqlTest/servlet/GetMysql?nubtn=<%=cugg-1%>"><%=cugg-1 %></a></li> <li class="num"><a href="/MysqlTest/servlet/GetMysql?nubtn=<%=cugg %>"><%=cugg %></a></li> <li class="num" ><a href="/MysqlTest/servlet/GetMysql?nubtn=<%=cugg+1%>"><%=cugg+1%></a></li> <li class="num"><a href="/MysqlTest/servlet/GetMysql?nubtn=<%=cugg+2%>"><%=cugg+2%></a></li> <li class="page-speciall">...</li><li class="num">34</li> <li class="num"><a href="/MysqlTest/servlet/GetMysql?cupg=<%=currentPage+1%>" >下一页</a></li> <li class="num"><a href=>尾页</a></li></ul> </div> </div> </div>