如何使用 JDBC 程序在数据库中插入 Timestamp 值?


SQL 中的 Timestamp 数据类型类似于 Date(在 SQL 中),两者都存储日:月:年:时:分:秒。此外,Timestamp 还存储小数秒。

将 Timestamp 插入数据库

PreparedStatement 接口提供了一个名为 setTimestamp() 的方法,该方法接受两个参数:一个整数变量,表示需要存储时间戳的占位符的参数索引;以及一个长整型变量,表示从纪元时间(标准基准时间,即 1970 年 1 月 1 日 00:00:00 GMT)到所需时间的毫秒数。

示例

假设我们在数据库中有一个名为 dispatches 的表,其描述如下:

+------------------+--------------+------+-----+-------------------+
| 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              |
+------------------+--------------+------+-----+-------------------+

如您所见,此表包含一个名为 Time_Of_Dispatch 的列,用于存储时间戳值。

我们可以使用 PreparedStatement 接口的 setTimestamp() 方法将时间戳存储到其中,并使用 ResultSet 接口的 getTimestamp() 方法检索它。

以下 JDBC 程序将记录存储到 dispatches 表中并检索它们:

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://127.0.0.1/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();
      }
   }
}

输出

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

更新于: 2019-07-30

3K+ 次查看

启动你的 职业生涯

通过完成课程获得认证

开始学习
广告