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("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
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"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<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>