Getting DateTime from ResultSet in JdbcTemplate

in the database, my column is of type TIMESTAMP, so my class has properties of type Datetime, like this:

public void setDiscoveryDate(final DateTime discoveryDtTm) { this.discoveryDtTm = discoveryDtTm; } 

now in JdbcTemplate I want to get it, so some code looks like this:

 variant.setDiscoveryDate(rs.getTimestamp("discovery_dt_tm")); 

which does not work, because the get column for the result set, I could not find something returning a DateTime, I only saw getDate or getTime.

+6
source share
2 answers

This is because DateTime not a standard Java type. If you mean the JodaTime type, try the following:

 variant.setDiscoveryDate( new DateTime(rs.getTimestamp("discovery_dt_tm").getTime()) ); 

This will break if rs.getTimestamp returns null , so you can break it down into smaller operators and add checks for null .

Note that this can be simplified, since the DateTime constructor accepts java.util.Date , which Timestamp is a subclass of:

 variant.setDiscoveryDate( new DateTime(rs.getTimestamp("discovery_dt_tm")) ); 

But this is also wrong, due to the poor construction of the Timestamp class (see javadoc for an explanation).

Stick to the first example (with getTime() )

+13
source

Try:

 variant.setDiscoveryDate(new DateTime(rs.getTimestamp("discovery_dt_tm").getTime())); 
+1
source

Source: https://habr.com/ru/post/894759/


All Articles