Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java sql.Time valueOf() method with example
In this program, we'll demonstrate how to insert and retrieve records from a MySQL database using JDBC . Specifically, we'll focus on how to convert a time value from a String format into a java.sql.Time object using the valueOf() method. This allows us to store time values in a database and retrieve them accurately.
Time time = Time.valueOf("time_string");
Steps to Use sql.Time valueOf() Method
Following are the steps to use sql.Time valueOf() method ?
- Import all the useful classes.
- Register the Driver by loading the MySQL JDBC driver.
- Establish a connection by connecting to the MySQL database using DriverManager.
- Prepare SQL query and define an SQL query to insert data into the dispatches table.
- Convert and insert time convert the time string to a Time object using Time.valueOf() and insert it.
- Execute the query by running the query to insert the record.
- Retrieve and display records by executing a query to retrieve all records and print them.
Inserting and retrieving records using Java sql.Time valueOf() method
Let us create a table with name dispatches in MySQL database using the CREATE statement as follows ?
CREATE TABLE dispatches( ProductName VARCHAR(255), CustomerName VARCHAR(255), DispatchDate date, DeliveryTime time, Price INT, Location VARCHAR(255));
Now, we will insert 5 records in dispatches table using INSERT statements ?
insert into dispatches values('Key-Board', 'Raja', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'Hyderabad');
insert into dispatches values('Earphones', 'Roja', DATE('2019-05-01'), TIME('11:00:00'), 2000, 'Vishakhapatnam');
insert into dispatches values('Mouse', 'Puja', DATE('2019-03-01'), TIME('10:59:59'), 3000, 'Vijayawada');
insert into dispatches values('Mobile', 'Vanaja', DATE('2019-03-01'), TIME('10:10:52'), 9000, 'Chennai');
insert into dispatches values('Headset', 'Jalaja', DATE('2019-04-06'), TIME('11:08:59'), 6000, 'Goa');
Following the JDBC program establishes a connection with the database and inserts a new record into the dispatches table.
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
public class Time_valueOf {
public static void main(String args[]) throws SQLException {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Inserting values to a table
String query = "INSERT INTO dispatches VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, "Watch");
pstmt.setString(2, "Rajan");
pstmt.setDate(3, new Date(1567315800000L));
Time time = Time.valueOf("10:59:59");
pstmt.setTime(4, time);
pstmt.setInt(5, 4000);
pstmt.setString(6, "Chennai");
pstmt.execute();
//Retrieving data
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from dispatches");
while(rs.next()) {
System.out.print("Name: "+rs.getString("ProductName")+", ");
System.out.print("Customer Name: "+rs.getString("CustomerName")+", ");
System.out.print("Dispatch Date: "+rs.getDate("DispatchDate")+", ");
System.out.print("Delivery Time: "+rs.getTime("DeliveryTime")+", ");
System.out.print("Price: "+rs.getInt("Price")+", ");
System.out.print("Location: "+rs.getString("Location"));
System.out.println();
}
}
}
Here, in this program, we are taking the time value in String format and converting it into the java.util.Time object using the valueOf() method.
Output
Connection established...... Name: Key-Board, Customer Name: Raja, Dispatch Date: 2019-09-01, Delivery Time: 11:00:00, Price: 7000, Location: Hyderabad, Name: Earphones, Customer Name: Roja, Dispatch Date: 2019-05-01, Delivery Time: 11:00:00, Price: 2000, Location: Vishakhapatnam, Name: Mouse, Customer Name: Puja, Dispatch Date: 2019-03-01, Delivery Time: 10:59:59, Price: 3000, Location: Vijayawada, Name: Mobile, Customer Name: Vanaja, Dispatch Date: 2019-03-01, Delivery Time: 10:10:52, Price: 9000, Location: Chennai, Name: Headset, Customer Name: Jalaja, Dispatch Date: 2019-04-06, Delivery Time: 11:08:59, Price: 6000, Location: Goa, Name: Watch, Customer Name: Rajan, Dispatch Date: 2019-09-01, Delivery Time: 10:59:59, Price: 4000, Location: Chennai