PreparedStatement 查詢時無效的列索引
在使用PreparedStatement 僅對oracle數據庫進行查詢是在 ps.setObject(i, values[i]);老是提示無效的列索引sql
錯誤代碼代碼
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);
}
} oracle
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, this
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, valuesget
[i]);這樣就ok了。還有在網上也看到有人出現一樣的問題,但不是我這樣的問題,而是下標的數量與?的數量不一致,或者將?寫成io
了?class