I’m trying to create a page which can list products which are saved in the database and then the customers can view the list of products that are available.The problem I am having is with the date in Java.
package servlets; import bean.ProductBean; import db.JavaConnect; import helper.ProductHelper; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AddNewProduct extends HttpServlet { public static final String Addproduct = "Addproduct"; public static final String productfailed = "productfailed"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { ProductBean addproduct = new ProductBean(); ProductHelper.populateaddproduct(addproduct, request); addproduct(addproduct); request.setAttribute(Addproduct, addproduct); } catch (Throwable ex) { Logger.getLogger(AddNewProduct.class.getName()).log(Level.SEVERE, null, ex); request.setAttribute(productfailed, ex); } request.getRequestDispatcher("Addproductstatus.jsp").forward(request, response); } public void addproduct(ProductBean addproduct) throws ApplicationException { Connection conn = null; PreparedStatement pst = null; Statement rowIDStmt = null; ResultSet rs = null; try { conn = JavaConnect.ConnectDb(); String sql = "INSERT INTO products(EAN,PIP,name,description,supplier,price,expiryDate,latest,discount)VALUES(?,?,?,?,?,?,?,?,?)"; pst = conn.prepareStatement(sql); pst.setString(1, addproduct.getEan()); pst.setString(2, addproduct.getPip()); pst.setString(3, addproduct.getName()); pst.setString(4, addproduct.getDescription()); pst.setString(5, addproduct.getSupplier()); pst.setDouble(6, addproduct.getPrice()); // problem here java.sql.Date sqlDate = new java.sql.Date(addproduct.getExpiryDate().getTime()); pst.setDate(7,addproduct.getExpiryDate()); pst.setString(8, addproduct.getLatest()); pst.setString(9, addproduct.getDiscounted()); pst.executeUpdate(); rowIDStmt = conn.createStatement(); rs = rowIDStmt.executeQuery("SELECT last_insert_rowid()"); rs.next(); Integer autoIncreamentId = rs.getInt(1); Logger.getLogger(AddNewProduct.class.getName()).log(Level.INFO, "Successfully inserted row with id {0}", autoIncreamentId); } catch (Throwable t) { throw new ApplicationException(t.getMessage(), t); } finally { JavaConnect.close(null, rowIDStmt, rs); JavaConnect.close(conn, pst, null); } } // end catch }
The error message I am getting is
no suitable method found for setDate(int.java.util.date) method prepared statement.setDate(int.java.sql.date,Calender) is not applicable. method prepared statement.setDate(int.java.sql.Date) is not applicable and cannot be converted to java.sql.date by method invocation.
Can someone please tell me what is wrong?
Advertisement
Answer
Replace
java.sql.Date sqlDate = new java.sql.Date(addproduct.getExpiryDate().getTime()); pst.setDate(7,addproduct.getExpiryDate());
By
java.sql.Date sqlDate = new java.sql.Date(addproduct.getExpiryDate().getTime()); pst.setDate(7,sqlDate);