It's not a secret anymore!
The moose likes JDBC and Relational Databases and the fly likes JDBC Problem with Date insertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "JDBC Problem with Date insertion" Watch "JDBC Problem with Date insertion" New topic

JDBC Problem with Date insertion


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(
at sun.jdbc.odbc.JdbcOdbc.standardError(
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(
at sun.jdbc.odbc.JdbcOdbcStatement.execute2(
at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(
at DateHelper.main(
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

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: 293

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

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

Joined: Mar 22, 2005
Posts: 42965
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.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63867

"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.

Forum bartender

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: JDBC Problem with Date insertion
It's not a secret anymore!