简体   繁体   中英

how to fix error when inserting DATETIME into db

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();

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

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.

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.

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.

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

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