Inserting date into Java database
Can someone please tell me an elegant way to insert a java date instance into a database?
I am currently using IBATIS with Spring MVC. I have defined the java.util.date property in the bean command and the property editor in the controller to convert the date, however I cannot insert the java.util.date property into the DB.
should I convert this to a java.sql.date instance for storage in the DB?
if i need to convert then where to write the conversion code (DAO, ...)?
I am new to J2EE, so quite confused with processing date objects.
any help?
considers
source to share
I have to say that I'm not familiar with iBATIS ... But by looking at their documentation (page 27), you can add a jdbcType
for your XML mapping and specifically tell the ORM structure which type to use.
source to share
If you are not using Pache Derby / javadb as your database, then the DATE type of the database has nothing to do with the Java Date Class.
Also, there is no standardization of the DATE type between database systems. Grandaddy DB2 uses "yyyy-mm-dd" as the external String format, Sybase and therefore SQLServer and odbc followed this convention. However, DATEFORMAT can be easily overridden in the SQLServer configuration, so it can be almost anything.
The default date format for ORACLEs databases is set at install time and can 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 which target DB2 is expecting.
source to share
There's a good introduction with lots of code examples here: http://www.javalobby.org/articles/ibatis-introduction/
source to share