10000 Statement.getUpdateCount() throws SQLException after getResultSet() for SELECT statements · Issue #205 · duckdb/duckdb-java · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Statement.getUpdateCount() throws SQLException after getResultSet() for SELECT statements #205

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

Closed
zhicwu opened this issue Apr 27, 2025 · 3 comments · Fixed by #207
Closed

Comments

@zhicwu
Copy link
zhicwu commented Apr 27, 2025

I got the following exception when calling Statement.getUpdateCount() after getResultSet() for SELECT statements:

Exception in thread "main" java.sql.SQLException: Prepare something first
	at org.duckdb.DuckDBPreparedStatement.getUpdateCountInternal(DuckDBPreparedStatement.java:402)
	at org.duckdb.DuckDBPreparedStatement.getUpdateCount(DuckDBPreparedStatement.java:414)
	at io.github.jdbcx.driver.WrappedStatement.getMoreResults(WrappedStatement.java:354)
	at io.github.jdbcx.Main.execute(Main.java:320)
	at io.github.jdbcx.Main.executeQueries(Main.java:350)
	at io.github.jdbcx.Main.process(Main.java:411)
	at io.github.jdbcx.Main.main(Main.java:468)

Could you please enhance the driver to align with the JDBC specification and return -1 from Statement.getUpdateCount() (and similarly getLargeUpdateCount) when "the result is a ResultSet object or there are no more results"?

staticlibs added a commit to staticlibs/duckdb-java that referenced this issue May 2, 2025
This change implements a group of `Large` methods, like
`executeLargeUpdate`, that were added in Java 8 (JDBC 4.2).

Testing: existing prepared statement tests are moved to a separate file
and updated to cover all `Large` methods.

Fixes: duckdb#205
@staticlibs
Copy link
Collaborator

@zhicwu

Thanks for the report! I wonder if you are calling getUpdateCount() on "unitialized" statement (before running the query)? Because the following returns -1 correctly both before and after reading the ResultSet:

try (Connection conn = DriverManager.getConnection("jdbc:duckdb:");
     Statement stmt = conn.createStatement()) {
    stmt.execute("SELECT 42");
    try (ResultSet rs = stmt.getResultSet()) {
        System.out.println(stmt.getUpdateCount());
        while (rs.next());
        System.out.println(stmt.getUpdateCount());
    }
}

About getLargeUpdateCount - this is a valid concern, this (along with other Large methods) should be fixed by #207.

@zhicwu
Copy link
Author
zhicwu commented May 2, 2025

Hi @staticlibs, thank you for the reply. While the statement should indeed be initialized beforehand, my point was about the potential state of the underlying statement at the moment getMoreResults() is called, as shown in the code below:

@Override
public boolean getMoreResults() throws SQLException {
    final boolean hasMore = stmt.getMoreResults(); // <-- this could be the trigger of the exception
    queryResult.reset();
    // getResultSet() succeed while a subsequent call to getUpdateCount() fails
    queryResult.updateAll(null, hasMore ? new WrappedResultSet(this, stmt.getResultSet()) : null,
            stmt.getUpdateCount());
    return hasMore;
}

There might be scenarios where the statement is no longer valid when attempting to retrieve the next result.

@staticlibs
Copy link
Collaborator

@zhicwu

I think you are right, there is no need to throw the exception here if it breaks client code, I will include the fix (to return -1 in this case) into #207 .

staticlibs added a commit to staticlibs/duckdb-java that referenced this issue May 2, 2025
This change implements a group of `Large` methods, like
`executeLargeUpdate`, that were added in Java 8 (JDBC 4.2).

Testing: existing prepared statement tests are moved to a separate file
and updated to cover all `Large` methods.

Fixes: duckdb#205
staticlibs added a commit that referenced this issue May 5, 2025
This change implements a group of `Large` methods, like
`executeLargeUpdate`, that were added in Java 8 (JDBC 4.2).

Testing: existing prepared statement tests are moved to a separate file
and updated to cover all `Large` methods.

Fixes: #205
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

Successfully merging a pull request may close this issue.

2 participants
0