如何在 JDBC 程序中将 Date 对象转换为 Timestamp?

jdbcjava 8object oriented programmingprogramming

java.sql.Date 类的 getTime() 方法检索并返回当前时间戳的时间(以毫秒为单位)(长整型),从纪元时间 1,1970 00:00:00.000 GMT 开始。

//检索日期
Date date = rs.getDate("Dispatch_Date");

java.sql.Timestamp 类的构造函数接受一个长整型变量,该变量表示从纪元时间开始的毫秒数,并构造 Timestamp 对象。

//创建 Timestamp 对象。
Timestamp ts = new Timestamp(date.getTime()));

使用这些,您可以在 JDBC 中将 Date 对象转换为 TimeStamp 对象。

假设我们已经与 MySQL 数据库建立了连接,并使用语句对象创建了一个名为 dispatch_data 的表:

假设我们已经与 MySQL 数据库建立了连接,并使用语句对象创建了一个名为 dispatch_data 的表:

//创建 Statement 对象
Statement stmt = con.createStatement();

//查询以创建表
String create_query = "Create table dispatch_data ("
   + "Product_Name VARCHAR(255), "
   + "Name_Of_Customer VARCHAR(255) , "
   + "Dispatch_Date date, "
   + "Location VARCHAR(255) )";
stmt.execute(create_query);
System.out.println("table created......");

我们使用 PreparedStatement 填充了表格,如下所示:

//向表格中插入值
String query = "INSERT INTO dispatch_data VALUES (?, ?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);

pstmt.setString(1, "KeyBoard");
pstmt.setString(2, "Amith");
pstmt.setDate(3, new Date(376401869000L));
pstmt.setString(4, "Hyderabad");
pstmt.execute();

pstmt.setString(1, "Ear phones");
pstmt.setString(2, "Sumith");
pstmt.setDate(3, new Date(356788333000L));
pstmt.setString(4, "Vishakhapatnam");
pstmt.execute();

pstmt.setString(1, "Mouse");
pstmt.setString(2, "Sudha");
pstmt.setDate(3, new Date(594733933000L));
pstmt.setString(4, "Vijayawada");
pstmt.execute();

System.out.println("Records inserted......");

以下 JDBC 程序从 ResultSet 中检索日期值并将其转换为 Timestamp 对象并打印详细信息。

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Timestamp;
public class DateToTimeStamp {
   public static void main(String args[])throws Exception {
      //注册驱动程序
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //获取连接
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("连接已建立......");
      //创建 Statement 对象
      Statement stmt = con.createStatement();
      //创建 Statement 对象
      stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select * from dispatch_data");
      //检索值
      while(rs.next()) {
         System.out.println("Product Name: "+rs.getString("Product_Name"));
         System.out.println("Name Of The Customer: "+rs.getString("Name_Of_Customer"));
         //检索日期
         Date date = rs.getDate("Dispatch_Date");
         //打印发货时间
         System.out.println("Dispatch_Timestamp: "+new Timestamp(date.getTime()));
         System.out.println();
      }
   }
}

输出

Connection established......
Product Name: KeyBoard
Name Of The Customer: Amith
Dispatch_Timestamp: 1981-12-05 00:00:00.0

Product Name: Ear phones
Name Of The Customer: Sumith
Dispatch_Timestamp: 1981-04-22 00:00:00.0

Product Name: Mouse
Name Of The Customer: Sudha
Dispatch_Timestamp: 1988-11-05 00:00:00.0

相关文章