java.time.LocalDate
物件問題:
如何使用JDBC在H2資料庫引擎等SQL資料庫中插入和檢索java.time
類型(例如LocalDate
)?
解答:
方法一:相容於JDBC 4.2的驅動程式
對於符合JDBC 4.2規範或更高版本的JDBC驅動程序,您可以直接使用setObject
和getObject
方法處理java.time
物件。驅動程式會自動偵測Java類型並將其轉換為對應的SQL類型。例如:
<code class="language-java">preparedStatement.setObject(1, myLocalDate); // LocalDate转换为SQL DATE LocalDate localDate = myResultSet.getObject("my_date_column_", LocalDate.class); // 指定预期类以确保类型安全</code>
方法二:JDBC 4.2之前的舊版驅動程式
對於不相容JDBC 4.2的驅動程序,您必須將java.time
物件短暫轉換為等效的java.sql
類型,反之亦然。使用新增到舊版類別中的轉換方法:
<code class="language-java">java.sql.Date mySqlDate = java.sql.Date.valueOf(myLocalDate); preparedStatement.setDate(1, mySqlDate); java.sql.Date sqlDate = myResultSet.getDate("date_"); //尽可能简短地处理转换 LocalDate localDate = sqlDate.toLocalDate();</code>
相容JDBC 4.2的驅動程式範例:
<code class="language-java">import java.sql.*; import java.time.LocalDate; import java.time.ZoneId; public class LocalDateExample { public static void main(String[] args) throws SQLException { String url = "jdbc:h2:mem:test_db"; // 更改为您的数据库URL String user = "user"; String password = "password"; try ( Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(); ) { stmt.execute("CREATE TABLE IF NOT EXISTS employees (id INT PRIMARY KEY, name VARCHAR(255), birthday DATE)"); // 插入LocalDate值 LocalDate today = LocalDate.now(ZoneId.of("America/Montreal")); PreparedStatement pstmt = conn.prepareStatement("INSERT INTO employees (name, birthday) VALUES (?, ?)"); pstmt.setString(1, "John Doe"); pstmt.setObject(2, today); // 直接传递LocalDate pstmt.executeUpdate(); // 检索LocalDate值 ResultSet rs = stmt.executeQuery("SELECT * FROM employees"); while (rs.next()) { LocalDate birthday = rs.getObject("birthday", LocalDate.class); // 指定预期类 System.out.println("员工:" + rs.getString("name") + ",生日:" + birthday); } rs.close(); pstmt.close(); stmt.close(); } } }</code>
舊版驅動程式範例:
<code class="language-java">import java.sql.*; import java.time.LocalDate; public class LocalDateExample { public static void main(String[] args) throws SQLException { String url = "jdbc:h2:mem:test_db"; // 更改为您的数据库URL String user = "user"; String password = "password"; try ( Connection conn = DriverManager.getConnection(url, user, password); Statement stmt = conn.createStatement(); ) { stmt.execute("CREATE TABLE IF NOT EXISTS employees (id INT PRIMARY KEY, name VARCHAR(255), birthday DATE)"); // 插入LocalDate值 LocalDate today = LocalDate.now(); java.sql.Date sqlDate = java.sql.Date.valueOf(today); PreparedStatement pstmt = conn.prepareStatement("INSERT INTO employees (name, birthday) VALUES (?, ?)"); pstmt.setString(1, "John Doe"); pstmt.setDate(2, sqlDate); // 将LocalDate转换为java.sql.Date pstmt.executeUpdate(); // 检索LocalDate值 ResultSet rs = stmt.executeQuery("SELECT * FROM employees"); while (rs.next()) { java.sql.Date sqlDate = rs.getDate("birthday"); LocalDate birthday = sqlDate.toLocalDate(); // 将java.sql.Date转换为LocalDate System.out.println("员工:" + rs.getString("name") + ",生日:" + birthday); } rs.close(); pstmt.close(); stmt.close(); } } }</code>
以上是如何使用 JDBC 在 H2 資料庫中插入和檢索'java.time.LocalDate”物件?的詳細內容。更多資訊請關注PHP中文網其他相關文章!