<%@ page language="java" import="java.util.*,com.eshore.pojo.Product" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="com.eshore.factory.DAOFactory" %>
<%
request.setCharacterEncoding("utf-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>查询产品列表</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String product_name = request.getParameter("product_name");
List<Product> list= DAOFactory.getIEmpDAOInstance().findAll(product_name);
%>
<form action="product_list.jsp" method="post">
请输入产品名称<input name="product_name"/>
<input type = "submit" value = "提交">
</form>
<table border = "1">
<tr>
<td>产品编号</td>
<td>产品名称</td>
<td>产品价格</td>
<td>产品信息</td>
</tr>
<%
for(int i=0;i<3;i++)
{
Product p= list.get(i);
%>
<tr>
<td><%=p.getProduct_id() %></td>
<td><%=p.getProduct_name() %></td>
<td><%=p.getPrice() %></td>
<td><%=p.getInfo() %></td>
</tr>
<% }
%>
</table>
</body>
</html>
以下是两个相关包
package com.eshore.factory;
import com.eshore.dao.ProductDao;
import com.eshore.service.ProductService;
public class DAOFactory {
public static ProductDao getIEmpDAOInstance() throws Exception{
return new ProductService();
}
}
package com.eshore.pojo;
public class Product {
private static final long serialVersionUID = 1L;
private String product_id;
private String product_name;
private double price;
private String info;
public Product(){
super();
}
public String getProduct_id() {
return product_id;
}
public void setProduct_id(String product_id) {
this.product_id = product_id;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
package com.eshore.dao;
import java.sql.*;
import java.util.*;
import com.eshore.pojo.Product;
public class ProductDaoImpl implements ProductDao {
private Connection conn = null;
private PreparedStatement pstmt = null;
public ProductDaoImpl(Connection conn){
this.conn = conn;
}
public boolean addProduct(Product product) throws Exception{
boolean flag = false ;
String sql =" insert into product(product_id,product_name,price,info) values(?,?,?,?)";
this.pstmt = this.conn.prepareStatement(sql);
this.pstmt.setString(1,product.getProduct_id());
this.pstmt.setString(2,product.getProduct_name());
this.pstmt.setDouble(3,product.getPrice());
this.pstmt.setString(4,product.getInfo());
if (this.pstmt.executeUpdate()>0) { flag = true; }
this.pstmt.close();
return flag;
}
public List<Product> findAll(String product_name) throws Exception{
List<Product> list = new ArrayList<Product>();
String sql = "select product_id,product_name,price,info from product";
if(product_name!=null&&!"".equals(product_name)){
sql = "select product product_id,product_name,price,info from product where product_name like? ";
this.pstmt.setString(1, "%" + product_name + "%" );
}
else {
this.pstmt = this.conn.prepareStatement(sql);
}
ResultSet rs = this.pstmt.executeQuery();
Product product = null;
while (rs.next()){
product = new Product();
product.setProduct_id(rs.getString(1));
product.setProduct_name(rs.getString(2));
product.setPrice(rs.getDouble(3));
product.setInfo(rs.getString(4));
list.add(product);
}
this.pstmt.close();
return list;
}
public Product findByProductId(String product_id) throws Exception{
Product product = null;
String sql ="select product_id,product_name,price,info from product where product_id =?";
this.pstmt = this .conn .prepareStatement(sql);
this.pstmt.setString(1, product_id);
ResultSet rs = this.pstmt.executeQuery();
if (rs.next()) {
product = new Product();
product.setProduct_id(rs.getString(1));
product.setProduct_name(rs.getString(2));
product.setPrice(rs.getDouble(3));
product.setInfo(rs.getString(4));
}
this.pstmt.close();
return product ;
}
}
黑女2008
慕工程4248865