- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to insert Timestamp value in a database using JDBC program?
Timestamp datatype in SQL is similar to Date (in SQL) both store day:month:year:hour:minute:second. In addition to this timestamp stores fractional seconds too.
Inserting Timestamp in to a database
The PreparedStatement interface provides a method named setTimestamp() this method accepts two parameters an integer variable representing the parameter index of the place holder at which you need to store the timestamp and a long variable representing the number of milliseconds from the epoch time(standard base time I.e. January 1, 1970, 00:00:00 GMT) to the required time.
Example
Assume we have a table in the database named dispatches with the following description −
+------------------+--------------+------+-----+-------------------+ | Field | Type | Null | Key | Default | +------------------+--------------+------+-----+-------------------+ | Product_Name | varchar(100) | YES | | NULL | | Name_Of_Customer | varchar(100) | YES | | NULL | | Time_Of_Dispatch | timestamp | NO | | CURRENT_TIMESTAMP | | Location | varchar(100) | YES | | NULL | +------------------+--------------+------+-----+-------------------+
As you can observe this table contains a column named Time_Of_Dispatch which stores a timestamp value.
We can store timestamp in to this using the setTimestamp() method of the PreparedStatement interface and retrieve it using the getTimestamp() method of the ResultSet interface.
Following JDBC program stores records into the dispatches table and retrieves them −
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.sql.Timestamp; public class TimeStampExample { 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 Dispatches VALUES (?, ?, ?, ?)"; PreparedStatement pstmt = con.prepareStatement(query); pstmt.setString(1, "KeyBoard"); pstmt.setString(2, "Amith"); pstmt.setTimestamp(3, new Timestamp(1567296000000L)); pstmt.setString(4, "Hyderabad"); pstmt.execute(); pstmt.setString(1, "Earphones"); pstmt.setString(2, "Sumith"); pstmt.setTimestamp(3, new Timestamp(1556668800000L)); pstmt.setString(4, "Vishakhapatnam"); pstmt.execute(); pstmt.setString(1, "Mouse"); pstmt.setString(2, "Sudha"); pstmt.setTimestamp(3, new Timestamp(1551398399000L)); pstmt.setString(4, "Vijayawada"); pstmt.execute(); System.out.println("Records inserted......"); //Creating Statement object Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from Dispatches"); //Retrieving values while(rs.next()) { System.out.println("Product Name: "+rs.getString("Product_Name")); System.out.println("Name Of The Customer: "+rs.getString("Name_Of_Customer")); System.out.println("Time Of Dispatch: "+rs.getTimestamp("Time_Of_Dispatch")); System.out.println("Location: "+rs.getString("Location")); System.out.println(); } } }
Output
Connection established...... Records inserted...... Product Name: KeyBoard Name Of The Customer: Amith Time Of Dispatch: 2019-09-01 05:30:00.0 Location: Hyderabad Product Name: Earphones Name Of The Customer: Sumith Time Of Dispatch: 2019-05-01 05:30:00.0 Location: Vishakhapatnam Product Name: Mouse Name Of The Customer: Sudha Time Of Dispatch: 2019-03-01 05:29:59.0 Location: Vijayawada
- Related Articles
- How to insert images in Database using JDBC?
- How to insert/store JSON array into a database using JDBC?
- How to insert current date and time in a database using JDBC?
- How to insert a record into a table in a database using JDBC API?
- How to connect to Derby database using a JDBC program?
- How to connect to HSQLDB database using a JDBC program?
- How to connect to PostgreSQL database using a JDBC program?
- How do we insert/store a file into MySQL database using JDBC?
- How to connect to a MongoDB database using a JDBC program?
- How to connect to an SQLite database using a JDBC program?
- How to convert a timestamp object in to Date in JDBC program?
- How to convert a Date object in to Timestamp in JDBC program?
- How to insert Date value into table in JDBC?
- How to drop a database using JDBC API?
- How to insert an image in to Oracle database using Java program?
