Domanda

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

È stato utile?

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

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top