toString()
java.sql.Timestamp类的方法以String变量的形式返回当前Timestamp对象的时间戳的JDBC转义格式。
即,使用此方法可以将Timestamp对象转换为String。
//Retrieving the Time object Timestamp timestampObj = rs.getTimestamp("DispatchTimeStamp"); //将Time对象转换为String格式 String time_stamp = timestampObj.toString();
让我们使用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.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; public class Timestamp_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("Connection established......"); //创建一个Statement对象 Statement stmt = con.createStatement(); //查询以检索dispatches_data表的内容 String query = "select * from dispatches_data"; //执行查询 ResultSet rs = stmt.executeQuery(query); while (rs.next()) { System.out.println("Product Name: " + rs.getString("ProductName")); System.out.println("Customer Name: " + rs.getString("CustomerName")); Timestamp timeStampObj = rs.getTimestamp("DispatchTimeStamp"); //将Time对象转换为String格式 String timeStamp = timeStampObj.toString(); System.out.println("Dispatch time stamp in String format: " + timeStamp); System.out.println("Location: " + rs.getString("Location")); System.out.println(); } } }
在这里,在此程序中,在检索列值时,我们已使用toString()
Timestamp类的方法将DeliveryTime值从Timestamp对象转换为字符串格式,并尝试显示它。
输出结果
Connection established...... Product Name: Key-Board Customer Name: Raja Dispatch time stamp in String format: 2019-05-04 15:02:45.0 Location: Hyderabad Product Name: Earphones Customer Name: Roja Dispatch time stamp in String format: 2019-06-26 14:13:12.0 Location: Vishakhapatnam Product Name: Mouse Customer Name: Puja Dispatch time stamp in String format: 2019-12-07 07:50:37.0 Location: Vijayawada Product Name: Mobile Customer Name: Vanaja Dispatch time stamp in String format: 2018-03-21 16:00:45.0 Location: Chennai Product Name: Headset Customer Name: Jalaja Dispatch time stamp in String format: 2018-12-30 10:49:27.0 Location: Goa