This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes Issue with Date insertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Issue with Date insertion" Watch "Issue with Date insertion" New topic
Author

Issue with Date insertion

K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
Hi,

I am trying to insert a date field to MS Access 2002 *.mdb file.
Below is the code I am using and my table structure is very simple
TEST12345(ff) -- the ff column is of type Date/Time



Whether I use createStatement or PrepareStatement, I get the below exception

java.sql.SQLException: General error
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(Unknown Source)
at com.smart.corp.dao.UpdateDAO.main(UpdateDAO.java:158)

Not sure where I am wrong. There is no problem with the connection object since I have created tables with it successfully. Any help on my issue would be highly appreciated.

Regards,
Kiran.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Seems to me that if the field's type is "Date/Time" then you should be using a Java class which matches that type. That would be java.sql.Timestamp and not java.sql.Date (which doesn't have a Time component).
K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
Thanks Paul. Timestamp worked for me only with PreparedStatement but not with Statement.
Am I missing anything in Statement? Please let me know. Below is mycode.


java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression '2009-10-08 11:41:38.984'.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
at com.smart.corp.dao.UpdateDAO.main(UpdateDAO.java:201)
java.lang.NullPointerException
at com.smart.corp.dao.UpdateDAO.main(UpdateDAO.java:217)



Regards,
Kiran.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

How would you insert this record if you would use an sql query tool?
How would you format the timestamp then?

Compare that with how your statement looks like in your javacode (Do System.out.println("insert into TEST12345(ff) values ("+ts+")");


OCUP UML fundamental and ITIL foundation
youtube channel
K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
Hi,
How would you insert this record if you would use an sql query tool?
I use TO_DATE('12-08-2009','DD-MM-YYYY') function.
How would you format the timestamp then?
In java there is no TO_DATE type method and hence trying to insert the Object.

The output for "insert into TEST12345(ff) values ("+ts+")"); is
insert into TEST12345(ff) values (2009-10-08 11:41:38.984)

Thanks for the suggestion.

Regards,
Kiran.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue with Date insertion
 
Similar Threads
How to read/write text file to db2 by using java code
Help with MS Access & Date
StreamCorruptedException
Getting error while inserting data into table
Insert date and time into Oracle database