• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tapas
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20735
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 174
Java Java ME Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Tapas", please check your private messages for an important administrative matter. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic