Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC Problem with Date insertion

 
Diwakar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 293
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a PreparedStatement, you can just pass Dates into it without doing any conversions to string or anything like that.
 
Diwakar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic