[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 13317
  • Last Modified:

How to convert java.util.Date to java.sql.Date or java.sql.Timestamp

I have a java.util.Date object stored in one of my objects and I want to insert it into a MS Access database.

patient.getPtDOB() returns a java.util.Date object.  I initially thought that I need to convert it to a java.sql.Date object in order to insert it into my database, but I understand that java.sql.Date does not contain time data.  So, should I convert it to java.sql.Date or java.sql.Timestamp?  Also, how do I do the conversion?

Thank you for your help!!!


***************************
Here is my code:

PreparedStatement ps = null;
String ptFName = patient.getPtFName();
String ptMName = patient.getPtMName();
String ptLName = patient.getPtLName();
Date ptDOB = patient.getPtDOB();

String insert = "INSERT INTO PATIENT (F_NAME, M_NAME, L_NAME, DOB) " + "VALUES (?, ?, ?, ?)";
ps = conn.prepareStatement(insert);
ps.setString(1, ptFName);
ps.setString(2, ptMName);
ps.setString(3, ptLName);
ps.setDate(4, ptDOB);
ps.executeUpdate();
ps.close();
0
akamauu
Asked:
akamauu
  • 2
  • 2
  • 2
  • +1
1 Solution
 
Manikandan ThiagarajanSenior consultantCommented:
0
 
objectsCommented:
> ps.setDate(4, ptDOB);

change that to:

ps.setTimestamp(4, new java.sql.Timestamp(ptDOB.getTime()));
0
 
CEHJCommented:
>>but I understand that java.sql.Date does not contain time data.

Do you need to know what time the patient was born? ;-)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
akamauuAuthor Commented:
Thank you objects, that worked great.  Also, CEHJ, you are right when it comes to the patient's DOB I don't need to know the time, but I am doing the same thing with other java.util.Date objects that I do need to know the time.
0
 
CEHJCommented:
>>Also, CEHJ, you are right when it comes to the patient's DOB I don't need to know the time

Yes, so be careful, since, if you're doing things like a query assembling people of the same age, be careful, as you could get unexpected results
0
 
akamauuAuthor Commented:
Thanks ... I will keep that in mind.
0
 
objectsCommented:
> Thank you objects, that worked great.

no worries :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now