如何使用 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:///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+ 次檢視

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.