How to insert current date and time in a database using JDBC?


The time stamp dataType in MySQL database stores day, month, year, hour, minute, second, fractional seconds. Using this you can represent date and time at once.

There are two ways to insert/get current time stamp values while working with JDBC.

  • Using the database default value for a date.

  • Using the getTime() method of the calendar class.

Database default value

Create a table named sample to store time stamps in MySQL database using the following query:

CREATE TABLE Sample(Id INT, Current_Time_Stamp TimeStamp);

Now describe the table as shown below:

+--------------------+-----------+------+-----+-------------------+
| Field              | Type      | Null | Key | Default           |
+--------------------+-----------+------+-----+-------------------+
| Id                 | int(11)   | YES  |     | NULL              |
| Current_Time_Stamp | timestamp | NO   |     | CURRENT_TIMESTAMP |
+--------------------+-----------+------+-----+-------------------+

If you observe, the default value of the time stamp is current timestamp of the system. i.e. if you do not pass any value to this column by default it is filled with the current timestamp value.

Therefore, while inserting value into a table which has time stamp using JDBC program, in the Prepared statement leave the column of time stamp or, simply use CURRENT_TIMESTAMP instead of the place holder ? this will be filled with the current timestamp value.

Example

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class InsertingCurrentTime {
   public static void main(String args[])throws Exception {
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Inserting values to a table

      String query = "INSERT INTO sample(ID, Current_Time_Stamp)
      VALUES (?, CURRENT_TIMESTAMP)";

      PreparedStatement pstmt = con.prepareStatement(query);
      pstmt.setInt(1, 1);
      pstmt.execute();
      System.out.println("Data inserted......");
   }
}

Output

Data inserted......

If you observe the contents of the table you can see it with the current time stamp value as:

+------+---------------------+
| Id   | Current_Time_Stamp  |
+------+---------------------+
| 1    | 2019-02-27 17:18:17 |
+------+---------------------+

The getTime() method of Calendar class

The getTime() method of the Calendar class returns an Object of the Date class which holds the current time value of the calendar.

Calendar calendar = Calendar.getInstance();
java.util.Date currentTime = calendar.getTime();

The java.util.Date class also provides a method named getTime() this method returns a variable of type long which represents the number of milliseconds from the standard epoch time (since January 1, 1970, 00:00:00 GMT) to the current time value of this object.

long time = currentTime.getTime();

Now, pass this obtained current time value to the setTimedtamp() method as one parameter where, the other one will be an integer representing the parameter index of the place holder ? to which you need to set the current timestamp value.

pstmt.setTimestamp(2, new Timestamp(time));

Updated on: 30-Jul-2019

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements