java - 如何将 BigInteger 从 java 传递到 Postgres?

标签 java spring postgresql jdbc postgresql-9.2

我需要将 BigInteger 参数传递给 SQL 查询。 (在 Postgres 9.2 上) 我的 DAO 中有这段代码:

    public List<PersonInfo> select(String id) {
        BigInteger bigIntId = new BigInteger(id);
        JdbcTemplate select = new JdbcTemplate(dataSource);
        return select
            .query("SELECT * FROM PE.SUPPLIER_INPUT_DATA WHERE ID = ?",
                    new Object[] { bigIntId },
                    new PersonRowMapper());
    }

我收到以下异常:

{"error":"Error invoking getPersonInfoById.[org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; 
bad SQL grammar [SELECT * FROM PE.SUPPLIER_INPUT_DATA WHERE ID = ?]; 
nested exception is org.postgresql.util.PSQLException: 
Can't infer the SQL type to use for an instance of java.math.BigInteger. 
Use setObject() with an explicit Types value to specify the type to use.]"}

id是bigint类型

试图传递纯字符串 - 也抛出类型异常。 在异常中搜索消息 - 没有相关结果。 有任何想法吗?

最佳答案

在 JDBC 4.1 (Java 7) 中添加了对 BigInteger 的支持,不知何故我在最初写这个答案时错过了这一点。

特别是 JDBC 4.1 规范变更概述的第 3.1 节指出:

  • Additional Mappings to Table B-4, Mapping from Java Object to JDBC Types
    [..]
    Support was also added to map java.lang.BigInteger[sic] to JDBC BIGINT.
  • Additional Mappings to Table B-5, Performed by setObject and setNull between Java Object Types and Target JDBC Types
    [..]
    Allow conversion of java.lang.BigInteger[sic] to CHAR, VARCHAR, LONGVARCHAR, and BIGINT.

我不确定这对驱动程序的支持程度如何。

原始答案

JDBC 规范不包括对 BigInteger 的支持;您要么需要使用不同的数据类型(例如 BigDecimal,比例为 0),要么查明 PostgreSQL 驱动程序是否提供了一些特定于实现的方法来设置 BigInteger 值。

关于java - 如何将 BigInteger 从 java 传递到 Postgres?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16357679/

相关文章:

java - 有什么方法可以将注释的值传递到 Spring Boot 中的配置吗?

ruby-on-rails - rails : Copy data for specific tables between databases/environments

sql - 函数显示错误 "relation my_table does not exist"

java - compareTo 方法如何在这里对对象进行一一排序

java - Lombok @Builder 不创建不可变对象(immutable对象)?

Spring - 使用抛出异常的类实例化一个 bean

jquery - 如何使用<spring :message tag in javascript/jQuery/jsp?

arrays - 将整数数组传递给 plpgsql 函数时出错 (postgres 8.3.7)

java - JFrame.setBounds 不适用于小值

java - Apache Log4j 2.17 版本 : ERROR StatusLogger Reconfiguration failed: No configuration found for '277050dc' at 'null' in 'null'