如何使用 JDBC 程序将时间戳值插入数据库?

jdbcjava 8object oriented programmingprogramming

SQL 中的时间戳数据类型类似于 SQL 中的日期类型,两者都存储日:月:年:时:分:秒。此外,时间戳还存储秒的小数部分。

将时间戳插入数据库

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 程序将记录存储到调度表中并检索它们 −

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 {
      //获取连接
      String mysqlUrl = "jdbc:mysql://localhost/sampledb";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("连接已建立......");
      //向表中插入值
      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......");
      //创建 Statement 对象
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from Dispatches");
      //检索值
      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

相关文章