简体   繁体   中英

cannot convert java.util.Date to java.sql.Date with JXDatePicker

i am using a JXDatepicker here. i dont know what to do with the error please help me. i tried a lot but failed. 在此处输入图片说明

i tried to print what cause this error and it prints this message. i want the format to be yyyy/MM/dd cause i want it to insert into the database.

AddProductGUI.java

txtExpiration = new JXDatePicker();                     txtExpiration.setName("Expiration");
txtExpiration.setBorder(null);
txtExpiration.setFormats(new SimpleDateFormat("yyyy.MM.dd"));

AddProduct.java

public java.util.Date returnDate(JXDatePicker txtExpiration, Boolean isExpiring) {
        if (isExpiring) {
            return txtExpiration.getDate();
        } else {
            return null;
        }
    }

ActionToDatabase.java

if (!DatabaseValidator
                            .isExistingData(
                                    "select ProductID from tblIndividualProduct where ProductID =?",
                                    product.getProductID())) {

                        sql = "insert into tblIndividualProduct (ProductId,Code,Expiration,Note,UpdatedPrice,PriceChanged) Values(?,?,?,?,?,?)";

                        connection
                                .setPreparedStatement((PreparedStatement) connection
                                        .getConnection().prepareStatement(sql));
                        connection.getPreparedStatement().setString(1,
                                product.getProductID());
                        connection.getPreparedStatement().setString(2,
                                product.getCode());

                        //connection.getPreparedStatement().setDate(3, new java.sql.Date(product.getExpiration().getTime()));
                        **connection.getPreparedStatement().setDate(3, (java.sql.Date)product.getExpiration());**

                        connection.getPreparedStatement().setString(4,
                                product.getNote());
                        connection.getPreparedStatement().setDouble(5,
                                product.getPrice());
                        connection.getPreparedStatement().setBoolean(6,
                                product.getPriceChanged());
                        connection.getPreparedStatement().executeUpdate();
                        System.out
                                .println("Successfully added individual product ("
                                        + product.getProductID()
                                        + ") to the database");

在此处输入图片说明

在此处输入图片说明

Create a new instance of a java.sql.Date using the java.util.Date as the base value, for example

connection.getPreparedStatement().setDate(3, 
    (product.getExpiration() == null ? 
        null : 
        new java.sql.Date(product.getExpiration().getTime())));

It would be possible to pass a java.sql.Date to JXDatePicker as java.sql.Date extends from java.util.Date , but it won't work the other way around (you can't cast a java.util.Date to a java.sql.Date without first knowing that the object is acutally a java.sql.Date masquerading as a java.util.Date

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM