从形式获取日期和保存到数据库和上市产品(Getting a date from a form and

2019-08-17 03:44发布

我想创建一个网页,其中可以列出被保存在数据库中的产品,然后客户可以查看的是我有available.The问题是在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 }

我得到的错误信息是

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.

有人能告诉我什么是错?

Answer 1:

更换

  java.sql.Date sqlDate = new java.sql.Date(addproduct.getExpiryDate().getTime());   
  pst.setDate(7,addproduct.getExpiryDate());

通过

java.sql.Date sqlDate = new java.sql.Date(addproduct.getExpiryDate().getTime()); 
pst.setDate(7,sqlDate);


文章来源: Getting a date from a form and saving into database and listing products
标签: java sql jdbc