我目前正在使用这种语法,如下所示
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)
谢谢。
答案 0 :(得分:1)
好吧。如果您确实需要Object[]
,则可以使用.toArray()
来转换List<Object>
。
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());