How to convert a String into a Date object using JDBC API?


The valueOf() method of the Date object accepts a String value representing a Date in JDBC escape format i.e. yyyy-mm-dd and converts the given String value into java.sql.Date object.

Date date = Date.valueOf(“date_string”);

Assume we have created a table named employee_data with the description as shown below:

+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| id       | int(11)      | YES  |     | NULL    |       |
| Name     | varchar(255) | YES  |     | NULL    |       |
| Dob      | date         | YES  |     | NULL    |       |
| Location | varchar(255) | YES  |     | NULL    |       |
+----------+--------------+------+-----+---------+-------+

Following JDBC program accepts id (integer), name(String), date of birth(String), and, location(String) of the employees, converts the date of birth value passed in JDBC escape syntax format to Date object and inserts the given details in to the employee_data table. At the end it retrieves all the records in the table once and displays.

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;
public class StringtoDate {
   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/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating a Statement object
      Statement stmt = con.createStatement();
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the number of records you need to insert: ");
      int num = sc.nextInt();
      //Inserting values to the table
      String query = "INSERT INTO employee_data VALUES (?, ?, ?, ?)";
      PreparedStatement pstmt = con.prepareStatement(query);
      for(int i=1; i<=num; i++) {
         System.out.println("Enter the Employee ID: ");
         int id = sc.nextInt();
         System.out.println("Enter the Employee name: ");
         String name =sc.next();
         System.out.println("Enter the Employee DOB in the format yyyy-mm-dd : ");
         String dateOfBirth = sc.next();
         System.out.println("Enter the Employee Location : ");
         String loc = sc.next();
         pstmt.setInt(1,id);
         pstmt.setString(2, name );
         pstmt.setDate(3, Date.valueOf(dateOfBirth));
         pstmt.setString(4, loc);
         pstmt.executeUpdate();
      }
      System.out.println("data inserted");
      //Creating Statement object
      stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from employee_data");
      //Retrieving values
      while(rs.next()) {
         System.out.println("Employee_Id: "+rs.getInt("ID"));
         System.out.println("Employee_Name: "+rs.getString("Name"));
         System.out.println("Employee_DOB: "+rs.getInt("DOB"));
         System.out.println("Employee_Location: "+rs.getString("Location"));
         System.out.println();
      }
   }
}

Output

Connection established......
table created......
Enter the number of records you need to insert in the table:
3
Enter the Employee ID:
1001
Enter the Employee name:
Krishna
Enter the Employee DOB in the format yyyy-mm-dd :
1989-09-26
Enter the Employee Location :
Hyderabad
Enter the Employee ID:
1002
Enter the Employee name:
Kasyap
Enter the Employee DOB in the format yyyy-mm-dd :
1990-06-25
Enter the Employee Location :
Vishakhapatnam
Enter the Employee ID:
1003
Enter the Employee name:
Maruthi
Enter the Employee DOB in the format yyyy-mm-dd :
1995-06-06
Enter the Employee Location :
Vijayawada
data inserted
Employee_Id: 1001
Employee_Name: Krishna
Employee_DOB: 1989
Employee_Location: Hyderabad

Employee_Id: 1002
Employee_Name: Kasyap
Employee_DOB: 1990
Employee_Location: Vishakhapatnam

Employee_Id: 1003
Employee_Name: Maruthi
Employee_DOB: 1995
Employee_Location: Vijayawada

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements