(jsp|(jsp 和 servlet功能篇) jsp+servlet+javaBean 实现分页代码(mysql 数据库)

实例代码:

JavaBeanpackage 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 getGoodsList(int currentpageno){ ArrayList GoodsList=new ArrayList(); 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控制器
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 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 { doGet(request,rsponse); } }

配置文件
xml配置: This is the description of my J2EE component This is the display name of my J2EE component GetLimiteGoods servlet.GetLimiteGoodsGetLimiteGoods /servlet/GetLimiteGoods


jsp代码:
表现层jsp Insert title here - 锐客网 首页 上一页 下一页 尾页
商品编号 商品名称 商品价格
${goods.goodsid} ${goods.goodsname} ${goods.price}

【(jsp|(jsp 和 servlet功能篇) jsp+servlet+javaBean 实现分页代码(mysql 数据库)】


    推荐阅读