【发布时间】:2013-05-03 11:16:06
【问题描述】:
我需要将 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 类型
试图传递纯字符串 - 也会引发类型异常。 谷歌搜索异常中的消息 - 没有相关结果。 有任何想法吗?
【问题讨论】:
-
ID 属于哪种数据类型?
-
JDBC 规范不包括对
BigInteger的支持;您要么需要使用不同的数据类型(例如比例为 0 的 BigDecimal),要么查看 PostgreSQL 驱动程序是否提供了一些特定于实现的方法来设置 BigInteger 值。 -
尝试将其作为Number 传递,也许可行?
-
尝试改用 java.lang.Long。
-
@Mark Rotteveel,介意发表您的评论作为答案,这样我可以接受吗?
标签: java spring postgresql jdbc postgresql-9.2