问题代码如下。
<jsp.useBean id = "product" class = "domain.Product" scope = "request" />
<%
request.setCharacterEncoding("UTF-8");
List<Product> products = product.searchAll();
for (Product p : products) {
%>
在上面的jsp文件里包含了这两个。
<%@ page import="domain.Product"%>
<%@ page import="java.util.*" %>
这个是product类,放在src文件夹下的domain包中。
package domain;
import java.util.;
import java.sql.;
public class Product {
private String id;
private String name;
private double price;
private String category;
private int pnum;
private String imgurl;
private String description;
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(double price) {
this.price = price;
}
public void setCategory(String category) {
this.category = category;
}
public void setPnum(int pnum) {
this.pnum = pnum;
}
public void setImgurl(String imgurl) {
this.imgurl = imgurl;
}
public void setDescription(String ds) {
this.description = ds;
}
public String getId() {
return this.id;
}
public String getName() {
return this.name;
}
public double getPrice() {
return this.price;
}
public String getCategory() {
return this.category;
}
public int getPnum() {
return this.pnum;
}
public String getImgurl() {
return this.imgurl;
}
public String getDescription() {
return this.description;
}
public List<Product> searchAll(){
List<Product> ps = new ArrayList<Product>();
try{
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/bookstoredb?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=CST";
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url,"root","0609chenwanyi");
}catch(Exception e){e.printStackTrace();}
String sql = "select * from bookstoredb.product";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
Product p = new Product();
p.setId(rs.getString(1));
p.setName(rs.getString(2));
p.setPrice(rs.getDouble(3));
p.setCategory(rs.getString(4));
p.setPnum(rs.getInt(5));
p.setImgurl(rs.getString(6));
p.setDescription(rs.getString(7));
ps.add(p);
}
}catch(Exception e) {e.printStackTrace();}
return ps;
}
public boolean add(Product product) {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/bookstoredb?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=CST";
try {
try{
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url,"root","0609chenwanyi");
}catch(Exception e){e.printStackTrace();}
String sql="insert into product (id,name,price,category,pnum,imgurl,description) values (?,?,?,?,?,?,?)";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1, UUID.randomUUID().toString());
pstmt.setString(2, product.getName());
pstmt.setDouble(3, product.getPrice());
pstmt.setString(4, product.getCategory());
pstmt.setInt(5,product.getPnum());
pstmt.setString(6, product.getImgurl());
pstmt.setString(7, product.getDescription());
}catch(Exception e) {e.printStackTrace();}
return true;
}
}