문제

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