java - 如何使用不同类型的值向 java new Object[] 添加动态值
问题描述
我目前正在使用如下这种语法工作正常
Object[] oParams = new Object[] {
Integer.valueOf(this.userId),
Date.valueOf(this.startDate),
Date.valueOf(this.endDate)
};
jdbcTemplate.queryForList(sql, oParams);
如果它在PHP
我们可以简单地添加索引但是在java中下面的语法是抛出错误
oParams[3] = Integer.valueOf(this.userId)
如果我添加如下值,
List<Object> oParams = new ArrayList<Object>();
oParams.add(Integer.valueOf(this.userId));
oParams.add(Date.valueOf(this.startDate));
oParams.add(Date.valueOf(this.endDate));
jdbcTemplate.queryForList(sql, oParams);
它导致以下错误,
org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.util.ArrayList. Use setObject() with an explicit Types value to specify the type to use.
at org.postgresql.jdbc.PgPreparedStatement.setObject(PgPreparedStatement.java:949)
提前致谢。
解决方案
好吧..如果你真的需要一个Object[]
你可以转换你的List<Object>
using .toArray()
。
List<Object> oParams = new ArrayList<>();
oParams.add(Integer.valueOf(this.userId));
oParams.add(Date.valueOf(this.startDate));
oParams.add(Date.valueOf(this.endDate));
jdbcTemplate.queryForList(sql, oParams.toArray());
推荐阅读
- ios - 如何在 UITableView Swift 中删除所有类型的单元格
- sql - 如何选择间隔为 30 秒的时间戳之间的数据
- r - 从大型数据集的数据框有效地创建矩阵
- cassandra - 数据迁移到 cassandra 时如何处理错误
- html - angular 5 中动态元素的可访问性
- java - Spring-retry @Recover method works only in interface definition
- scala - Null values from a csv on Scala and Apache Spark
- mysql - Add two columns from a table as a foreign key in another
- vbscript - 如何使用 sendkeys vbs 添加 X=msgbox 命令
- java - JDBC 连接突然开始检查严格类型并抛出 java.sql.SQLException 类型不兼容