使用H2数据库在JDBC中从负-509变为正510 [英] Year changing from negative -509 to a positive 510 in JDBC with H2 Database

查看:125
本文介绍了使用H2数据库在JDBC中从负-509变为正510的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通过使用JDBC,可以看到某种改变或错误的数据。所以我观察到在Java 8 Update 151上使用 H2数据库版本1.4.196。

I am seeing some kind of altered or erroneous data, with the use of JDBC. So I observe using H2 Database version 1.4.196 on Java 8 Update 151.

这是一个完整的例子。

Here is a complete example.

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

Note how we retrieve the date value three times, first as a LocalDate object, secondly as text, and thirdly as an int year number extracted from a cast LocalDate object. In the text version we can see that the year is indeed negative. Mysteriously the LocalDate has a different year number and it is positive rather than negative. Seems like a 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( );
    }
}

运行时。


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

ROW: 1 | 2017-01-23 | when as String: 2017-01-23 | year: 2017

行:2 | 0510-01-01 |当作为字符串时:-509-01-01 |年:510

ROW: 2 | 0510-01-01 | when as String: -509-01-01 | year: 510

因此,似乎有涉及JDBC的事情。请注意年份如何呈现为正510而不是负509.我不明白这种行为。

So there seems to be something going on with JDBC being involved. Note how the year is presented as a positive 510 rather than a negative 509. I do not understand this behavior.

我可以在中推断出这是一个问题。 JDBC 而不是 LOCALDATE 。请参阅在IdeOne.com上直播的示例代码,显示 LocalDate 对象确实携带并报告负面年份。

I can deduce that it is an issue within JDBC rather than within LocalDate. See this example code run live in IdeOne.com showing that a LocalDate object does indeed carry and report a negative year.

LocalDate ld = LocalDate.of( -509 , 1 , 1 ) ;
System.out.println( "ld.toString(): " + ld ) ;
System.out.println( "ld.getYear(): " + ld.getYear() ) ;

注意我们在交易时如何从-509转换为510只有 LocalDate ,没有JDBC。

Notice how we do not get converted from -509 to 510 when dealing with a LocalDate only, with no JDBC.


ld:-0509-01 -01

ld: -0509-01-01

ld.getYear(): - 509

ld.getYear(): -509

我打开了关于H2项目的发行票

I opened a Issue ticket on the H2 project.

推荐答案

错误,已修复



此问题是由于< a href =https://github.com/h2database/h2database/pull/708 =nofollow noreferrer> H2中的错误。

现已修复,截至2018-01。

Now fixed, as of 2018-01.

这篇关于使用H2数据库在JDBC中从负-509变为正510的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆