0

i am using a JXDatepicker here. i dont know what to do with the error please help me. i tried a lot but failed. enter image description here

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

enter image description here

enter image description here

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

i already tried that new jaba.sql.Date(product.getExpiration().getTime()) but i get a nullpointer because i also want to return null when its the product is not expiring.
Sorry my bad, you could use a tri operator to detect the null from the produce and set the column value accordingly - see update...
thanks sir you save me everytime. i owe you a lot. :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.