PreparedStatement 查询时无效的列索引
在使用PreparedStatement 仅对oracle数据库进行查询是在 ps.setObject(i, values[i]);总是提示无效的列索引
错误代码代码
public List queryForBeans(final String sql, final Object... values) {
Connection connection = null;
try {
connection = super.getDataSource().getConnection();
PreparedStatement ps = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
if (values != null) {
for (int i = 0; i < values.length; i++) {
ps.setObject(i, values[i]);
}
}
ResultSet rs = ps.executeQuery();
RowSetDynaClass rsdc = new RowSetDynaClass(rs);
rs.close();
ps.close();
return rsdc.getRows();
} catch (SQLException ex) {
throw this.getExceptionTranslator().translate("", sql, ex);
} finally {
this.close(connection);
}
}
public List queryForBeans(final String sql, final Object... values) {
Connection connection = null;
try {
connection = super.getDataSource().getConnection();
PreparedStatement ps = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
if (values != null) {
for (int i = 0; i < values.length; i++) {
ps.setObject(i, values[i]);
}
}
ResultSet rs = ps.executeQuery();
RowSetDynaClass rsdc = new RowSetDynaClass(rs);
rs.close();
ps.close();
return rsdc.getRows();
} catch (SQLException ex) {
throw this.getExceptionTranslator().translate("", sql, ex);
} finally {
this.close(connection);
}
}
后来在网上收了下原来是ps的参数是从1开始的而不是从0开始的,所有总是报无效的列索引,改为 ps.setObject(i+1, values
[i]);这样就ok了。还有在网上也看到有人出现同样的问题,但不是我这样的问题,而是下标的数量与?的数量不一致,或者将?写成
了?