JavaBean+Servlet+jsp真分页实现

来源:互联网 发布:js取小数点后2位 编辑:程序博客网 时间:2024/05/21 10:10

JavaBean

package Bean;//物品类public class Goods{ private int goodsid;  // 物品编号 public String goodsname;   // 物品名称 private float price;        // 物品单件 public void setGoodsid(int goodsid) {  this.goodsid = goodsid; } public int getGoodsid() {  return goodsid; } // 物品名称 public void setGoodsname(String goodsname) {  this.goodsname = goodsname; } public String getGoodsname() {  return goodsname; } // 物品价格 public void setPrice(float price) {  this.price = price; } public float getPrice() {  return price; }}

单例类

package util;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DbConnection { private static Connection conn=null; private String driver="com.mysql.jdbc.Driver"; private String url="jdbc:mysql://localhost:3306/mydata"; private String user="root"; private String password="1234567";  private DbConnection(){  try {   Class.forName(driver);   conn=DriverManager.getConnection(url, user, password);  } catch (ClassNotFoundException e) {   e.printStackTrace();  } catch (SQLException e) {   e.printStackTrace();  } }  public static Connection getConnection() {  if(conn==null){   new DbConnection();  }  return conn; }}

数据库访问实现

package DAO;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import Bean.Goods;import util.DbConnection;public class Dao { private static Connection conn; private static ResultSet rs; private static Statement stmt; private static int pagesize=5; static{  conn=DbConnection.getConnection(); }  //通用的查询方法 public static ResultSet ExecuteQuery(String sql) {  try {   stmt=conn.createStatement();   rs=stmt.executeQuery(sql);  } catch (SQLException e) {   e.printStackTrace();  }  return rs; }  //分页逻辑 public static ArrayList<Goods> getGoodsList(int currentpageno){  ArrayList<Goods> GoodsList=new ArrayList<Goods>();  int BeginRecord=(currentpageno-1)*pagesize;  int EndRecord=currentpageno*pagesize;  rs=ExecuteQuery("select * from goods limit "+BeginRecord+","+EndRecord);  try {   while(rs.next()){    Goods goods=new Goods();    goods.setGoodsid(rs.getInt(1));    goods.setGoodsname(rs.getString(2));    goods.setPrice(rs.getFloat(3));    GoodsList.add(goods);   }  } catch (SQLException e) {   e.printStackTrace();  }  return GoodsList; }  //统计记录数 public static int getPageCount(){  int total=0;  int PageCount=0;  rs=ExecuteQuery("select count(*) from goods");  try {   if(rs.next()){    total=rs.getInt(1);    PageCount=(total-1)/pagesize+1;   }  } catch (SQLException e) {   e.printStackTrace();  }  return PageCount; }}

servlet控制器

package servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import Bean.Goods;import DAO.Dao;public class GetLimiteGoods extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {  response.setContentType("text/html");  String pageno= request.getParameter("currentpageno");  int currentpageno=1;  if(pageno!=null){   currentpageno=Integer.parseInt(pageno);  }  ArrayList<Goods> GoodsList=Dao.getGoodsList(currentpageno);  request.setAttribute("GoodsList", GoodsList);  request.setAttribute("currentpageno", currentpageno);  request.setAttribute("PageCount", Dao.getPageCount());  request.getRequestDispatcher("/LimiteGoods.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {  response.setContentType("text/html");  PrintWriter out = response.getWriter();    out.println("<HTML>");  out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");  out.println("  <BODY>");  out.print("    This is ");  out.print(this.getClass());  out.println(", using the POST method");  out.println("  </BODY>");  out.println("</HTML>");  out.flush();  out.close(); }}

xml配置:

<?xml version="1.0" encoding="UTF-8"?>  <servlet>    <description>This is the description of my J2EE component</description>    <display-name>This is the display name of my J2EE component</display-name>    <servlet-name>GetLimiteGoods</servlet-name>    <servlet-class>servlet.GetLimiteGoods</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>GetLimiteGoods</servlet-name>    <url-pattern>/servlet/GetLimiteGoods</url-pattern>  </servlet-mapping></web-app>

表现层jsp

<html> <head>  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  <title>Insert title here</title> </head> <body> <center> <c:if test="${currentpageno>=1}">   <a href="GetLimiteGoods?currentpageno=1">首页</a>   <a href="GetLimiteGoods?currentpageno=${currentpageno-1}">上一页</a>  </c:if>  <c:if test="${currentpageno==1}">   <a href="GetLimiteGoods?currentpageno=${currentpageno+1}">下一页</a>   <a href="GetLimiteGoods?currentpageno=${PageCount}">尾 页</a>  </c:if>  <table width="80%" border="1" height="56">   <tr align="center">    <td>     商品编号    </td>    <td>     商品名称    </td>    <td>     商品价格    </td>   </tr>   <c:forEach var="goods" items="${GoodsList}">    <tr align="center">     <td>      ${goods.goodsid}     </td>     <td>      ${goods.goodsname}     </td>     <td>      ${goods.price}     </td>    </tr>   </c:forEach>  </table>  </center> </body></html>


0 0
原创粉丝点击