0

so i made simple site where i can add products to list (text area) using Spring framework 4.0.1, i can see page content but in text area on the bottom there is this exception preventing me from getting products on list.

Model class

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

And now this is my controller class

package controller;

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

and this is my JSP page

<%@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>

this is my SQL Query

 `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`));

Error occurred when i load page, i can see content (those labels and text area) but in text-area i can see this java lang class cast exception is there, so when i try to get products on list i can't. What could it be?

  • What are the types of columns in table `products`? Also what do you expect as result when calling `rs.getString(", ")`? – Ivan Jun 28 '18 at 14:59
  • 1
    Read up on [ClassCastException](https://stackoverflow.com/questions/907360/explanation-of-classcastexception-in-java). Somewhere in your code you are likely selecting a BigInteger from the database and trying to store it in a Long. The relevant code does not seem to be above. – Stephan Jun 28 '18 at 15:03
  • Hey Ivan my columns are product_id , productname, productstock, productdescription – Mihajlo Sljukic Jun 28 '18 at 15:09
  • What are their types? Please also add full stacktrace and corresponding code snippet to the question. – Ivan Jun 28 '18 at 15:14
  • Also i used your point on rs. and edited it to product_list.append(rs.getInt("product_id")); product_list.append(rs.getString("productname")); product_list.append(rs.getInt("productstock")); product_list.append(rs.getString("productdescription")); still throwing same error – Mihajlo Sljukic Jun 28 '18 at 15:17
  • I updated post with sql query – Mihajlo Sljukic Jun 28 '18 at 15:18

0 Answers0