Home >Database >Mysql Tutorial >How to Insert and Retrieve `java.time.LocalDate` Objects using JDBC?
Insert java.time.LocalDate
objects into and retrieve
Question:
How to handle java.time
types such as LocalDate
using JDBC (for SQL databases like H2)?
Answer:
There are two methods:
JDBC 4.2 compatible driver:
Insert data:
<code class="language-java"> myPreparedStatement.setObject(1, myLocalDate); // 自动转换</code>
Retrieve data:
<code class="language-java"> LocalDate localDate = myResultSet.getObject("my_date_column_", LocalDate.class); // 预期返回类型</code>
Incompatible driver (pre-JDBC 4.2):
java.sql.Date.valueOf(myLocalDate)
to convert LocalDate
to java.sql.Date
. sqlDate.toLocalDate()
to convert java.sql.Date
to LocalDate
. java.time
Framework for Java 8:
LocalDate
). java.util.Date
, Calendar
). Note: H2 supports JDBC 4.2, so you can use the easier compatibility method (first method).
The above is the detailed content of How to Insert and Retrieve `java.time.LocalDate` Objects using JDBC?. For more information, please follow other related articles on the PHP Chinese website!