JDBC Driver - Statement

A java.sql.Statement sends SQL to the server and exposes its current result or update count. Use a PreparedStatement when SQL contains application data.

Execution and results

Method Behavior
executeQuery(sql) Returns a single relational ResultSet. Raises SQLException if the command has no relational result set.
executeUpdate(sql) Executes a statement that produces an update count or no row set, such as INSERT or CREATE TABLE. Returns the scalar count as an int.
execute(sql) Returns true when the current result is a ResultSet and false when it is an update count.
getResultSet() Returns the current result set or null.
getUpdateCount(), getLargeUpdateCount() Return the current update count or -1 when there is none. The large version returns a long.
getMoreResults(), getMoreResults(Statement.CLOSE_CURRENT_RESULT) Close the current result and return false. The driver exposes at most one result per statement execution. The other result retention flags are unsupported.
addBatch(sql), clearBatch() Queue an SQL update or clear queued SQL statements.
executeBatch(), executeLargeBatch() Send queued SQL updates and return counts in batch order as int[] or long[]. The driver clears the queue after execution.
executeLargeUpdate(sql) The driver does not override the JDBC default, which throws UnsupportedOperationException. The parameterless PreparedStatement variant is implemented.
getGeneratedKeys() and generated-key overloads of execute, executeUpdate and executeLargeUpdate Unsupported.

Reusing a statement closes its previous result. Close or consume a result before executing another command on the same statement.

Example: create a table and insert rows

Run this on a development instance where public.jdbc_docs_tasks does not yet exist. The other JDBC reference examples use these rows.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Arrays;

public class CreateTasks {
    public static void main(String[] args) throws Exception {
        String url = args.length == 0 ? "jdbc:polypheny://localhost:20590" : args[0];
        try (Connection connection = DriverManager.getConnection(url, "pa", "");
             Statement statement = connection.createStatement()) {
            statement.executeUpdate("CREATE TABLE public.jdbc_docs_tasks "
                + "(id INTEGER PRIMARY KEY, title VARCHAR(100), owner VARCHAR(100))");

            statement.addBatch("INSERT INTO public.jdbc_docs_tasks (id, title) VALUES (1, 'Write docs')");
            statement.addBatch("INSERT INTO public.jdbc_docs_tasks (id, title) VALUES (2, 'Review docs')");
            System.out.println(Arrays.toString(statement.executeBatch()));

            if (statement.execute("SELECT id, title FROM public.jdbc_docs_tasks ORDER BY id")) {
                try (ResultSet rows = statement.getResultSet()) {
                    while (rows.next()) {
                        System.out.printf("%d %s%n", rows.getInt("id"), rows.getString("title"));
                    }
                }
            }
        }
    }
}

Settings and lifecycle

Method Behavior
close(), isClosed() Release resources and inspect the statement’s local closed state.
closeOnCompletion(), isCloseOnCompletion() Set or read whether closing the statement’s result also closes the statement.
getConnection() Returns the parent JDBC connection.
setFetchSize(rows), getFetchSize() Set or read the fetch size for result frames. The default is 100. Negative values are rejected.
setFetchDirection(direction), getFetchDirection() The supported direction is ResultSet.FETCH_FORWARD. A scrollable cursor can still move backward across fetched rows.
setMaxFieldSize(bytes), getMaxFieldSize() Set or read the local field limit. Driver 3.1 truncates strings by Java string length and binary values by byte length. The default 0 imposes no field size limit.
setMaxRows(rows), getMaxRows(), setLargeMaxRows(rows), getLargeMaxRows() Set or read a row limit. The default 0 has no limit. Set a limit in SQL when the server needs to restrict the rows it produces.
setQueryTimeout(seconds), getQueryTimeout() Set or read a timeout in seconds. It participates in synchronous requests when both the query timeout and the connection network timeout are positive. Unparameterized statement execution does not enforce a query timeout.
cancel() Checks that the statement is open. It does not cancel server work in 3.1.
getResultSetType(), getResultSetConcurrency(), getResultSetHoldability() Report the settings chosen at creation.
setPoolable(poolable), isPoolable() Store or read the poolable flag. The driver does not provide statement pooling.
getWarnings(), clearWarnings() The driver does not track warnings. getWarnings() returns null.
setEscapeProcessing(enabled) Stores the flag. The driver sends SQL as supplied without rewriting JDBC escapes.
setCursorName(name) Unsupported.
isWrapperFor(type), unwrap(type) Check or obtain the implementation for a compatible type.
© Polypheny GmbH. All Rights Reserved.