-509 vs 510
I see some kind of altered or erroneous data using JDBC. Therefore, I am observing the use of H2 Database version 1.4.196 on Java 8 Update 151.
Here is a complete example.
Pay attention to the fact that we get the date value three times, first as an object LocalDate, secondly as a text, and thirdly as a year number intextracted from a cast object LocalDate. In the text version, we see that the year is really negative. Mysteriously LocalDatehas a different year number, and it is more likely positive than negative. Sounds like a mistake.
private void doIt ( )
{
System.out.println( "BASIL - Running doIt." );
try
{
Class.forName( "org.h2.Driver" );
} catch ( ClassNotFoundException e )
{
e.printStackTrace( );
}
try (
Connection conn = DriverManager.getConnection( "jdbc:h2:mem:" ) ;
)
{
conn.setAutoCommit( true );
String sqlCreate = "CREATE TABLE history ( id IDENTITY , when DATE ); ";
String sqlInsert = "INSERT INTO history ( when ) VALUES ( ? ) ; ";
String sqlQueryAll = "SELECT * FROM history ; ";
PreparedStatement psCreate = conn.prepareStatement( sqlCreate );
psCreate.executeUpdate( );
PreparedStatement psInsert = conn.prepareStatement( sqlInsert );
psInsert.setObject( 1 , LocalDate.of( 2017 , 1 , 23 ) );
psInsert.executeUpdate( );
psInsert.setObject( 1 , LocalDate.of( -509 , 1 , 1 ) );
psInsert.executeUpdate( );
PreparedStatement psQueryAll = conn.prepareStatement( sqlQueryAll );
ResultSet rs = psQueryAll.executeQuery( );
while ( rs.next( ) )
{
long l = rs.getLong( 1 );
LocalDate ld = rs.getObject( 2 , LocalDate.class );
String s = rs.getString( 2 );
int y = ( ( LocalDate ) rs.getObject( 2 , LocalDate.class ) ).getYear( );
String output = "ROW: " + l+ " | " + ld + " | when as String: " + s+ " | year: " + y ;
System.out.println( output );
}
conn.close( );
} catch ( SQLException e )
{
e.printStackTrace( );
}
}
At startup.
ROW: 1 | 2017-01-23 | when as a string: 2017-01-23 | Year: 2017
ROW: 2 | 0510-01-01 | : -509-01-01 | : 510
, JDBC, , - . , 510, 509. .
, JDBC, LocalDate. . , IdeOne.com, , LocalDate .
LocalDate ld = LocalDate.of( -509 , 1 , 1 ) ;
System.out.println( "ld.toString(): " + ld ) ;
System.out.println( "ld.getYear(): " + ld.getYear() ) ;
, -509 510 LocalDate, JDBC.
ld: -0509-01-01
ld.getYear(): -509
"" H2.