简体   繁体   中英

Error while storing jXDatePicker1 Date value in database

I am using swingxLabs' component jXDatePicker1 to pick date in a graphical format and trying to store it in the database made in derby. My code was this:

Date date=jXDatePicker1.getDate();

 PreparedStatement statement = connect
      .prepareStatement("INSERT INTO BILLING (DATE, DHRNUMBER) VALUES('"+date+"', "+dhrNumber+")");

The error which i am getting is:

java.sql.SQLDataException: The syntax of the string representation of a datetime value is incorrect.

Am i doing it right? Or there can be some other way to solve this.

Thanks

Derby's built-in DATE datatype supports a short list of string formats: http://db.apache.org/derby/docs/10.9/ref/rrefsqlj18730.html

Since you are using PreparedStatement, the best thing to do is to prepare the statement

INSERT INTO BILLING (DATE, DHRNUMBER) VALUES(?,?)

and then substitute your actual values using the setDate() and setInt() methods from: http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html

This alternative totally worked for me:

    Date d=jXDatePicker1.getDate();
    System.out.println(d);
    DateFormat df=new SimpleDateFormat("MM/dd/yyyy");
    String date=df.format(d);
    System.out.println(date);

PreparedStatement statement = connect
      .prepareStatement("INSERT INTO BILLING (DATE) VALUES('"+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