How to persist an item of java.time.LocalDate in a column of type Date within a DB2 database ?
            Asked
            
        
        
            Active
            
        
            Viewed 1,441 times
        
    -2
            
            
         
    
    
        Ghassen
        
- 591
- 1
- 15
- 33
- 
                    1What have you tried so far? What didn't work? – M. Prokhorov Aug 23 '17 at 12:48
- 
                    Actually now to unblock the situation I changer my attribute to String but not for too much time because this will not help me – Ghassen Aug 23 '17 at 14:50
2 Answers
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
        
- 303,325
- 100
- 852
- 1,154
- 
                    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
        
- 11,321
- 2
- 13
- 29
- 
                    2
- 
                    1If 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