0

所以我做了一个简单的网站,我可以使用 Spring 框架 4.0.1 将产品添加到列表(文本区域),我可以看到页面内容,但在底部的文本区域中有这个异常阻止我在列表中获取产品。

模型类

package model;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author mkSS
 */
public class Products {
    public int product_id;
    public String productname;
    public String productstock;
    public String productdescription;

    public int getProduct_id() {
        return product_id;
    }

    public void setProduct_id(int product_id) {
        this.product_id = product_id;
    }

    public String getProductname() {
        return productname;
    }

    public void setProductname(String productname) {
        this.productname = productname;
    }

    public String getProductstock() {
        return productstock;
    }

    public void setProductstock(String productstock) {
        this.productstock = productstock;
    }

    public String getProductdescription() {
        return productdescription;
    }

    public void setProductdescription(String productdescription) {
        this.productdescription = productdescription;
    }


    public static String productList() throws ClassNotFoundException, SQLException {
    StringBuilder product_list = new StringBuilder();
    Class.forName("com.mysql.jdbc.Driver");
    try (java.sql.Connection conn= DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "miksa988");){
    Statement st= conn.createStatement();
    st.executeQuery("select productname, productstock,productdescription from products"); 
    ResultSet rs= st.getResultSet();

    while (rs.next()){

    product_list.append(rs.getString("productname"));
    product_list.append(rs.getString(", "));
    product_list.append(rs.getString("productstock"));
    product_list.append(rs.getString(", ")); 
    product_list.append(rs.getString("productdescription"));
    product_list.append(rs.getString("\n"));






    }
    } catch(SQLException ex){
      product_list.append(ex.getMessage());
    }
      return product_list.toString();
    }


    public void addProduct() throws ClassNotFoundException {
    Class.forName("com.mysql.jdbc.Driver");
    try (java.sql.Connection conn= DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "miksa988");){
    if (productname !=null && !(productname.isEmpty()) && productstock !=null && !(productstock.isEmpty()) && productdescription !=null && !(productdescription.isEmpty())){
    Statement st = conn.createStatement();
    st.execute("insert into products (productname,productstock, productdescription) values ('"+ productname + "','"+productstock+"','"+productdescription+"')");
    }  


    }catch(SQLException ex){
        System.out.println("Error while trying to connect with databse"+ex);
}
}
}

现在这是我的控制器类

包控制器;

import java.sql.SQLException;
import model.Products;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class ProductController {

    @RequestMapping(value = "/product", method= RequestMethod.GET)
    public String createForm(ModelMap model) throws ClassNotFoundException, SQLException{
    model.addAttribute("product", new Products());
    model.addAttribute("products", Products.productList() );
    return "product";
    }
    @RequestMapping(value = "/product", method= RequestMethod.POST)
    public String addProduct(@ModelAttribute("product") Products product , ModelMap model) throws ClassNotFoundException ,SQLException {
        product.addProduct();
        createForm(model);
        return "product";
    }
}

这是我的 JSP 页面

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>E-commerce</title>
    </head>
    <body>
        <h1>Our Products</h1>
        <form:form action="product.htm" method="post" commandName="product">
            <form:label path="productname">Enter product name:</form:label></br>
            <form:input id="pname" type="text" path="productname" placeholder="product name"></form:input><br>
            <form:label path="productstock">Enter amount of items in stock:</form:label></br>
            <form:input id="pstock" type="text" path="productstock" placeholder="product stock"></form:input><br>
            <form:label path="productdescription">Enter product description:</form:label></br>
            <form:input id="pdescription" type="text" path="productdescription" placeholder="product description"></form:input><br>
            <input type="submit" value="Dodaj proizvod"/>
        </form:form>
            <label for="product_list" id="products_list_label">List of products: </label> </br>
            <textarea id="products_list" rows="20" cols="100" readonly>${products}</textarea>
    </body>
</html>

这是我的 SQL 查询

 `product_id` INT NOT NULL AUTO_INCREMENT,
  `productname` VARCHAR(45) NOT NULL,
  `productstock` INT NOT NULL,
  `productdescription` VARCHAR(45) NOT NULL,
  PRIMARY KEY (`product_id`));

加载页面时发生错误,我可以看到内容(那些标签和文本区域)但在文本区域中我可以看到这个 java lang 类转换异常,所以当我尝试在列表中获取产品时我不能。会是什么呢?

4

0 回答 0