我需要将 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 mapjava.lang.BigInteger
[sic] to JDBCBIGINT
.- Additional Mappings to Table B-5, Performed by
setObject
andsetNull
between Java Object Types and Target JDBC Types
[..]
Allow conversion ofjava.lang.BigInteger
[sic] toCHAR
,VARCHAR
,LONGVARCHAR
, andBIGINT
.
我不确定这对驱动程序的支持程度如何。
原始答案
JDBC 规范不包括对 BigInteger
的支持;您要么需要使用不同的数据类型(例如 BigDecimal
,比例为 0),要么查明 PostgreSQL 驱动程序是否提供了一些特定于实现的方法来设置 BigInteger
值。
关于java - 如何将 BigInteger 从 java 传递到 Postgres?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16357679/