Skip to content

Connection.prepareStatement executes all but the last statement of multi-statement SQL #1004

Done

Description

@ebyhr

Description

java.sql.Connection.prepareStatement(String) is expected to only parse and plan. With multi-statement text, duckdb-java executes every statement except the last one during prepare. The side effects of those statements happen before execute() is called.

This is a security problem for applications that use prepare to inspect the shape of user-supplied SQL, for example calling getMetaData() to learn the result columns. A "metadata only" call performs writes.

Reproduction (duckdb_jdbc 1.5.6.0)

try (Connection c = DriverManager.getConnection("jdbc:duckdb:")) {
    try (PreparedStatement p = c.prepareStatement("CREATE TABLE z(i int); SELECT 1 AS x")) {
        System.out.println(p.getMetaData().getColumnCount());   // 1
        // execute() was never called
        try (Statement s = c.createStatement();
                ResultSet r = s.executeQuery("SELECT count(*) FROM duckdb_tables() WHERE table_name = 'z'")) {
            r.next();
            System.out.println(r.getInt(1));                    // 1: table z was created by prepare
        }
    }
}

Expected

prepareStatement has no side effects. It either rejects multi-statement text or prepares without executing. The count of z should be 0.

Actual

CREATE TABLE z is executed at prepare time.

Activity

  1. staticlibs commented on Nov 25, 2026

    @staticlibs
    Member

    Hi, thanks for the report!

    These multi-statement support is a UX feature, it was there from the beginning, before the prepared and non-prepared statements got separate implementation.

    I agree with the reasoning and filed #1008 to reject multi-statements on prepare.

    Side note:

    The following:

    prepareStatement has no side effects

    is correct for the core DuckDB, but not necessary correct for queries that go to extensions. Getting columns metadata ("binding" step) may or may not have side-effects in extensions (in core extensions we are striving for it to have no side effects). For example when odbc_scanner extension is used, preparing the query which contains odbc_query() table function will call "prepare" on a third-party ODBC driver, and some drivers will just run the query in remote DB (and discard the result) to get the column types.

  2. added a commit that references this issue on Nov 25, 2026
    80a62cb
  3. staticlibs commented on Nov 25, 2026

    @staticlibs
    Member

    Just FYI, the patched version (compatible with v2.0.1 extensions) is available in this repo:

    <dependency>
        <groupId>org.duckdb</groupId>
        <artifactId>duckdb_jdbc</artifactId>
        <version>2.0.1-dev-47396bf</version>
    </dependency>
    <repository>
        <id>duckdb</id>
        <url>https://duckdb-staging.duckdb.org/duckdb/duckdb-java/maven/</url>
    </repository>
  4. added a commit that references this issue on Nov 26, 2026
    2324985
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions