java.sql.Timestamp类的 valueOf() 方法接受表示JDBC转义格式时间戳的String值,并将给定的String值转换为Timestamp对象。
Timestamp timeStamp = Time.valueOf("timeStamp_string");
让我们使用CREATE语句在MySQL数据库中创建一个名为dispatches_data的表,如下所示:
CREATE TABLE dispatches_data( ProductName VARCHAR(255), CustomerName VARCHAR(255), DispatchTimeStamp timestamp, Price INT, Location VARCHAR(255));
现在,我们将使用INSERT语句在dispatches_data表中插入5条记录:
insert into dispatches_data values('Key-Board', 'Raja', TIMESTAMP('2019-05-04', '15:02:45'), 7000, 'Hyderabad'); insert into dispatches_data values('Earphones', 'Roja', TIMESTAMP('2019-06-26', '14:13:12'), 2000, 'Vishakhapatnam'); insert into dispatches_data values('Mouse', 'Puja', TIMESTAMP('2019-12-07', '07:50:37'), 3000, 'Vijayawada'); insert into dispatches_data values('Mobile', 'Vanaja' , TIMESTAMP ('2018-03-21', '16:00:45'), 9000, 'Chennai'); insert into dispatches_data values('Headset', 'Jalaja' , TIMESTAMP('2018-12-30', '10:49:27'), 6000, 'Goa');
随后的JDBC程序建立与数据库的连接,并将新记录插入到dispatches_data表中。
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; public class Timestamp_valueOf { 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("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.execute(); //检索数据 Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from dispatches_data"); while(rs.next()) { System.out.print("Name: "+rs.getString("ProductName")+", "); System.out.print("Customer Name: "+rs.getString("CustomerName")+", "); System.out.print("Dispatch Time Stamp: "+rs.getDate("DispatchTimeStamp")+", "); System.out.print("Price: "+rs.getInt("Price")+", "); System.out.print("Location: "+rs.getString("Location")); System.out.println(); } } }
在这里,在此程序中,我们以String格式获取时间戳记值,并使用valueOf()
方法将其转换为java.util.Time对象。
输出结果
Connection established...... Name: Key-Board, Customer Name: Raja, Dispatch Time Stamp: 2019-05-04, Price: 7000, Location: Hyderabad Name: Earphones, Customer Name: Roja, Dispatch Time Stamp: 2019-06-26, Price: 2000, Location: Vishakhapatnam Name: Mouse, Customer Name: Puja, Dispatch Time Stamp: 2019-12-07, Price: 3000, Location: Vijayawada Name: Mobile, Customer Name: Vanaja, Dispatch Time Stamp: 2018-03-21, Price: 9000, Location: Chennai Name: Headset, Customer Name: Jalaja, Dispatch Time Stamp: 2018-12-30, Price: 6000, Location: Goa Name: Watch, Customer Name: Rajan, Dispatch Time Stamp: 2019-04-21, Price: 4000, Location: Chennai