Insert date in different formats - Java Program

Q. Write a program to insert the date value in different format in oracle database.  

Answer:

The Date class is available in java.util package, it encapsulates the current date and time. The class Date represents a specific instant in time, with millisecond precision.

The SQL also has the date datatype to insert the date and time. There are different date formats that come standard in SQL Server as part of the CONVERT function. 

Create table

Create table person_tab(id number, name varchar2(20), dob date, doj date)

DateInsert .java

import java.util.*;
import java.sql.*;
import java.text.*;
class DateInsert  
{
     public static void main(String[] args) throws Exception
     {
          //read input value from keyboard
          Scanner sc = new Scanner(System.in);
          System.out.println("Enter the person id: ");
          int id = sc.nextInt();
          System.out.println("Enter the person name: ");
          String name = sc.next();
          System.out.println("Enter the date of birth: ");
          String dob = sc.next();
          System.out.println("Enter the date of joining: ");
          String doj = sc.next();
          //convert String date value to java.lang.Date class
          //for date of birth
          SimpleDateFormat sdf1 = new SimpleDateFormat("dd-mm-yy");
          java.util.Date udob = sdf1.parse(dob);
          java.sql.Date sqdob = new java.sql.Date(udob.getTime());
          //for date of joining(yyyy-mm-dd)
          java.sql.Date sqdoj = java.sql.Date.valueOf(doj);
          //create JDBC connection object
          Class.forName("oracle.jdbc.driver.OracleDriver");
          Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","local", "test");
          //create prepared Statement object
          PreparedStatement ps = con.prepareStatement("insert into person_tab values (?,?,?,?)");
          //set values to the parameters query
          ps.setInt(1, id);
          ps.setString(2, name);
          ps.setDate(3, sqdob);
          ps.setDate(4, sqdoj);
          //execute the query
          int res = ps.executeUpdate();
          //processing the result
          if (res == 0)
          {
               System.out.println("Record is not inserted");
          }
          else
               System.out.println("Record is inserted");
          //close jdbc objects
          ps.close();
          con.close();
     }
}


Output:

date insert