从表单获取日期并将其保存到数据库并列出产品 [英] Getting a date from a form and saving into database and listing products

查看:275
本文介绍了从表单获取日期并将其保存到数据库并列出产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个页面,该页面可以列出 保存在数据库中,然后客户可以查看清单 我所遇到的问题是日期 在Java中.

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 }

我收到的错误消息是

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?

推荐答案

替换

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

这篇关于从表单获取日期并将其保存到数据库并列出产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆