Java sql.Timestamp valueOf() method with example

JDBCJava 8Object Oriented ProgrammingProgramming

The valueOf() method of the java.sql.Timestamp class accepts a String value representing a time stamp in JDBC escape format and converts the given String value into Timestamp object.

Timestamp timeStamp = Time.valueOf("timeStamp_string");


Let us create a table with name dispatches_data in MySQL database using CREATE statement as shown below:

CREATE TABLE dispatches_data(
   ProductName VARCHAR(255),
   CustomerName VARCHAR(255),
   DispatchTimeStamp timestamp,
   Price INT,
   Location VARCHAR(255));

Now, we will insert 5 records in dispatches_data table using INSERT statements:

insert into dispatches_data values('Key-Board', 'Raja', TIMESTAMP('2019-05-04', '15:02:45'), 7000, 'Hyderabad');
insert into dispatches_data values('Earphones', 'Roja', TIMESTAMP('2019-06-26', '14:13:12'), 2000, 'Vishakhapatnam');
insert into dispatches_data values('Mouse', 'Puja', TIMESTAMP('2019-12-07', '07:50:37'), 3000, 'Vijayawada');
insert into dispatches_data values('Mobile', 'Vanaja' , TIMESTAMP ('2018-03-21', '16:00:45'), 9000, 'Chennai');
insert into dispatches_data values('Headset', 'Jalaja' , TIMESTAMP('2018-12-30', '10:49:27'), 6000, 'Goa');

Following JDBC program establishes connection with the database and inserts a new record in to the dispatches_data table.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
public class Timestamp_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_data VALUES (?, ?, ?, ?, ?)";
      PreparedStatement pstmt = con.prepareStatement(query);
      pstmt.setString(1, "Watch");
      pstmt.setString(2, "Rajan");
      Timestamp timestamp = Timestamp.valueOf("2019-04-21 14:17:02.0");
      pstmt.setTimestamp(3, timestamp);
      pstmt.setInt(4, 4000);
      pstmt.setString(5, "Chennai");
      //Retrieving data
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from dispatches_data");
      while( {
         System.out.print("Name: "+rs.getString("ProductName")+", ");
         System.out.print("Customer Name: "+rs.getString("CustomerName")+", ");
         System.out.print("Dispatch Time Stamp: "+rs.getDate("DispatchTimeStamp")+", ");
         System.out.print("Price: "+rs.getInt("Price")+", ");
         System.out.print("Location: "+rs.getString("Location"));

Here, in this program we are taking the time stamp value in String format and converting it into the java.util.Time object using the valueOf() method.


Connection established......
Name: Key-Board, Customer Name: Raja, Dispatch Time Stamp: 2019-05-04, Price: 7000, Location: Hyderabad
Name: Earphones, Customer Name: Roja, Dispatch Time Stamp: 2019-06-26, Price: 2000, Location: Vishakhapatnam
Name: Mouse, Customer Name: Puja, Dispatch Time Stamp: 2019-12-07, Price: 3000, Location: Vijayawada
Name: Mobile, Customer Name: Vanaja, Dispatch Time Stamp: 2018-03-21, Price: 9000, Location: Chennai
Name: Headset, Customer Name: Jalaja, Dispatch Time Stamp: 2018-12-30, Price: 6000, Location: Goa
Name: Watch, Customer Name: Rajan, Dispatch Time Stamp: 2019-04-21, Price: 4000, Location: Chennai
Updated on 30-Jul-2019 22:30:26