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.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;
         System.out.println("Enter the Employee DOB in the format yyyy-mm-dd : ");
         String dateOfBirth =;
         System.out.println("Enter the Employee Location : ");
         String loc =;
         pstmt.setString(2, name );
         pstmt.setDate(3, Date.valueOf(dateOfBirth));
         pstmt.setString(4, loc);
      System.out.println("data inserted");
      //Creating Statement object
      stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from employee_data");
      //Retrieving values
      while( {
         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"));


Connection established......
table created......
Enter the number of records you need to insert in the table:
Enter the Employee ID:
Enter the Employee name:
Enter the Employee DOB in the format yyyy-mm-dd :
Enter the Employee Location :
Enter the Employee ID:
Enter the Employee name:
Enter the Employee DOB in the format yyyy-mm-dd :
Enter the Employee Location :
Enter the Employee ID:
Enter the Employee name:
Enter the Employee DOB in the format yyyy-mm-dd :
Enter the Employee Location :
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