使用 H2 数据库在 JDBC 中将年份从负 -509 更改为正 510

2024-04-21

-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项目。


The problem is caused by the conversion from java.sql.Date to LocalDate . Because it's a negative year, the Calendar instance holding the fetched result will transform the year to 1 - year but when converting to LocalDate java is not considering the additional information (era==BC) that indicates that the year < 0 The following is the final method executed before returning the result. enter image description here

试试这个:

public class Test {
 public static void main(String[] args) {

            Calendar instance = Calendar.getInstance();
            instance.set(-509,1,1);

            java.sql.Date d = new Date(instance.getTime().getTime());

            System.out.println(d.toLocalDate().getYear());// 510


        }
}

谢谢 Ole V.V.征求您的意见!!!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 H2 数据库在 JDBC 中将年份从负 -509 更改为正 510 的相关文章

随机推荐

  • 停用所有 conda 环境

    当激活 conda 环境时 conda 会记住之前激活的环境 例如 如果没有激活环境 则调用conda activate foo进而conda deactivate结果没有环境处于活动状态 然而 调用conda activate foo 进
  • 如何使用 PySimpleGui 从列表创建单选按钮?

    我想使用 PySimpleGui 从列表动态创建单选按钮 但我在布局代码中插入循环的努力正在捕获语法错误 这可以通过 API 来完成还是需要使用 tkinter 来完成 我的列表是通过网络驱动器的目标文件搜索生成的 我尝试连接 布局 将单选
  • 模块范围内的全局关键字

    我有一个 Python 文件 其中包含以下几行 import sys global AdminConfig global AdminApp 该脚本在 Jython 上运行 我理解在函数内部使用 global 关键字 但是在模块级别使用 gl
  • 模型中的列表字段?

    在我的模型中 我想要一个包含三元组列表的字段 例如 1 3 4 4 2 6 8 12 3 3 3 9 数据库中是否有一个字段可以存储这些数据 您可以使用 JSON 将其转换为字符串并将其存储为字符串 例如 In 3 json dumps 1
  • jQuery FullCalendar 适用于触摸设备 - 但事件存在小问题

    http page test co uk cal http page test co uk cal 完整日历演示 我已经设置了它 这是一个基本的 jQuery FullCalendar 设置 带有相关的附加功能 以支持触摸设备 链接页面中包
  • 区分大小写 Directory.Exists / File.Exists

    有没有办法区分大小写Directory Exists File Existssince Directory Exists folderPath and Directory Exists folderPath ToLower 都返回true
  • Angular js 2 'node_modules/rxjs/Observable"' 没有导出成员 'Observable'。 import Observable

    我在 Node Modules 包中的 Auth d ts 文件中遇到以下错误 ts 模块 node modules rxjs Observable 没有导出成员 Observable 导入可观察的 找到 Auth d ts 文件的以下代码
  • 如何删除具有特定类名的所有 div?

    使用jquery 删除具有特定类名的所有div的最佳方法是什么 我不想只是隐藏 div 而是完全删除它 所以如果我有这个代码 div class Test div class ABC div class Test 在我调用这个方法 其中 c
  • 如何在 Pygame 表面中实现洪水填充

    我想知道填充 Pygame 表面部分的好方法 我想要的最好的例子是 MS Paint 中油漆桶的工作方式 例如 如果在白色表面上用黑色绘制一个圆圈 我想填充圆圈内的白色 或任何形状 为了让您了解我正在做什么 我正在制作一个像素艺术工具 并且
  • 如何在flutter中重新加载网络图像?

    在flutter中使用网络图像时有时会出现错误Connection closed before full header was received 下面的代码允许我输出错误 但是如何强制小部件重新加载图像 Image network p th
  • 阻止 Visual Studio 在启动时连接到 Team Foundation Server

    Visual Studio 在启动时自动尝试连接到 Team Foundation Server 但有时当您频繁更改 TFS 服务器时 Visual Studio 会在尝试连接到上次使用的 TFS 时花费很长时间超时 如何禁用此功能 您可以
  • 从移动网站中的链接打开电报应用程序

    有什么方法可以从手机中的网站打开电报应用程序吗 我知道如果您使用 telegram 您可以打开 telegram 应用程序 但如何打开 telegram 并使用给定号码创建新对话 我知道可以通过 Whatsapp 之类的方式实现this h
  • 如何创建dll文件

    使用 Visual Studio 2005 我有类文件列表 当我尝试运行类文件时 它显示错误为 输出类型为类库的项目无法直接启动 如何运行类文件 如何创建 dll 文件 我是 Visual Studio 2005 的新手 需要帮忙 A Cl
  • 在 React Native 渲染文本组件中显示动画值

    我无法在渲染器上显示动画的值并返回此错误 不变违规 对象作为 React 子对象无效 发现 带有键 value 的对象 如果您打算渲染子集合 请改用数组 当然 我看到了其中的价值console constructor props super
  • C# 计算两个日期之间的工作日数

    如何获取两个给定日期之间的工作日数 而无需迭代之间的日期并计算工作日 看起来相当简单 但我似乎找不到符合以下条件的结论性正确答案 总数应包含在内 因此 GetNumberOfWeekdays new DateTime 2009 11 30
  • vue 组件中的 Csrf 令牌

    我有集成了 Vue js 的 Laravel 5 3 项目 我想使用CSRF TOKEN以我的形式 表单html代码在Vue组件文件中 resources assets js bootstrap js 我有这个 Vue http inter
  • 如何在不向服务器发送数据的情况下显示选定的图像?

    我试图向客户展示他选择的图像
  • 检查 Ruby 中是否存在 URL

    我如何使用 Ruby 检查 URL 是否存在 例如 对于 URL https google com 结果应该是truthy 但是对于 URL https no such domain or https stackoverflow com n
  • C中的副作用是什么?

    维基百科说 在计算机科学中 一个操作 函数或表达式被认为具有副作用如果它在其本地环境之外修改某些状态变量值 也就是说 除了向操作的调用者返回一个值 主要效果 之外 还具有可观察到的效果 但是我们如何访问本地环境之外的变量 任何人都可以解释这
  • 使用 H2 数据库在 JDBC 中将年份从负 -509 更改为正 510

    509 vs 510 我在使用 JDBC 时看到某种已更改或错误的数据 所以我观察使用H2数据库 http h2database com Java 8 更新 151 上的版本 1 4 196 这是一个完整的例子 请注意我们如何检索日期值三次