- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Example
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(rs.next()) { 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")); System.out.println(); } } }
Output
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
- Related Articles
- How can we retrieve a blob datatype from a table using the getBinaryStream() method in JDBC?
- How can we set time in a table in JDBC?
- How can we retrieve file from database using JDBC?
- How to retrieve Date from a table in JDBC?
- How to retrieve binary data from a table using JDBC?
- How to retrieve a DATALINK object from a table using JDBC?
- How to retrieve a record from an existing table in a database using JDBC API?
- How to retrieve a record from an existing table in oracle database using JDBC API?
- How to retrieve particular columns of a table using JDBC program?
- Write an JDBC example to retrieve Clob value from a table using the getCharacterStream() method?
- How can we remove a column from MySQL table?
- How to retrieve multiple ResultSets from a stored procedure using a JDBC program?
- How to retrieve table names from a database in MySQL?
- How to retrieve the contents of a ResultSet from last to first in JDBC?
- What is Result in JDBC? How to retrieve data from ResultSet object?
