我有这个错误:由:java.sql.SQLException:无效列索引引起,当我想从resultSet中获取电子邮件时,以便通过循环搜索结果发送电子邮件,并逐个获取电子邮件。
public List<UserDto> getEmail() {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet searchResultSet = null;
try {
connection = getConnection();
preparedStatement = connection.prepareStatement(
"SELECT EMAIL FROM USER WHERE USER.U_SEQ IN ('1','650')");
searchResultSet = preparedStatement.executeQuery();
return getEmail(searchResultSet);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
preparedStatement.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
private List<UserDto> getEmail(ResultSet searchResultSet) throws SQLException {
List<UserDto> result = new ArrayList<UserDto >();
UserDto userDto = null;
int index = 1;
while (searchResultSet.next()) {
userDto = new UserDto();
userDto .setEmailAddress(searchResultSet.getString(index));
result.add(userDto);
index++;
}
return result;
}
第二个我调用getEmail方法的类:
Delegate delegate = new Delegate();
UserDto userDto = new UserDto();
List<UserDto> users = delegate.getEmail();
delegate.sendNotification("****", "****", users .toString(), "", "",
"", body);
getEmail(ResultSet searchResultSet)函数失败
为什么要增加索引值?
索引变量是列索引,不是行索引。
在while循环中使用.next()遍历结果集。
保持索引值为1,永远不要改变。