In this tutorial, we are going to describe how to get current TIMESTAMP using JDBC API.
In this tutorial, we are going to describe how to get current TIMESTAMP using JDBC API.In this tutorial, we are going to describe how to get current TIMESTAMP using JDBC API.
Get current TimeStamp :
CURRENT_TIMESTAMP returns the
current date and time .The TIMESTAMP data type provides automatic initialization
and updating to the current date and time (that is, the current timestamp).
It contains date and time information in the following format -
yyyy-mm-dd hh:mm:ss.[fff...]
Example : In this example we are using TimeStamp to display the current timestamp. we are using CURRENT_TIMESTAMP with select statement to display the current time stamp.
package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; class CurrentTimeStamp{ public static void main(String[] args) { System.out.println("Current TimeStamp Example..."); Connection con = null; Statement statement = null; ResultSet rs = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "students"; String driverName = "com.mysql.jdbc.Driver"; String userName = "root"; String password = "root"; try { // Connecting to the database Class.forName(driverName); con = DriverManager.getConnection(url + dbName, userName, password); try { // Creating object of Statement statement = con.createStatement(); // Get current TimeStamp String sql = "SELECT CURRENT_TIMESTAMP"; rs = statement.executeQuery(sql); while (rs.next()) { System.out.println("Current TimeStamp : "+ rs.getTimestamp(1)); } } catch (SQLException e) { System.out.println(e); } con.close(); } catch (Exception e) { e.printStackTrace(); } } }
Output :
Current TimeStamp Example... Current TimeStamp : 2012-09-28 15:44:21.0