

# Get started with the JDBC 4.x driver

For requirements and behavior on engine version 3 and DuckDB workgroups, see [Requirements and behavior by workgroup engine](jdbc-v4-driver-engine-comparison.md).

Set connection parameters in a JDBC URL or a `Properties` object passed to the driver.

This example configures a connection for an existing DuckDB workgroup with Athena-managed query results and the default AWS credentials provider chain. Replace the Region and workgroup with your own values.

```java
import com.amazon.athena.jdbc.AthenaDriver;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class AthenaExample {
    public static void main(String[] args) throws SQLException {
        Properties properties = new Properties();
        properties.setProperty("Region", "us-west-2");
        properties.setProperty("WorkGroup", "my-duckdb-workgroup");
        properties.setProperty("CredentialsProvider", "DefaultChain");
        try (Connection connection = new AthenaDriver().connect("jdbc:athena://", properties);
             Statement statement = connection.createStatement();
             ResultSet results = statement.executeQuery("SELECT 1 AS example_value")) {
            while (results.next()) {
                System.out.println(results.getInt("example_value"));
            }
        } catch (SQLException error) {
            System.err.println("Query failed: " + error.getMessage());
            throw error;
        }
    }
}
```

For an S3 query result location, set `OutputLocation` to the result bucket and prefix, or configure the workgroup result location.

## Configure the JVM for Arrow
<a name="jdbc-v4-driver-arrow-jvm-configuration"></a>

The `GetQueryResultStream` fetcher uses Apache Arrow off-heap buffers. On Java 9 or later, start the JVM with `--add-opens=java.base/java.nio=ALL-UNNAMED` to allow Arrow to allocate these buffers.

```bash
java --add-opens=java.base/java.nio=ALL-UNNAMED -cp "athena-jdbc-4.0.0-with-dependencies.jar:." AthenaExample
```

With `ResultFetcher=auto`, a DuckDB query using Athena-managed results falls back to `GetQueryResults` if Arrow buffers are unavailable.

Explicitly selecting `ResultFetcher=GetQueryResultStream` fails the connection if Arrow buffers are unavailable. Add the JVM flag or select `ResultFetcher=GetQueryResults`.

## Connect with AthenaDataSource
<a name="jdbc-v4-driver-data-source"></a>

Use `com.amazon.athena.jdbc.AthenaDataSource` when your application uses a data source instead of creating an `AthenaDriver` directly.

```java
AthenaDataSource source = new AthenaDataSource();
source.setRegion("us-west-2");
source.setWorkGroup("my-duckdb-workgroup");
source.setCredentialsProvider("DefaultChain");
try (Connection connection = source.getConnection()) {
    // Use the connection with the JDBC API.
}
```

## Bind query parameters
<a name="jdbc-v4-driver-prepared-statements"></a>

Use JDBC `PreparedStatement` setters to bind values to `?` placeholders. The driver counts placeholders using the selected engine dialect and renders bound values with that dialect.

```java
try (PreparedStatement statement =
         connection.prepareStatement("SELECT ? AS example_value")) {
    statement.setInt(1, 42);
    try (ResultSet results = statement.executeQuery()) {
        while (results.next()) {
            System.out.println(results.getInt("example_value"));
        }
    }
}
```

For DML, call `executeUpdate` to obtain the affected-row count. The Arrow path reads the count supplied for no-result write statements rather than exposing an internal count result as an application result set.

## Compile and run on your operating system
<a name="jdbc-v4-driver-platform-commands"></a>

Use the platform instructions below to compile and run the connection example.