热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

JavaSQLTimestampValueOf方法详解及应用实例

本文详细介绍了Java中`java.sql.Timestamp`类的`valueOf()`方法,该方法用于将符合JDBC转义格式的时间戳字符串转换为`Timestamp`对象,并通过示例展示了其在实际开发中的应用。

`java.sql.Timestamp` 类的 `valueOf()` 方法能够接收表示 JDBC 转义格式时间戳的字符串,并将其转换成 `Timestamp` 对象。例如:`Timestamp timeStamp = Timestamp.valueOf("timeStamp_string");`

下面通过一个具体的例子来说明如何使用 `valueOf()` 方法。首先,在 MySQL 数据库中创建一个名为 `dispatches_data` 的表,包含产品名称、客户名称、发货时间戳、价格和位置等字段:

```sql
CREATE TABLE dispatches_data (
ProductName VARCHAR(255),
CustomerName VARCHAR(255),
DispatchTimeStamp TIMESTAMP,
Price INT,
Location VARCHAR(255)
);
```

接着,向 `dispatches_data` 表中插入几条记录:

```sql
INSERT INTO dispatches_data VALUES ('Keyboard', 'Raja', '2019-05-04 15:02:45', 7000, 'Hyderabad');
INSERT INTO dispatches_data VALUES ('Earphones', 'Roja', '2019-06-26 14:13:12', 2000, 'Vishakhapatnam');
INSERT INTO dispatches_data VALUES ('Mouse', 'Puja', '2019-12-07 07:50:37', 3000, 'Vijayawada');
INSERT INTO dispatches_data VALUES ('Mobile', 'Vanaja', '2018-03-21 16:00:45', 9000, 'Chennai');
INSERT INTO dispatches_data VALUES ('Headset', 'Jalaja', '2018-12-30 10:49:27', 6000, 'Goa');
```

接下来,编写一个 JDBC 程序,实现与数据库的连接,并向 `dispatches_data` 表中插入一条新的记录。以下是完整的代码示例:

```java
import java.sql.*;

public class TimestampValueOfExample {
public static void main(String[] args) throws SQLException {
// 注册驱动
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());

// 建立连接
String mysqlUrl = "jdbc:mysql://localhost:3306/mydatabase";
Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
System.out.println("Database connection established...");

// 插入数据
String query = "INSERT INTO dispatches_data VALUES (?, ?, ?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, "Watch");
pstmt.setString(2, "Rajan");
Timestamp timestamp = Timestamp.valueOf("2019-04-21 14:17:02.0");
pstmt.setTimestamp(3, timestamp);
pstmt.setInt(4, 4000);
pstmt.setString(5, "Chennai");
pstmt.executeUpdate();

// 查询数据
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM dispatches_data");
while (rs.next()) {
System.out.print("Product Name: " + rs.getString("ProductName") + ", ");
System.out.print("Customer Name: " + rs.getString("CustomerName") + ", ");
System.out.print("Dispatch Time Stamp: " + rs.getTimestamp("DispatchTimeStamp") + ", ");
System.out.print("Price: " + rs.getInt("Price") + ", ");
System.out.print("Location: " + rs.getString("Location") + "\n");
}
}
}
```

在这个程序中,我们通过 `Timestamp.valueOf()` 方法将字符串形式的时间戳转换为 `Timestamp` 对象,然后将其插入到数据库中。最后,程序查询了所有记录并打印出来。

运行上述程序后,控制台将显示如下输出:

```
Database connection established...
Product Name: Keyboard, Customer Name: Raja, Dispatch Time Stamp: 2019-05-04 15:02:45.0, Price: 7000, Location: Hyderabad
Product Name: Earphones, Customer Name: Roja, Dispatch Time Stamp: 2019-06-26 14:13:12.0, Price: 2000, Location: Vishakhapatnam
Product Name: Mouse, Customer Name: Puja, Dispatch Time Stamp: 2019-12-07 07:50:37.0, Price: 3000, Location: Vijayawada
Product Name: Mobile, Customer Name: Vanaja, Dispatch Time Stamp: 2018-03-21 16:00:45.0, Price: 9000, Location: Chennai
Product Name: Headset, Customer Name: Jalaja, Dispatch Time Stamp: 2018-12-30 10:49:27.0, Price: 6000, Location: Goa
Product Name: Watch, Customer Name: Rajan, Dispatch Time Stamp: 2019-04-21 14:17:02.0, Price: 4000, Location: Chennai
```


推荐阅读
author-avatar
372630243
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有