Java sql.Time setTime() 方法示例
jdbcjava 8object oriented programmingprogramming更新于 2025/9/4 11:56:17
java.util.Time 类的 setTime() 方法接受一个 long 类型的变量,该变量表示从纪元时间(1970 年 1 月 1 日 00:00:00.000 GMT)到所需时间的毫秒数,并将指定的时间值设置为当前 Time 对象
//设置时间 time.setTime(time_value_in_long);
示例
让我们使用 CREATE 语句在 MySQL 数据库中创建一个名为 dispatches 的表,如下所示 −
CREATE TABLE dispatches( ProductName VARCHAR(255), CustomerName VARCHAR(255), DispatchDate date, DeliveryTime time, Price INT, Location VARCHAR(255));
现在,我们将使用 INSERT 语句向 dispatches 表中插入 5 条记录 −
insert into dispatches values('Key-Board', 'Raja', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'Hyderabad');
insert into dispatches values('Earphones', 'Roja', DATE('2019-05-01'), TIME('11:00:00'), 2000, 'Vishakhapatnam');
insert into dispatches values('Mouse', 'Puja', DATE('2019-03-01'), TIME('10:59:59'), 3000, 'Vijayawada');
insert into dispatches values('Mobile', 'Vanaja', DATE('2019-03-01'), TIME('10:10:52'), 9000, 'Chennai');
insert into dispatches values('Headset', 'Jalaja', DATE('2019-04-06'), TIME('11:08:59'), 6000, 'Goa');
以下 JDBC 示例通过传递所需的值将新记录插入到调度表中。
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
public class Time_setTime {
public static void main(String args[]) throws SQLException {
//注册驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//获取连接
String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("连接已建立......");
//实例化 Time 类
Time time = new Time(0L);
//设置时间
time.setTime(new java.util.Date().getTime());
//Date 对象
Date date = new Date(time.getTime());
//创建预处理语句
String query = "INSERT INTO Dispatches VALUES (?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, "Watch");
pstmt.setString(2, "Rajan");
pstmt.setDate(3, date);
pstmt.setTime(4, time);
pstmt.setInt(5, 4000);
pstmt.setString(6, "Chennai");
pstmt.execute();
System.out.println("Rows inserted ....");
//Retrieving values
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from dispatches");
while(rs.next()) {
System.out.println("Product Name: "+rs.getString("ProductName"));
System.out.println("Customer Name: "+rs.getString("CustomerName"));
System.out.println("Date Of Dispatch: "+rs.getDate("DispatchDate"));
System.out.println("Delivery Time: "+rs.getTime("DeliveryTime"));
System.out.println("Location: "+rs.getString("Location"));
System.out.println();
}
}
}
在此程序中,我们通过将 0L 传递给其构造函数(纪元时间:1970-01-01 05:30:00.0)来实例化 Time 类,并使用 setTime() 方法将时间更改为当前时间。
我们通过传递上面创建的 Time 对象的时间值来创建 Date 对象,并将这些时间和日期值作为新记录的值插入。
输出
连接已建立...... Rows inserted .... Product Name: Key-Board Customer Name: Raja Date of Dispatch: 1970-01-19 Delivery Time: 08:51:36 Location: Hyderabad Product Name: Earphones Customer Name: Roja Date of Dispatch: 1970-01-19 Delivery Time: 05:54:28 Location: Vishakhapatnam Product Name: Mouse Customer Name: Puja Date of Dispatch: 1970-01-19 Delivery Time: 04:26:38 Location: Vijayawada Product Name: Mobile Customer Name: Vanaja Date of Dispatch: 1970-01-19 Delivery Time: 04:26:35 Location: Chennai Product Name: Headset Customer Name: Jalaja Date of Dispatch: 1970-01-19 Delivery Time: 05:19:16 Location: Delhi Product Name: Watch Customer Name: Rajan Date Of Dispatch: 2019-03-28 Delivery Time: 13:08:04 Location: Chennai

