How to retrieve Date from a table in JDBC?

The ResultSet interface provides a method named getDate() this method accepts an integer parameter representing the index of the column, (or, a String parameter representing the name of the column) from which you need to retrieve the date value. To retrieve date value from a table −
Register the driver class using the registerDriver() method of the DriverManager class. Pass the driver class name to it, as parameter.
Register the driver class using the registerDriver() method of the DriverManager class. Pass the driver class name to it, as parameter.
Connect to the database using the getConnection() method of the DriverManager class. Passing URL (String), username (String), password (String) as parameters to it.
Connect to the database using the getConnection() method of the DriverManager class. Passing URL (String), username (String), password (String) as parameters to it.
Create a Statement object using the createStatement() method of the Connection interface.
Create a Statement object using the createStatement() method of the Connection interface.
Execute the query using the executeQuery() method. Pass the select query to retrieve data (String) as a parameter to it.
Execute the query using the executeQuery() method. Pass the select query to retrieve data (String) as a parameter to it.
From the obtained result set object get the date value (along with other values) using the getDate() method of the ResultSet interface. Pass the name of the column (String) as a parameter to this.
From the obtained result set object get the date value (along with other values) using the getDate() method of the ResultSet interface. Pass the name of the column (String) as a parameter to this.
Assume we have a table named Emp in the database as shown below −
+--------+------------+----------------+
| Name   | DOB        | Location       |
+--------+------------+----------------+
| Amit   | 1989-09-26 | Hyderabad      |
| Sumith | 1989-09-01 | Vishakhapatnam |
| Sudha  | 1980-09-01 | Vijayawada     |
+--------+------------+----------------+
Following is a JDBC example which retrieves the Date and String values from a table using getDate() and getString() methods of the ResultSet interface.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class RetrievingDate {
   public static void main(String args[])throws Exception {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating Statement object
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from Emp");
      //Retrieving values
      while(rs.next()) {
         System.out.print("Name: "+rs.getString("Name"));
         System.out.print(" Date of Birth: "+rs.getDate("DOB"));
         System.out.print(" Location: "+rs.getString("Location"));
         System.out.println();
      }
   }
}
Connection established......
Name: Amit Date Of Birth: 1989-09-26 Location: Hyderabad
Name: Sumith Date Of Birth: 1989-09-01 Location: Vishakhapatnam
Name: Sudha Date Of Birth: 1980-09-01 Location: Vijayawada