该java.time Java8的包提供了一个名为类LocalDateTime用于获取本地日期和时间的当前值。除日期和时间值外,还可以使用此字段来获取其他日期和时间字段,例如,一年中的某天,一周中的某天和一周中的某周。
要将本地日期和时间值设置为表中的列-
获取LocalDateTime对象-您可以通过以以下方式调用静态方法来获取 LocalDateTime对象now()
-
//获取LocalDateTime对象 LocalDateTime localDateTime = LocalDateTime.now();
从上面获得的LocalDateTime中获取LocalDate和LocalTime对象为-
LocalDate localDate = localDateTime.toLocalDate(); LocalTime localTime = localDateTime.toLocalTime()
现在,将LocalDate和LocalTime对象分别传递给java.sql.Date和java.sql.Time类的valueOf()方法,如下所示:
java.sql.Date date = java.sql.Date.valueOf(localDate); java.sql.Time time = java.sql.Time.valueOf(localTime);
让我们使用CREATE语句在MySQL数据库中创建一个带有名称调度的表,如下所示:
CREATE TABLE dispatches( ProductName VARCHAR(255), CustomerName VARCHAR(255), DispatchDate date, DeliveryTime time, Price INT, Location VARCHAR(255) );
现在,我们将使用INSERT语句在分派表中插入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程序通过传递所需的值,将新记录插入到分派表中。在这里,我们获取当前的本地日期和时间值,并将其作为表的“日期”和“时间”列的值插入。
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.time.LocalDateTime; public class settingLocatDate { 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......"); //获取LocalDateTime对象 LocalDateTime localDateTime = LocalDateTime.now(); System.out.println(localDateTime.toString()); //将日期和时间值从本地转换为SQL- java.sql.Date date = java.sql.Date.valueOf(localDateTime.toLocalDate()); java.sql.Time time = java.sql.Time.valueOf(localDateTime.toLocalTime()); //创建一个准备好的语句 String query = "INSERT INTO Dispatches VALUES (?, ?, ?, ?, ?, ?)"; PreparedStatement pstmt = con.prepareStatement(query); pstmt.setString(1, "Watch"); pstmt.setString(2, "Rajan"); pstmt.setDate(3, date); pstmt.setObject(4, time); pstmt.setInt(5, 4000); pstmt.setString(6, "Chennai"); pstmt.execute(); System.out.println("Rows inserted ...."); //检索值 Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from dispatches"); while(rs.next()) { System.out.println("Product Name: "+rs.getString("ProductName")); System.out.println("Customer Name: "+rs.getString("CustomerName")); System.out.println("Date Of Dispatch: "+rs.getDate("DispatchDate")); System.out.println("Delivery Time: "+rs.getTime("DeliveryTime")); System.out.println("Location: "+rs.getString("Location")); System.out.println(); } } }
输出结果
Connection established...... 2019-05-14T15:48:42.457 Rows inserted .... Product Name: Key-Board Customer Name: Raja Date Of Dispatch: 2019-09-01 Delivery Time: 11:00:00 Location: Hyderabad Product Name: Earphones Customer Name: Roja Date Of Dispatch: 2019-05-01 Delivery Time: 11:00:00 Location: Vishakhapatnam Product Name: Mouse Customer Name: Puja Date Of Dispatch: 2019-03-01 Delivery Time: 10:59:59 Location: Vijayawada Product Name: Mobile Customer Name: Vanaja Date Of Dispatch: 2019-03-01 Delivery Time: 10:10:52 Location: Chennai Product Name: Headset Customer Name: Jalaja Date Of Dispatch: 2019-04-06 Delivery Time: 11:08:59 Location: Goa Product Name: Watch Customer Name: Rajan Date Of Dispatch: 2019-05-14 Delivery Time: 15:48:42 Location: Chennai