How can we retrieve time from a table in JDBC?

The ResultSet interface provides a method named getTime() 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 time value. To retrieve time 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.

  • 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.

  • Execute the query using the executeQuery() method. Pass the select query to retrieve data (String) as a parameter to it.

  • From the obtained ResultSet object get the time value (along with other values) using the getTime() method of the ResultSet interface. Pass the name of the column (String) as a parameter to this method.


Assume we have a table named dispatches in the database with the following details −

| Product_Name | Date_Of_Dispatch | Time_Of_Dispatch | Location       |
| KeyBoard     | 2019-09-01       | 05:30:00         | Hyderabad      |
| Earphones    | 2019-05-01       | 05:30:00         | Vishakhapatnam |
| Mouse        | 2019-03-01       | 05:29:59         | 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 RetrievingTime {
   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 Dispatches");
      //Retrieving values
      while( {
         System.out.println("Product Name: "+rs.getString("Product_Name"));
         System.out.println("Date Of Dispatch: "+rs.getDate("Date_Of_Dispatch"));
         System.out.println("Date Of Dispatch: "+rs.getTime("Time_Of_Dispatch"));
         System.out.println("Location: "+rs.getString("Location"));


Connection established......
Product Name: KeyBoard
Date Of Dispatch: 2019-09-01
Date Of Dispatch: 05:30:00
Location: Hyderabad
Product Name: Earphones
Date Of Dispatch: 2019-05-01
Date Of Dispatch: 05:30:00
Location: Vishakhapatnam
Product Name: Mouse
Date Of Dispatch: 2019-03-01
Date Of Dispatch: 05:29:59
Location: Vijayawada

Updated on: 30-Jul-2019


Kickstart Your Career

Get certified by completing the course

Get Started