Java sql.Time valueOf() 方法及示例
在本程序中,我们将演示如何使用 MySQL 数据库 和 JDBC 插入和检索记录。具体来说,我们将重点介绍如何使用 valueOf() 方法 将字符串格式的时间值转换为 **java.sql.Time 对象**。这使我们能够将时间值存储到数据库中并准确地检索它们。
Time time = Time.valueOf("time_string");
使用 sql.Time valueOf() 方法的步骤
以下是使用 sql.Time valueOf() 方法的步骤:
- 导入所有有用的类。
- 通过加载 MySQL JDBC 驱动程序来注册驱动程序。
- 通过使用 DriverManager 连接到 MySQL 数据库来建立连接。
- 准备 SQL 查询并定义一个 SQL 查询以将数据插入到 dispatches 表中。
- 转换和插入时间 使用 Time.valueOf() 将时间字符串转换为 **Time 对象** 并插入。
- 执行查询 通过运行查询来插入记录。
- 检索和显示记录 执行查询以检索所有记录并打印它们。
使用 Java sql.Time valueOf() 方法插入和检索记录
让我们使用 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.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_valueOf { 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://127.0.0.1/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Inserting values to a table String query = "INSERT INTO dispatches VALUES (?, ?, ?, ?, ?, ?)"; PreparedStatement pstmt = con.prepareStatement(query); pstmt.setString(1, "Watch"); pstmt.setString(2, "Rajan"); pstmt.setDate(3, new Date(1567315800000L)); Time time = Time.valueOf("10:59:59"); pstmt.setTime(4, time); pstmt.setInt(5, 4000); pstmt.setString(6, "Chennai"); pstmt.execute(); //Retrieving data Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from dispatches"); while(rs.next()) { System.out.print("Name: "+rs.getString("ProductName")+", "); System.out.print("Customer Name: "+rs.getString("CustomerName")+", "); System.out.print("Dispatch Date: "+rs.getDate("DispatchDate")+", "); System.out.print("Delivery Time: "+rs.getTime("DeliveryTime")+", "); System.out.print("Price: "+rs.getInt("Price")+", "); System.out.print("Location: "+rs.getString("Location")); System.out.println(); } } }
在这里,在这个程序中,我们使用 valueOf() 方法将字符串格式的时间值转换为 java.util.Time 对象。
输出
Connection established...... Name: Key-Board, Customer Name: Raja, Dispatch Date: 2019-09-01, Delivery Time: 11:00:00, Price: 7000, Location: Hyderabad, Name: Earphones, Customer Name: Roja, Dispatch Date: 2019-05-01, Delivery Time: 11:00:00, Price: 2000, Location: Vishakhapatnam, Name: Mouse, Customer Name: Puja, Dispatch Date: 2019-03-01, Delivery Time: 10:59:59, Price: 3000, Location: Vijayawada, Name: Mobile, Customer Name: Vanaja, Dispatch Date: 2019-03-01, Delivery Time: 10:10:52, Price: 9000, Location: Chennai, Name: Headset, Customer Name: Jalaja, Dispatch Date: 2019-04-06, Delivery Time: 11:08:59, Price: 6000, Location: Goa, Name: Watch, Customer Name: Rajan, Dispatch Date: 2019-09-01, Delivery Time: 10:59:59, Price: 4000, Location: Chennai
广告