weixin_52310393 2022-04-22 23:38 采纳率: 90%
浏览 35
已结题

java web 无法为JSP编译类

问题代码如下。

<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;
}

}

  • 写回答

1条回答 默认 最新

  • 吕布辕门 后端领域新星创作者 2022-04-23 14:31
    关注

    这种写法不太好,不要再jsp🀄️写java方法调用,最好提供接口,望采纳

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 7月22日
  • 已采纳回答 7月14日
  • 创建了问题 4月22日

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来