Amazon Athena
User Guide  | API Reference

Examples: Using the JDBC Driver with the JDK

The following code examples demonstrate how to use the JDBC driver in a Java application. This example assumes that the AWS JAVA SDK is included in your classpath, specifically the aws-java-sdk-core module, which includes the authorization packages (com.amazonaws.auth.*) referenced in the example.

Example: Creating a Driver#

Properties info = new Properties();
info.put("user", "AWSAccessKey");
info.put("password", "AWSSecretAccessKey");
info.put("s3_staging_dir", "s3://S3 Bucket Location/");


Connection connection = DriverManager.getConnection("jdbc:awsathena://", info);

Example: Using a Credentials Provider#

The following examples demonstrate different ways to use a credentials provider that implements the AWSCredentialsProvider interface with the JDBC.

Credentials provider with a single argument

Properties myProps = new Properties();

In this case, the filename called /Users/myUser/.athenaCredentials should contain the following:

accessKey = ACCESSKEY
secretKey = SECRETKEY

You replace the right part of the assignments with your account's AWS access and secret keys.

Credentials provider with multiple arguments

This example shows an example credentials provider, CustomSessionsCredentialsProvider, that uses an access and secret key in addition to a session token. CustomSessionsCredentialsProvider is shown for example only and is not included in the driver. The signature of the class looks like the following:

public CustomSessionCredentialsProvider(String accessId, String secretKey, String token)

You would then set the properties as follows:

Properties myProps = new Properties();
String providerArgs = "My_Access_Key," + "My_Secret_Key," + "My_Token";

Using InstanceProfileCredentialsProvider

If you use the InstanceProfileCredentialsProvider, you don't need to supply any credential provider arguments because they are provided using the EC2 instance profile for the instance on which you are running your application. You would still set the aws_credentials_provider_class property to this class name, however.

Example: Executing a SELECT Query#

Statement statement = connection.createStatement();
ResultSet queryResults = statement.executeQuery("SELECT * FROM cloudfront_logs");

Example: Running CREATE/ALTER Statements#

Statement statement = connection.createStatement();
ResultSet queryResults = statement.executeQuery("CREATE EXTERNAL TABLE tableName ( Col1 String ) LOCATION ‘s3://bucket/tableLocation");

Example: Listing Tables#

import java.sql.*;
import java.util.Properties;
import com.amazonaws.athena.jdbc.AthenaDriver;
import com.amazonaws.auth.PropertiesFileCredentialsProvider;

public class AthenaJDBCDemo {

  static final String athenaUrl = "jdbc:awsathena://";

  public static void main(String[] args) {

      Connection conn = null;
      Statement statement = null;

      try {
          Properties info = new Properties();
          info.put("s3_staging_dir", "s3://my-athena-result-bucket/test/");
          info.put("log_path", "/Users/myUser/.athena/athenajdbc.log");
          String databaseName = "default";

          System.out.println("Connecting to Athena...");
          conn = DriverManager.getConnection(athenaUrl, info);

          System.out.println("Listing tables...");
          String sql = "show tables in "+ databaseName;
          statement = conn.createStatement();
          ResultSet rs = statement.executeQuery(sql);

          while ( {
              //Retrieve table column.
              String name = rs.getString("tab_name");

              //Display values.
              System.out.println("Name: " + name);
      } catch (Exception ex) {
      } finally {
          try {
              if (statement != null)
          } catch (Exception ex) {

          try {
              if (conn != null)
          } catch (Exception ex) {

      System.out.printf("Finished connectivity test.");