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 wasNull throws exception on insert row #816

Closed
mrotteveel opened this issue Sep 17, 2024 · 3 comments
Closed

ResultSet wasNull throws exception on insert row #816

mrotteveel opened this issue Sep 17, 2024 · 3 comments

Comments

@mrotteveel
Copy link
Member

As reported on firebird-java - Error: No row available for wasNull., the method wasNull throws an exception when on the insert row.

This can be reproduced with the program provided by Pierre Vacher:

    public void testWasNull(java.sql.Connection connection) {
        String select = "select * from t1";
        try {
            populateTable(connection);

            java.sql.PreparedStatement ps = connection.prepareStatement(select,
                                                                        java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
                                                                        java.sql.ResultSet.CONCUR_UPDATABLE);

            java.sql.ResultSet rs = ps.executeQuery();

            rs.moveToInsertRow();
            rs.updateNull(1);
            rs.updateString(2, "s10");

            for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                Object value = rs.getObject(i);
                if (rs.wasNull()) {
                    System.out.println("SensitiveResultSet.testWasNull() Value was Null");
                }
                else {
                    System.out.println("SensitiveResultSet.testWasNull() Value: " + value.toString());
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    void populateTable(java.sql.Connection connection) throws SQLException {
        java.sql.Statement statement = connection.createStatement();
        //statement.execute("drop table t1 if exists");
        statement.execute("create table t1 (i int primary key, v varchar(10))");
        statement.close();

        String insert = "insert into t1 values(?, ?)";
        java.sql.PreparedStatement ps = connection.prepareStatement(insert);

        for (int i = 0; i < 10; i++) {
            ps.setInt(1, i);
            ps.setString(2, String.valueOf(i) + " s");
            ps.execute();
        }

        ps.close();
    }

producing the exception:

java.sql.SQLException: No row available for wasNull.
        at org.firebirdsql.jdbc.FBResultSet.wasNull(FBResultSet.java:465)
        at io.github.prrvchr.jdbcdriver.resultset.SensitiveResultSet.testWasNull(SensitiveResultSet.java:117)
        at io.github.prrvchr.jdbcdriver.resultset.SensitiveResultSet.<init>(SensitiveResultSet.java:96)
        at io.github.prrvchr.jdbcdriver.resultset.ResultSetHelper.getResultSet(ResultSetHelper.java:77)
        at io.github.prrvchr.uno.sdbcx.RowSetSuper.<init>(RowSetSuper.java:69)
        at io.github.prrvchr.uno.sdb.RowSet.<init>(RowSet.java:58)
        at io.github.prrvchr.uno.sdb.PreparedStatement.getResultSet(PreparedStatement.java:93)
        at io.github.prrvchr.uno.sdbc.PreparedStatementMain.executeQuery(PreparedStatementMain.java:404)
@mrotteveel
Copy link
Member Author

The javadoc for wasNull() doesn't mention any requirement to throw an exception if not currently in a row:

Reports whether the last column read had a value of SQL NULL. Note that you must first call one of the getter methods on a column to try to read its value and then call the method wasNull to see if the value read was SQL NULL.

Returns:
true if the last column value read was SQL NULL and false otherwise
Throws:
SQLException - if a database access error occurs or this method is called on a closed result set

So possibly just dropping that check is the simplest and correct fix.

@mrotteveel
Copy link
Member Author

To be frontported to Jaybird 6 as well.

@prrvchr
Copy link

prrvchr commented Sep 19, 2024

Thanks for this fix and the archives made available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants