質問

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

役に立ちましたか?

解決 2

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

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top