[英]Perform Update using Spring jdbctemplate
please suggest some way to perform the below query using jdbctemplate. 请提出一些使用jdbctemplate执行以下查询的方法。
update student set result='pass' where stud_id in (100,101,102);
i have tried the below, but stuck with invalid column type. 我已经尝试了以下方法,但卡在无效的列类型中。
i am passing the 我正在通过
String query = "UPDATE STUDENT SET RESULT = ? WHERE OBJ_ID IN ( SELECT * FROM TABLE )";
int[] stud_ids = new int[]{100,101,102};
getJdbcTemplate().query(updateStateSQL, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
final Connection con = getJdbcTemplate().getNativeJdbcExtractor().getNativeConnection(ps.getConnection());
ps.setString(1, 'PASS');
ps.setArray(2, stud_ids);
}
}, new RowMapper<String>() {
@Override
public String mapRow(ResultSet rs, int arg1) throws SQLException {
return rs.getString(1);
}
});
}
You wrote: 你写了:
String query = "UPDATE STUDENT SET RESULT = ? WHERE OBJ_ID IN ( SELECT * FROM TABLE )";
getJdbcTemplate().query(updateStateSQL, ...)
You didn't write the value of updateStateSQL
. 您没有写updateStateSQL
的值。 If it's the same as query
, then this cannot work: 如果与query
相同,那么它将无法正常工作:
ps.setString(1, 'PASS');
ps.setArray(2, stud_ids);
Because you would need precisely 2 question marks ( ?
) in the query and you have only one in your post. 因为在查询中只需要2个问号( ?
),而帖子中只有一个。
You could try this: 您可以尝试以下方法:
String query = "UPDATE STUDENT SET RESULT = ? WHERE OBJ_ID IN ( ? )";
But to be honest I've never used ps.setArray
and I'm not sure it will do what you expect. 但是,老实说,我从未使用过ps.setArray
,我不确定它是否能满足您的期望。
What should work is using NamedParameterJdbcTemplate
, something like this: 应该可以使用NamedParameterJdbcTemplate
,如下所示:
String sql = "UPDATE STUDENT SET RESULT = :result WHERE OBJ_ID IN ( :ids )";
Map<String, Object> params = new HashMap<String, Object>();
params.put("result", "PASS");
params.put("ids", Arrays.asList(new Integer[] {100, 101, 102}));
getSimpleJdbcTemplate().update(sql, params);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.