How to Insert and Retrieve java.time Objects Using JDBC?
Inserting and Retrieving java.time Objects Using JDBC
JDBC offers two ways to exchange java.time objects through JDBC:
JDBC 4.2 Compliant Drivers
JDBC 4.2 compliant drivers let you interact with java.time objects directly, without conversions.
Insertion: Use setObject to pass your java.time object. The driver automatically converts it to the appropriate SQL type (e.g., LocalDate to SQL DATE).
Retrieval: Call getObject twice, first without arguments to retrieve the value as an Object, then with the expected class as an argument for type-safety.
Non-compliant Drivers
For drivers that don't support JDBC 4.2, you can convert between java.time and java.sql types using the following code:
Insertion:
LocalDate date = ...; java.sql.Date sqlDate = java.sql.Date.valueOf(date); preparedStatement.setDate(1, sqlDate);
Retrieval:
java.sql.Date sqlDate = resultSet.getDate(1); LocalDate date = sqlDate.toLocalDate();
Example with H2
Using JDBC 4.2 with H2:
try { Class.forName("org.h2.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try ( Connection conn = DriverManager.getConnection("jdbc:h2:mem:mydb"); Statement stmt = conn.createStatement(); ) { String sql = "CREATE TABLE test (id UUID, date DATE);"; stmt.execute(sql); LocalDate today = LocalDate.now(); sql = String.format("INSERT INTO test (id, date) VALUES (%s, %s)", UUID.randomUUID(), today); stmt.executeUpdate(sql); sql = "SELECT id, date FROM test"; try (ResultSet rs = stmt.executeQuery(sql)) { while (rs.next()) { UUID id = rs.getObject("id", UUID.class); LocalDate date = rs.getObject("date", LocalDate.class); System.out.println(String.format("id: %s, date: %s", id, date)); } } } catch (SQLException e) { e.printStackTrace(); }
Example with Non-compliant Drivers
Using non-JDBC 4.2 with H2:
try { Class.forName("org.h2.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try ( Connection conn = DriverManager.getConnection("jdbc:h2:mem:mydb"); Statement stmt = conn.createStatement(); ) { String sql = "CREATE TABLE test (id UUID, date DATE);"; stmt.execute(sql); LocalDate today = LocalDate.now(); java.sql.Date sqlDate = java.sql.Date.valueOf(today); sql = String.format("INSERT INTO test (id, date) VALUES (%s, %s)", UUID.randomUUID(), sqlDate); stmt.executeUpdate(sql); sql = "SELECT id, date FROM test"; try (ResultSet rs = stmt.executeQuery(sql)) { while (rs.next()) { UUID id = (UUID) rs.getObject("id"); java.sql.Date date = rs.getDate("date"); LocalDate localDate = date.toLocalDate(); System.out.println(String.format("id: %s, date: %s", id, localDate)); } } } catch (SQLException e) { e.printStackTrace(); }
The above is the detailed content of How to Insert and Retrieve java.time Objects Using JDBC?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
