File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes JDBC Problem with Date insertion Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "JDBC Problem with Date insertion" Watch "JDBC Problem with Date insertion" New topic
Author

JDBC Problem with Date insertion

Diwakar
Greenhorn

Joined: May 23, 2005
Posts: 9
Hi Friends,

Please help me out Iam getting the following exception with this java code relating to insertion of date into the data base.

java.sql.Date sqldt=new java.sql.Date(new java.util.Date().getTime());
System.out.println("The SQL Date is "+sqldt);
int i=st.executeUpdate("insert into DiwTest2 values('Daewoo',"+sqldt+")");
System.out.println("The value of i after the statement is executed is "+i);



java.sql.SQLException: [Oracle][ODBC][Ora]ORA-00932: inconsistent datatypes: expected DATE got NUMBER
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6057)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:6214)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:2509)
at sun.jdbc.odbc.JdbcOdbcStatement.execute2(JdbcOdbcStatement.java:421)
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:302)
at DateHelper.main(DateHelper.java:221)
Exception in thread "main"



When I place the date varaible within quotes I get the message "literal does not match format string".


Can anybody help me out !!!
Christian Caesar
Greenhorn

Joined: Jul 27, 2005
Posts: 2
When I am dealing with date values I first convert a date into a string (using the SimpleDateFormat class) and then write it to the database.

Hope that helps.
Dave Salter
Ranch Hand

Joined: Jul 20, 2005
Posts: 292

If you use a PreparedStatement, you can just pass Dates into it without doing any conversions to string or anything like that.
Diwakar
Greenhorn

Joined: May 23, 2005
Posts: 9
Hi Christian,
I got what you had mentioned but how should I deal with this type of Data Types in-compatibility when using EntityBeans; where I want to insert "Date of Birth" as a DATE field into the data base. I had already declared this field (as CMP field) as java.sql.Date.
When java.sql.Date is mapped to DATE in SQL why can't we directly insert the data using the variables directly?? That is using a variable of java.sql.Date to insert date into the data base??
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35241
    
    7
I'm confused. If you are using CMP, why would you need to create SQL Insert statements directly? The EJB container should do this for you.

What value does the insert statement have before you execute it, i.e. what is printed by
System.out.println("insert into DiwTest2 values('Daewoo',"+sqldt+")") ?
That should point to your problem pretty quickly.


Android appsImageJ pluginsJava web charts
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56185
    
  13

"Diwakar k",

You have already received three warnings to change your display name to conform to JavaRanch standards: here, here and here.

Read the JavaRanch Naming Policy and adjust your display name to match it prior to your next post.

Accounts with invalid display names are deleted.

bear
Forum bartender


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC Problem with Date insertion
 
Similar Threads
Error
java.sql.SQLException: Invalid handle
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Opti onal fature not implem
please clear me to display
Simple JDBC question