Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ResultSet.beforeFirst() Unexpectedly Succeeds on TYPE_FORWARD_ONLY ResultSet #597

Open
dwenking opened this issue Nov 16, 2023 · 0 comments

Comments

@dwenking
Copy link

According to JDBC specifications, invoking beforeFirst() on a TYPE_FORWARD_ONLY ResultSet should either throw a SQLException or have no effect, as TYPE_FORWARD_ONLY ResultSets do not support cursor movements other than forward.
In the provided test case, a ResultSet is created with TYPE_FORWARD_ONLY and CONCUR_READ_ONLY parameters. After iterating through the ResultSet using rs.next(), an attempt is made to reset the cursor to the beginning of the ResultSet using rs.beforeFirst(). Contrary to expectations, this operation does not throw an error and appears to succeed.

@Test
public void test() {
    try (Connection con = DriverManager.getConnection("jdbc:pgsql://localhost:5432/test13?user=user&password=password")) {
        Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        stmt.execute("CREATE TABLE table13_0 (id INT PRIMARY KEY, name VARCHAR(20))");
        stmt.execute("INSERT INTO table13_0 VALUES (1, 'name1'), (2, 'name2'), (3, 'name3')");
        ResultSet rs = stmt.executeQuery("SELECT * FROM table13_0");
        while (rs.next()) {
            System.out.println(rs.getInt(1) + " " + rs.getString(2));
        }
        rs.beforeFirst(); // expected throw error for ResultSet.TYPE_FORWARD_ONLY
        while (rs.next()) {
            System.out.println(rs.getInt(1) + " " + rs.getString(2));
        }
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant