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 !!!
Joined: Jul 27, 2005
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.
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
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??
Joined: Mar 22, 2005
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.