-2

How to persist an item of java.time.LocalDate in a column of type Date within a DB2 database ?

Ghassen
  • 421
  • 8
  • 21

2 Answers2

3

Using a JDBC driver supporting JDBC 4.2 or later, use:

  • PreparedStatement::setObject()
  • ResultSet::getObject()

Example:

myPStmt.setObject( … , myLocalDate ) ;

Retrieving that date.

LocalDate myLocalDate = myResultSet.getObject( … , LocalDate.class ) ;
Basil Bourque
  • 218,480
  • 72
  • 657
  • 915
  • Can I ask you a question? Where do you get a DB2 driver that implements JDBC 4.2? I could only find 4.0/4.1 implementations at https://www.ibm.com/support/pages/node/382667. Also Maven Central has similar drivers, but 4.2 drivers are nowhere to be found. – The Impaler Feb 29 '20 at 16:59
  • @TheImpaler I don't know about DB2. I only use Postgres & H2. I imagine IBM *must* be providing such drivers. JDBC 4.2 came out several years ago, and we are already onto JDBC 4.3. See [JSR 221](https://www.jcp.org/en/jsr/detail?id=221). Also, you may consider 3rd party drivers as well, possibly open-source or commercial. – Basil Bourque Feb 29 '20 at 19:59
0

This seems to be a FAQ. Convert the java.time.LocalDate to java.util.Date which you can persist to a DB2 DATE column.

Related thread with example: example

You can also convert to java.sql.Date

mao
  • 8,724
  • 2
  • 10
  • 25
  • 2
    How is is answering the question? – M. Prokhorov Aug 23 '17 at 13:25
  • 1
    If that answered the question, then this should be marked as duplicate. Except the OP doesn't want a different Java type, they want the _database_ type, which is rather different (and requires library support). – Clockwork-Muse Aug 23 '17 at 16:28