(Exception) java.sql.SQLException: ResultSet.next is not called

Keywords: Database JDBC

When using ResultSet to receive a result set from a database query, even if the result set has only one data, the resultSet.next() function is needed to move the cursor to get the data.

Otherwise, the ResultSet.next exception will be reported not to be invoked.

Wrong writing

try {
    pstm=connection.prepareStatement("select * from account where card_id=?");
    pstm.setInt(1, card_id);
			
    resultSet = pstm.executeQuery();
        account.setCard_id(resultSet.getInt(1));
        account.setUsername(resultSet.getString(2));
        account.setPassword(resultSet.getString(3));
        account.setBalance(resultSet.getDouble(4));
        account.setMobile(resultSet.getString(5));

} 

Correct writing

    pstm=connection.prepareStatement("select * from account where card_id=?");
    pstm.setInt(1, card_id);
    resultSet = pstm.executeQuery();
    while (resultSet.next()) {
        account.setCard_id(resultSet.getInt(1));
        account.setUsername(resultSet.getString(2));
        account.setPassword(resultSet.getString(3));
        account.setBalance(resultSet.getDouble(4));
        account.setMobile(resultSet.getString(5));
    }

ResultSet.next annotation information

public interface ResultSet extends Wrapper, AutoCloseable {

    /**
     * Moves the cursor forward one row from its current position.
     * A <code>ResultSet</code> cursor is initially positioned
     * before the first row; the first call to the method
     * <code>next</code> makes the first row the current row; the
     * second call makes the second row the current row, and so on.
     * <p>
     * When a call to the <code>next</code> method returns <code>false</code>,
     * the cursor is positioned after the last row. Any
     * invocation of a <code>ResultSet</code> method which requires a
     * current row will result in a <code>SQLException</code> being thrown.
     *  If the result set type is <code>TYPE_FORWARD_ONLY</code>, it is vendor specified
     * whether their JDBC driver implementation will return <code>false</code> or
     *  throw an <code>SQLException</code> on a
     * subsequent call to <code>next</code>.
     *
     * <P>If an input stream is open for the current row, a call
     * to the method <code>next</code> will
     * implicitly close it. A <code>ResultSet</code> object's
     * warning chain is cleared when a new row is read.
     *
     * @return <code>true</code> if the new current row is valid;
     * <code>false</code> if there are no more rows
     * @exception SQLException if a database access error occurs or this method is
     *            called on a closed result set
     */
    boolean next() throws SQLException;

You can see the second and third lines in the method annotations for this interface

A <code>ResultSet</code> cursor is initially positioned
     * before the first row;

The cursor is initially located in front of the first line.

Therefore, it is not advisable to retrieve data from resultSet without calling the next method.

Posted by feckless on Sun, 03 Feb 2019 13:03:16 -0800