-509
vs 510
我在使用 JDBC 时看到某种已更改或错误的数据。所以我观察使用H2数据库 http://h2database.com/Java 8 更新 151 上的版本 1.4.196。
这是一个完整的例子。
请注意我们如何检索日期值三次,第一次作为LocalDate
对象,其次作为文本,第三作为int
从演员表中提取的年份号LocalDate
目的。在文本版本中我们可以看到年份确实是负数。神秘的是LocalDate
有不同的年份数字,并且它是正数而不是负数。看起来像一个错误。
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:" ) ; // Unnamed throw-away in-memory database.
)
{
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 ); // Identity column.
// Retrieve the same date value in three different ways.
LocalDate ld = rs.getObject( 2 , LocalDate.class ); // Extract a `LocalDate`, and implicitly call its `toString` method that uses standard ISO 8601 formatting.
String s = rs.getString( 2 ); // Extract the date value as text from the database using the database-engine’s own formatting.
int y = ( ( LocalDate ) rs.getObject( 2 , LocalDate.class ) ).getYear( ); // Extract the year number as an integer from a `LocalDate` object.
String output = "ROW: " + l+ " | " + ld + " | when as String: " + s+ " | year: " + y ;
System.out.println( output );
}
conn.close( );
} catch ( SQLException e )
{
e.printStackTrace( );
}
}
运行时。
行:1 | 2017-01-23 |当作为字符串时:2017-01-23 |年份: 2017
行:2 | 0510-01-01 |当作为字符串时:-509-01-01 |年份:510
所以似乎有一些与 JDBC 相关的事情正在发生。请注意年份是如何呈现为正 510 而不是负 509。我不理解这种行为。
我可以推断这是一个问题JDBC https://en.wikipedia.org/wiki/Java_Database_Connectivity而不是在内部LocalDate https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html。看到这个在 IdeOne.com 中实时运行的示例代码 https://www.ideone.com/iVb7PW表明一个LocalDate
对象确实携带并报告负年份。
LocalDate ld = LocalDate.of( -509 , 1 , 1 ) ;
System.out.println( "ld.toString(): " + ld ) ;
System.out.println( "ld.getYear(): " + ld.getYear() ) ;
注意我们是如何做的not处理时从 -509 转换为 510LocalDate
仅,没有 JDBC。
LD:-0509-01-01
ld.getYear(): -509
我开了一个出票 https://github.com/h2database/h2database/issues/644关于H2项目。