Java sql.Time toString() 方法及示例
在这篇文章中,您将学习如何使用java.sql.Time 类中的toString() 方法将 Time 对象转换为字符串。此方法允许我们将 Time 对象轻松转换为其 JDBC 转义格式,然后可以将其作为字符串处理。
//Retrieving the Time object
Time timeObj = rs.getTime("DeliveryTime");
//Converting the Time object to String format
String time = timeObj.toString();
使用 Java sql.Time toString() 方法的步骤
以下是使用 Java sql.Time toString() 方法的步骤:
- 创建一个名为 dispatches 的 MySQL 表。
- 向表中插入一些示例记录。
- 建立到MySQL 数据库的 JDBC 连接。
- 从 dispatches 表中检索时间值。
- 使用toString() 方法将检索到的 Time 对象转换为字符串。
- 显示转换后的时间以及其他数据。
Java sql.Time 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 程序建立与数据库的连接并检索 dispatches 表的内容。
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 {
//Registering the Driver
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//Getting the connection
String mysqlUrl = "jdbc:mysql:///mydatabase";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Connection established......");
//Creating a Statement object
Statement stmt = con.createStatement();
//Query to retrieve the contents of the dispatches table
String query = "select * from dispatches";
//Executing the query
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() 方法将“Delivery Time”值从 Time 对象转换为字符串格式并显示它。
输出
Connection established...... 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
代码解释
上面给出的程序首先注册MySQL 驱动程序并使用DriverManager建立与数据库的连接。我们创建一个 Statement 对象来执行SQL 查询并使用ResultSet从 dispatches 表中检索数据。当我们到达 DeliveryTime 字段时,我们使用getTime() 方法获取时间作为 Time 对象。
然后使用toString() 方法将此对象转换为字符串,该方法以易于显示或进一步处理的方式格式化时间。最后,将转换后的时间和其他信息打印到控制台。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP