View a markdown version of this page

Get started with the JDBC 4.x driver - Amazon Athena

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.

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.

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

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.

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

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

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

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.

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

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