我正在尝试创建一个可以列出产品的页面 保存在数据库中,然后客户可以查看列表 可用的产品。我遇到的问题是日期 在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.
有人可以告诉我出了什么问题吗?
答案 0 :(得分: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);