使用H2数据库在JDBC中从负-509变为正510

Bas*_*que 6 java jdbc h2 java-8 localdate

-509 VS 510

我使用JDBC看到了某种改变或错误的数据.所以我观察在Java 8 Update 151上使用H2 Database版本1.4.196.

这是一个完整的例子.

注意我们如何检索三次日期值,首先作为LocalDate对象,其次作为文本,第三次作为int从演员LocalDate对象中提取的年份数.在文本版本中,我们可以看到年份确实是负面的.神秘地说,LocalDate它具有不同的年份数,而且是积极的而不是消极的.好像是一个bug.

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( );
    }
}
Run Code Online (Sandbox Code Playgroud)

跑步时

行:1 | 2017-01-23 | 当字符串:2017-01-23 | 年:2017年

行: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() ) ;
Run Code Online (Sandbox Code Playgroud)

请注意如何,我们都不会得到从-509具有打交道时转换为510 LocalDate只,没有JDBC.

ld:-0509-01-01

ld.getYear(): - 509

我在H2项目上开了一张发票.

Bas*_*que 1

错误,已修复

这个问题是由于H2 中的错误造成的。

截至 2018 年 1 月,现已修复。