aspose file tools*
The moose likes Spring and the fly likes How to insert Date data in Oracle database in MM/DD/YYYY HH:MM:SS format? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "How to insert Date data in Oracle database in MM/DD/YYYY HH:MM:SS format?" Watch "How to insert Date data in Oracle database in MM/DD/YYYY HH:MM:SS format?" New topic
Author

How to insert Date data in Oracle database in MM/DD/YYYY HH:MM:SS format?

Tapas
Greenhorn

Joined: Oct 30, 2007
Posts: 2
Through BatchSqlUpdate how to insert date data as MM/DD/YYYY HH:MM:SS?
Here is my code

String insert_Query ="insert into table ("+
"student_id,student_name,admission_date) values (?,?,?)";
BatchSqlUpdate updater = new BatchSqlUpdate(getDataSource(), insert_Query);
updater.declareParameter(new SqlParameter(Types.VARCHAR));
updater.declareParameter(new SqlParameter(Types.VARCHAR));//app_id
updater.declareParameter(new SqlParameter(Types.DATE));
updater.compile();
System.out.println("After updater");

while (rs.next()) {

System.out.println("Inside While");
Object[] values = new Object[3];
values[0] = rs.getString("student_id");
values[1] = rs.getString("student_name");

//Here arrival_time is Epoch time. e.g. value will be like '1316363322','1316377882'
Long dateTemp1 = null;
dateTemp1 = rs.getLong("admission_date");
values[2] = new java.util.Date (dateTemp1*1000);

//With SOP we are getting value as Sun Sep 18 19:52:08 IST 2011 for new java.util.Date (dateTemp1*1000);

//After the insertion into oracle database the value is only 09/18/2011
// How to get the value as 09/18/2011 19:52:08
//In Oracle the admission_date column datatype is Date


System.out.println(new java.util.Date (dateTemp1*1000));
updater.update(values);
}
updater.flush();
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Tapas wrote:updater.declareParameter(new SqlParameter(Types.DATE));

Are you sure this is the correct type? Normally in SQL there are Date, Time, and Timestamp column types. Date produces the results you are seeing.
Tapas
Greenhorn

Joined: Oct 30, 2007
Posts: 2
Hi Paul,

updater.declareParameter(new SqlParameter(Types.DATE));

is the correct type. It is entering Date data into Oracle db table only MM/DD/YYYY data.

I tried

updater.declareParameter(new SqlParameter(Types.TIME));

in place of above statement. Now it is working as expected. Date entered into db as MM/DD/YYYY HH:MM:SS .

Thanks,
Tapas



Zandis Murāns
Ranch Hand

Joined: Aug 18, 2009
Posts: 174

1) Make sure you're inserting correct data format (for example, timestamp for datetime)
2) Make sure that in database this date column is with correct data type for holding both date and time.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

"Tapas", please check your private messages for an important administrative matter. Thanks.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to insert Date data in Oracle database in MM/DD/YYYY HH:MM:SS format?