views:

396

answers:

3

hi,

can someone please tell me the elegant way of inserting a java date instance into database?

presently, i am using IBATIS with Spring MVC . i have defined a java.util.date property in the command bean and property editor in the controller for Date conversion, however i am unable to insert java.util.date property in DB .

should i convert this into java.sql.date instance inorder to store in DB?

if i have to convert, then where to write the code of conversion(DAO,...)?

i am new to J2EE, so pretty confused with handling date objects.

any help?

regards

+1  A: 

Unless you are using Pache Derby/javadb as your database then the Database's DATE type has nothing to do with Java's Date Class.

Furthermore there is no standarisation of the DATE type between Database systems. The grandaddy DB2 uses 'yyyy-mm-dd' as the external String format, this convention was followed by Sybase and hence SQLServer and odbc. However DATEFORMAT can be easily overridden in the SQLServer configuration so it could end up as almost anything.

An ORACLEs databases default date format is set at installation time and could be almost anything depending on the whim of the installer.

Most ORMs (not too familiar with IBATIS) can be configured to handle these date conversions, once you know waht the target DB2 expects.

James Anderson
thank you james,i think, ibatis do not provide configuration for date conversions.so, what to do with java.sql.date class is it necessary to convert java.util.date to sql.date in order to store it in DB?
ernesto
BTW i mean database by DB it's not DB2. sorry!
ernesto
A: 

There's a good introduction with lots of sample code here: http://www.javalobby.org/articles/ibatis-introduction/

Philip Davis
+1  A: 

I have to say I'm not familiar with iBATIS... But looking through their documentation (page 27), you can add a jdbcType attribute to your XML mapping and specifically tell the ORM framework what type to use.

Yuval
hey, it,s working man many thanks.
ernesto