【问题标题】:Getting a date from a form and saving into database and listing products从表单中获取日期并保存到数据库中并列出产品
【发布时间】:2013-02-23 17:01:31
【问题描述】:

我正在尝试创建一个可以列出以下产品的页面 保存在数据库中,然后客户可以查看列表 可用的产品。我遇到的问题是日期 在 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.

谁能告诉我怎么了?

【问题讨论】:

  • 不要对段落使用代码块
  • 你能正确格式化脚趾码吗?
  • 正在处理中。

标签: java sql jdbc


【解决方案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);

【讨论】:

  • 非常感谢!!!我被困了 2 天,我不知道,但非常感谢
  • 现在还有另一个错误.....当我运行代码时它说由于 null 注册失败:(有什么想法吗?
  • 嗯,我是 netbeans 的新手,我不知道该怎么做 :(
  • 我的意思是复制并粘贴终端显示的文本。
  • init:deps-module-jar:deps-ear-jar:deps-jar:library-inclusion-in-archive:library-inclusion-in-manifest:compile:compile-jsps:Undeploying。 .. undeploy?path=/alliance
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
  • 1970-01-01
  • 2020-04-29
  • 1970-01-01
  • 2018-06-15
  • 2017-06-30
  • 1970-01-01
相关资源
最近更新 更多