How to Insert timestamp in java Jdbc example


In this post we will Insert timestamp in Jdbc with example/program in java
DATE and TIMESTAMP both allows you to store DAY:MONTH:YEAR HOUR:MINUTE:SECOND


DATE and TIMESTAMP provides 7 bytes for storing all the above mentioned data.

But, TIMESTAMP additionally allows you to store FRACTIONAL SECONDS.
TIMESTAMP provides 11 bytes for storing FRACTIONAL SECONDS.
TIMESTAMP provides additional 2 bytes for storing TIMEZONE as well.

When to use DATE and TIMESTAMP?
If we just need to store time without much precision you must go for DATE.
But, if you need time to be stored with lot of precision where even fractional seconds could matter.
Example - In casinos, bidding and jackpot games where even fractional seconds could matter.


--Before executing java program execute these database scripts  >

create table EMPLOYEE(NAME varchar2(22), CREATION_DATE TIMESTAMP);

--If table already exists then execute the DROP command >
drop table EMPLOYEE;


For inserting timestamp in Jdbc use -
new java.sql.Timestamp(new java.util.Date().getTime())


Example/program to Insert timestamp in Jdbc java >
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class InsertTimeStampJdbcExample {
   public static void main(String... arg) {
          Connection con = null;
          PreparedStatement prepStmt = null;
          try {
                 // registering Oracle driver class
                 Class.forName("oracle.jdbc.driver.OracleDriver");
                 // getting connection
                 con = DriverManager.getConnection(
                              "jdbc:oracle:thin:@localhost:1521:orcl", "ankit",
                              "Oracle123");
                 System.out.println("Connection established successfully!");
                 prepStmt = con
                              .prepareStatement("INSERT into EMPLOYEE (NAME, CREATION_DATE) "
                                            + "values (?, ?) ");
                 prepStmt.setString(1, "ankit");
                 prepStmt.setTimestamp(2,
                             new java.sql.Timestamp(new java.util.Date().getTime()));
                 // execute insert query
                 int numberOfRowsInserted = prepStmt.executeUpdate();
                 System.out.println("numberOfRowsInserted=" + numberOfRowsInserted);
                 prepStmt.close(); // close PreparedStatement
                 con.close(); // close connection
          } catch (ClassNotFoundException e) {
                 e.printStackTrace();
          } catch (SQLException e) {
                 e.printStackTrace();
          }
   }
}
/*OUTPUT
Connection established successfully!
numberOfRowsInserted=1
*/

Using above program we could Insert timestamp in Jdbc in java



Let’s execute Sql query to fetch inserted timestamp from EMPLOYEE table in Oracle >

SELECT TO_CHAR(CREATION_DATE,'MM/DD/YYYY HH24:MI:SS:FF3') CREATION_DATE  FROM employee;


SELECT CREATION_DATE  FROM employee;

How to convert DATE to TIMESTAMP?
SELECT CAST(CREATION_DATE AS TIMESTAMP) CREATION_DATE  FROM employee;

Labels: Core Java JDBC
eEdit
Must read for you :