uma
uma

Reputation: 93

ResultSet Invalid column index

I have this error: Caused by: java.sql.SQLException: Invalid column index, when I want to get the emails form the resultSet in order to send an emails by looping in the search result and get one email by one.

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;
}

second class that I call the getEmail method:

Delegate delegate = new Delegate();

UserDto userDto = new UserDto();

List<UserDto> users = delegate.getEmail();

delegate.sendNotification("****", "****", users .toString(), "", "",
                   "", body);


Upvotes: 0

Views: 269

Answers (1)

szeak
szeak

Reputation: 335

The failure is in getEmail(ResultSet searchResultSet) function.

Why are you increasing the index value?

The index variable is the column index, not row index.

You loop through the result set with the .next() in your while cycle.

Keep index value on 1, and never change it.

Upvotes: 4

Related Questions