Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBC Dates Deprecated in Java (java.sql package)

Tags:

java

jdbc

I am working with JDBC and MySQL. I have a date column that I need included in my result set. Unfortunately, I cannot find a class in Java to retrieve the date. The SQL Date class is deprecated. How do you get Date objects from a result set?

like image 335
Ryan H Avatar asked Nov 27 '22 08:11

Ryan H


1 Answers

You use java.sql.Date. A constructor and some methods are deprecated. The class isn't. Confused by this versus, say, java.util.Date or java.util.Calendar? Look at Making Sense of Java's Dates.

There are three JDBC date/time types:

  • DATE: granularity of days, use java.sql.Date;
  • TIMESTAMP: date and time, use java.sql.Timestamp;
  • TIME, just the time with no date, use java.sql.Time.

The confusion probably arises from the fact that java.sql.Date extends java.util.Date and thus inherits its deprecated methods.

Just use the right type for the type of your column.

like image 168
cletus Avatar answered Dec 04 '22 12:12

cletus