Package java.time của Java 8 cung cấp lớp LocalDateTime để lấy giá trị hiện tại của ngày và giờ địa phương. Ngoài giá trị ngày/giờ, bạn còn có thể lấy các trường khác như day-of-year, day-of-week, week-of-year.
Các bước đặt LocalDateTime vào cột bảng
- Lấy đối tượng LocalDateTime bằng phương thức tĩnh
now():
LocalDateTime localDateTime = LocalDateTime.now();
- Tách LocalDate và LocalTime từ LocalDateTime:
LocalDate localDate = localDateTime.toLocalDate(); LocalTime localTime = localDateTime.toLocalTime();
- Chuyển sang java.sql.Date và java.sql.Time bằng phương thức
valueOf():
java.sql.Date date = java.sql.Date.valueOf(localDate); java.sql.Time time = java.sql.Time.valueOf(localTime);
Ví dụ hoàn chỉnh (JDBC MySQL)
Tạo bảng dispatches:
CREATE TABLE dispatches(
ProductName VARCHAR(255),
CustomerName VARCHAR(255),
DispatchDate date,
DeliveryTime time,
Price INT,
Location VARCHAR(255)
);
Chương trình JDBC chèn bản ghi với ngày/giờ hiện tại:
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......");
// Lấy LocalDateTime hiện tại
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDateTime.toString());
// Chuyển sang java.sql.Date và java.sql.Time
java.sql.Date date = java.sql.Date.valueOf(localDateTime.toLocalDate());
java.sql.Time time = java.sql.Time.valueOf(localDateTime.toLocalTime());
// PreparedStatement chèn dữ liệu
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 ....");
// Truy vấn kiểm tra
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();
}
}
}
Kết quả đầu ra
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: Watch Customer Name: Rajan Date Of Dispatch: 2019-05-14 Delivery Time: 15:48:42 Location: Chennai
Lưu ý: Từ JDBC 4.2 (Java 8), có thể dùng trực tiếp setObject() với LocalDateTime, LocalDate, LocalTime mà không cần chuyển đổi qua java.sql.Date/Time.