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();
akamauuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Manikandan ThiagarajanSenior consultantCommented:
0
objectsCommented:
> ps.setDate(4, ptDOB);

change that to:

ps.setTimestamp(4, new java.sql.Timestamp(ptDOB.getTime()));
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.