Java sql.Time toString() 方法示例

jdbcjava 8object oriented programmingprogramming更新于 2025/6/16 9:16:17

java.sql.Time 类的 toString() 方法以字符串变量的形式返回当前 Time 对象时间的 JDBC 转义格式。

也就是说,使用此方法可以将 Time 对象转换为字符串。

//检索 Time 对象
Time timeObj = rs.getTime("DeliveryTime");
//将 Time 对象转换为字符串格式
String time = timeObj.toString();

示例

让我们使用 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.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
public class Time_toString {
   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("连接已建立......");
      //创建 Statement 对象
      Statement stmt = con.createStatement();
      //查询以检索 dispatches 表的内容
      String query = "select * from dispatches";
      //执行查询
      ResultSet rs = stmt.executeQuery(query);
      while(rs.next()) {
         System.out.println("Product Name: "+rs.getString("ProductName"));
         System.out.println("Customer Name: "+rs.getString("CustomerName"));
         System.out.println("Dispatch Date: "+rs.getDate("DispatchDate"));
         Time timeObj = rs.getTime("DeliveryTime");
         //Converting the Time object to String format
         String time = timeObj.toString();
         System.out.println("Delivery time in String format: "+time);
         System.out.println("Location: "+rs.getString("Location"));
         System.out.println();
      }  
   }
}

在此程序中,在检索列值时,我们使用 Time 类的 toString() 方法将交货时间值从 Time 对象转换为字符串格式并显示它。

输出

连接已建立......
Product Name: Key-Board
Customer Name: Raja
Dispatch Date: 2019-09-01
Delivery time in String format: 11:00:00
Location: Hyderabad
Product Name: Earphones
Customer Name: Roja
Dispatch Date: 2019-05-01
Delivery time in String format: 11:00:00
Location: Vishakhapatnam
Product Name: Mouse
Customer Name: Puja
Dispatch Date: 2019-03-01
Delivery time in String format: 10:59:59
Location: Vijayawada
Product Name: Mobile
Customer Name: Vanaja
Dispatch Date: 2019-03-01
Delivery time in String format: 10:10:52
Location: Chennai
Product Name: Headset
Customer Name: Jalaja
Dispatch Date: 2019-04-06
Delivery time in String format: 11:08:59
Location: Goa

相关文章