# Interface Connection (2.65.0)

2.65.0 (latest) 2.62.0 2.61.0 2.60.0 2.59.0 2.58.0 2.57.2 2.56.0 2.55.3 2.54.2 2.53.0 2.52.0 2.51.0 2.50.1 2.49.0 2.48.1 2.47.0 2.46.0 2.45.0 2.44.0 2.43.3 2.42.3 2.41.0 2.40.3 2.39.1 2.38.2 2.37.2 2.36.0 2.35.0 2.34.2 2.33.2

    public interface Connection

A Connection is a session between a Java application and BigQuery. SQL statements are executed
and results are returned within the context of a connection.

## Methods

### close()

    public abstract boolean close()

> [!WARNING]
> **Beta**
>
> This feature is covered by the [Pre-GA Offerings Terms](https://docs.cloud.google.com/terms/service-terms#1) of the Terms of Service. Pre-GA libraries might have limited support, and changes to pre-GA libraries might not be compatible with other pre-GA versions. For more information, see the launch stage descriptions.

Sends a query cancel request. This call will return immediately

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` |   |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuerySQLException` |   |

### dryRun(String sql)

    public abstract BigQueryDryRunResult dryRun(String sql)

> [!WARNING]
> **Beta**
>
> This feature is covered by the [Pre-GA Offerings Terms](https://docs.cloud.google.com/terms/service-terms#1) of the Terms of Service. Pre-GA libraries might have limited support, and changes to pre-GA libraries might not be compatible with other pre-GA versions. For more information, see the launch stage descriptions.

Execute a query dry run that returns information on the schema and query parameters of the
query results.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `sql` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` typically a static SQL SELECT statement |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryDryRunResult` |   |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuerySQLException` |   |

### executeSelect(String sql)

    public abstract BigQueryResult executeSelect(String sql)

> [!WARNING]
> **Beta**
>
> This feature is covered by the [Pre-GA Offerings Terms](https://docs.cloud.google.com/terms/service-terms#1) of the Terms of Service. Pre-GA libraries might have limited support, and changes to pre-GA libraries might not be compatible with other pre-GA versions. For more information, see the launch stage descriptions.

Execute a SQL statement that returns a single ResultSet.

Example of running a query.

{
@code
ConnectionSettings connectionSettings =
ConnectionSettings.newBuilder()
.setRequestTimeout(10L)
.setMaxResults(100L)
.setUseQueryCache(true)
.build();
Connection connection = bigquery.createConnection(connectionSettings);
String selectQuery = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;";
BigQueryResult bqResultSet = connection.executeSelect(selectQuery)
ResultSet rs = bqResultSet.getResultSet();
while (rs.next()) {
System.out.printf("%s,", rs.getString("corpus"));
}

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `sql` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` a static SQL SELECT statement |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryResult` | a ResultSet that contains the data produced by the query |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuerySQLException` |   |

### executeSelect(String sql, List\<Parameter\> parameters, Map\<String,String\>\[\] labels)

    public abstract BigQueryResult executeSelect(String sql, List<Parameter> parameters, Map<String,String>[] labels)

> [!WARNING]
> **Beta**
>
> This feature is covered by the [Pre-GA Offerings Terms](https://docs.cloud.google.com/terms/service-terms#1) of the Terms of Service. Pre-GA libraries might have limited support, and changes to pre-GA libraries might not be compatible with other pre-GA versions. For more information, see the launch stage descriptions.

This method executes a SQL SELECT query

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `sql` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` SQL SELECT query |
| `parameters` | `https://docs.oracle.com/javase/8/docs/api/java/util/List.html<https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Parameter>` named or positional parameters. The set of query parameters must either be all positional or all named parameters. |
| `labels` | `https://docs.oracle.com/javase/8/docs/api/java/util/Map.html<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html,https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>[]` (optional) the labels associated with this query. You can use these to organize and group your query jobs. Label keys and values can be no longer than 63 characters, can only contain lowercase letters, numeric characters, underscores and dashes. International characters are allowed. Label values are optional and Label is a Varargs. You should pass all the Labels in a single Map .Label keys must start with a letter and each label in the list must have a different key. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQueryResult` | BigQueryResult containing the output of the query |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuerySQLException` |   |

### executeSelectAsync(String sql)

    public abstract ListenableFuture<ExecuteSelectResponse> executeSelectAsync(String sql)

> [!WARNING]
> **Beta**
>
> This feature is covered by the [Pre-GA Offerings Terms](https://docs.cloud.google.com/terms/service-terms#1) of the Terms of Service. Pre-GA libraries might have limited support, and changes to pre-GA libraries might not be compatible with other pre-GA versions. For more information, see the launch stage descriptions.

Execute a SQL statement that returns a single ResultSet and returns a ListenableFuture to
process the response asynchronously.

Example of running a query.

{
@code
ConnectionSettings connectionSettings =
ConnectionSettings.newBuilder()
.setUseReadAPI(true)
.build();
Connection connection = bigquery.createConnection(connectionSettings);
String selectQuery = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` GROUP BY corpus;";
ListenableFuture\<ExecuteSelectResponse\> executeSelectFuture = connection.executeSelectAsync(selectQuery);
ExecuteSelectResponse executeSelectRes = executeSelectFuture.get();

if(!executeSelectRes.getIsSuccessful()){
throw executeSelectRes.getBigQuerySQLException();
}

BigQueryResult bigQueryResult = executeSelectRes.getBigQueryResult();
ResultSet rs = bigQueryResult.getResultSet();
while (rs.next()) {
System.out.println(rs.getString(1));
}

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `sql` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` a static SQL SELECT statement |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `com.google.common.util.concurrent.ListenableFuture<https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.ExecuteSelectResponse>` | a ListenableFuture that is used to get the data produced by the query |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuerySQLException` | upon failure |

### executeSelectAsync(String sql, List\<Parameter\> parameters, Map\<String,String\>\[\] labels)

    public abstract ListenableFuture<ExecuteSelectResponse> executeSelectAsync(String sql, List<Parameter> parameters, Map<String,String>[] labels)

> [!WARNING]
> **Beta**
>
> This feature is covered by the [Pre-GA Offerings Terms](https://docs.cloud.google.com/terms/service-terms#1) of the Terms of Service. Pre-GA libraries might have limited support, and changes to pre-GA libraries might not be compatible with other pre-GA versions. For more information, see the launch stage descriptions.

Execute a SQL statement that returns a single ResultSet and returns a ListenableFuture to
process the response asynchronously.

Example of running a query.

{
@code
ConnectionSettings connectionSettings =
ConnectionSettings.newBuilder()
.setUseReadAPI(true)
.build();
Connection connection = bigquery.createConnection(connectionSettings);
String selectQuery =
"SELECT TimestampField, StringField, BooleanField FROM "

- MY_TABLE
- " WHERE StringField = @stringParam"
- " AND IntegerField IN UNNEST(@integerList)";
  QueryParameterValue stringParameter = QueryParameterValue.string("stringValue");
  QueryParameterValue intArrayParameter =
  QueryParameterValue.array(new Integer\[\] {3, 4}, Integer.class);
  Parameter stringParam =
  Parameter.newBuilder().setName("stringParam").setValue(stringParameter).build();
  Parameter intArrayParam =
  Parameter.newBuilder().setName("integerList").setValue(intArrayParameter).build();
  List\<Parameter\> parameters = ImmutableList.of(stringParam, intArrayParam);

  ListenableFuture\<ExecuteSelectResponse\> executeSelectFut =
  connection.executeSelectAsync(selectQuery, parameters);
  ExecuteSelectResponse executeSelectRes = executeSelectFuture.get();

  if(!executeSelectRes.getIsSuccessful()){
  throw executeSelectRes.getBigQuerySQLException();
  }

  BigQueryResult bigQueryResult = executeSelectRes.getBigQueryResult();
  ResultSet rs = bigQueryResult.getResultSet();
  while (rs.next()) {
  System.out.println(rs.getString(1));
  }

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `sql` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` SQL SELECT query |
| `parameters` | `https://docs.oracle.com/javase/8/docs/api/java/util/List.html<https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.Parameter>` named or positional parameters. The set of query parameters must either be all positional or all named parameters. |
| `labels` | `https://docs.oracle.com/javase/8/docs/api/java/util/Map.html<https://docs.oracle.com/javase/8/docs/api/java/lang/String.html,https://docs.oracle.com/javase/8/docs/api/java/lang/String.html>[]` (optional) the labels associated with this query. You can use these to organize and group your query jobs. Label keys and values can be no longer than 63 characters, can only contain lowercase letters, numeric characters, underscores and dashes. International characters are allowed. Label values are optional and Label is a Varargs. You should pass all the Labels in a single Map .Label keys must start with a letter and each label in the list must have a different key. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `com.google.common.util.concurrent.ListenableFuture<https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.ExecuteSelectResponse>` | a ListenableFuture that is used to get the data produced by the query |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-bigquery/latest/com.google.cloud.bigquery.BigQuerySQLException` | upon failure |