4

I am trying to to do this:

pr.setStartdate("2006-09-10T00:00:00");

I am getting this error:

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

any ideas on how to successfully insert would be great.

here is a little more code. now do i need to setDate? or does setString work for begintime, endtime, and date? they are all DATETIME objects:

PreparedStatement pstmt = conn.prepareStatement("UPDATE Event SET date=?, begintime=?, endtime=?, status=?, productionid=?, conceptual_packageid=? WHERE id=?");
        pstmt.setString(1, pkg.getDate());
        pstmt.setString(2, pkg.getBeginTime());
        pstmt.setString(3, pkg.getEndTime());
        pstmt.setString(4, pkg.getStatus());
        pstmt.setString(5, pkg.getProductionID());
        pstmt.setString(6, pkg.getConceptual_PackageID());
        pstmt.setString(7, pkg.getId());

        pstmt.executeUpdate();
        pstmt.close();
6
  • Try passing date parameter's in single quotes. Commented Feb 10, 2011 at 3:42
  • I have tried that, but I get error lines saying, "unclosed character literal, not a statement, ';' excpected. I am pretty stumped here and it seems so simple Commented Feb 10, 2011 at 3:44
  • what is 'pr'? I don't think it is a prepared statement Commented Feb 10, 2011 at 3:48
  • pr is an object that I save in the db Commented Feb 10, 2011 at 3:49
  • then we need to see the code for pr class and also the jdbc code Commented Feb 10, 2011 at 3:57

5 Answers 5

6

I'd suggest you use setTimestamp(...) and setDate(...) methods of PreparedStatement and then pass in the actual Date objects to them rather than working with strings. If you are limited to working with Strings in the "pr" class then convert the Strings to the format specified in the derby doc and then use the below code

java.sql.Timestamp.valueOf("time/date converted");
Sign up to request clarification or add additional context in comments.

1 Comment

Thus: ps.setTimestamp(Timestamp.valueOf("2006-09-10 00:00:00"));
4

From what I can tell, derby has no "DATETIME" data type, they support DATE, TIME, and TIMESTAMP.

http://db.apache.org/derby/docs/10.10/ref/crefsqlj31068.html

As such I would make sure you're declaring your 'date' column as a TIMESTAMP if you are wanting to keep both the time and date value in that field.

Second I would single quote the column name 'date' as DATE is a reserved data type. I don't know if that would be the problem here but might want to try it.

Thirdly, when setting/getting the 'date' column, I would try to use set/getTimestamp and pass/assign a java.sql.Timestamp object where applicable.

Hope that provides some help.

Comments

2

Dates, times, and timestamps cannot be mixed with one another in expressions. Derby supports the following formats for TIMESTAMP:

yyyy-mm-dd hh:mm:ss[.nnnnnn]
yyyy-mm-dd-hh.mm.ss[.nnnnnn]

The year must always have four digits. Months, days, and hours may have one or two digits. Minutes and seconds must have two digits. Nanoseconds, if present, may have between one and six digits.

So, you should replace 'T' with space or hyphen.

Comments

1

In case someone else runs across this months-old question (as I did) doing a similar but not identical operation: Look carefully at the two formats in Lev Khomich's answer: there is a space between day/hour with colons between the hour/min, and a dash between day/hour with periods between hour/min. I just had a case, using a SQL statement with periods between hour/min, where a space between day/hour caused an error but a dash did not.

I'm used to DB2, which accepts either a dash or a space when using periods between hour/min and min/sec, making it even easier to overlook. I was just assuming either space or dash was allowed between day/hour.

2 Comments

I get error that the timestamp is not in the right format: can you please tell me what is wrong? Here is my INSERT stt: INSERT INTO APP.MYTABLE VALUES ('2012-09-10 08:32:22[.1020]');
I don't have any testing environment set up to test this, but I know of no DB that allows the square brackets in the date format. The square brackets are fairly standard notation in describing the format, indicating that the portion within the brackets is optional. But I think your problem is likely that the brackets cannot actually appear. Just take out [ and ].
0

I had the same issue, then I found the solution here:

http://apache-database.10148.n7.nabble.com/Date-Timestamp-format-for-inserts-td99979.html

Basically you have to use this format:

yyyy-mm-dd-hh.mm.ss[.nnnnnn]

For instances, this insert works for me:

statement.executeUpdate("INSERT INTO animal VALUES (1, 1, 'Elsa', '2006-09-10-00.00.00')");

Comments

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.