C3P0JDBCUtils
Product:
package domain;
import java.util.Date;
public class Product {
private String pid;
private String pname;
private Double market_price;
private Double shop_price;
private String pimage;
private Date pdate;
private Integer is_hot;
private String pdesc;
private Integer pflag;
public Product() {
}
public Product(String pid, String pname, Double market_price,
Double shop_price, String pimage, Date pdate, Integer is_hot,
String pdesc, Integer pflag) {
this.pid = pid;
this.pname = pname;
this.market_price = market_price;
this.shop_price = shop_price;
this.pimage = pimage;
this.pdate = pdate;
this.is_hot = is_hot;
this.pdesc = pdesc;
this.pflag = pflag;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Double getMarket_price() {
return market_price;
}
public void setMarket_price(Double market_price) {
this.market_price = market_price;
}
public Double getShop_price() {
return shop_price;
}
public void setShop_price(Double shop_price) {
this.shop_price = shop_price;
}
public String getPimage() {
return pimage;
}
public void setPimage(String pimage) {
this.pimage = pimage;
}
public Date getPdate() {
return pdate;
}
public void setPdate(Date pdate) {
this.pdate = pdate;
}
public Integer getIs_hot() {
return is_hot;
}
public void setIs_hot(Integer is_hot) {
this.is_hot = is_hot;
}
public String getPdesc() {
return pdesc;
}
public void setPdesc(String pdesc) {
this.pdesc = pdesc;
}
public Integer getPflag() {
return pflag;
}
public void setPflag(Integer pflag) {
this.pflag = pflag;
}
}
ProductDao:
package ProductDao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import Utils.C3P0JDBCUtils;
import domain.Product;
public class ProductDao {
public List<Product> findAll() throws SQLException {
QueryRunner queryRunner = new QueryRunner(C3P0JDBCUtils.getDataSource());
String sql = "select * from product order by pdate desc";
List<Product> list = queryRunner.query(sql, new BeanListHandler<Product>(Product.class));
return list;
}
}
ProductService:
package ProductService;
import java.sql.SQLException;
import java.util.List;
import ProductDao.ProductDao;
import domain.Product;
public class ProductService {
public List<Product> findAll() throws SQLException {
ProductDao productDao = new ProductDao();
List<Product> list = productDao.findAll();
return list;
}
}
ProductServlet:
package ProductServlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ProductService.ProductService;
import domain.Product;
public class ProductServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
ProductService productService = new ProductService();
List<Product> list = productService.findAll();
request.setAttribute("list", list);
request.getRequestDispatcher("/demo4/product.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
product.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%
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 'product.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">
</head>
<body>
<table border="1px" width="1000">
<tr>
<td>商品编号</td>
<td>商品名称</td>
<td>商品价格</td>
<td>是否热门</td>
<td>商品描述</td>
</tr>
<c:forEach var="p" items="${ list }">
<tr>
<td>${ p.pid }</td>
<td>${ p.pname }</td>
<td>${ p.shop_price }</td>
<td>
<c:if test="${ p.is_hot == 1 }">
是
</c:if>
<c:if test="${ p.is_hot == 0 }">
否
</c:if>
</td>
<td>${ p.pdesc }</td>
</tr>
</c:forEach>
</table>
</body>
</html>