impossibl / pgjdbc-ng

A new JDBC driver for PostgreSQL aimed at supporting the advanced features of JDBC and Postgres
https://impossibl.github.io/pgjdbc-ng
Other
600 stars 108 forks source link

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

Open dwenking opened 10 months ago

dwenking commented 10 months ago

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