# Connect to Managed ClickHouse® with Java


Learn how to connect to your Managed ClickHouse® service with Java
using the ClickHouse JDBC driver and the HTTPS port.

## Prerequisites

-   [Java 8](https://www.java.com/en/download/) or later
-   [ClickHouse JDBC
    driver](https://github.com/ClickHouse/clickhouse-jdbc/tree/master/clickhouse-jdbc)

## Identify connection information

To run the code for connecting to your service, first identify values of
the following variables:

|        Variable         |                Description                |
|-------------------------|-------------------------------------------|
| `CLICKHOUSE_HTTPS_HOST` | `Host` for the ClickHouse connection      |
| `CLICKHOUSE_HTTPS_PORT` | `Port` for the ClickHouse connection      |
| `CLICKHOUSE_USER`       | `User` for the ClickHouse connection      |
| `CLICKHOUSE_PASSWORD`   | `Password` for the ClickHouse connection  |

Read the host and the port from the `clickhouse_https` entry of the service
components. Ports are assigned per service, so read them from your own service
rather than reusing a value from an example:

```bash
exo x get-dbaas-service-clickhouse my-clickhouse -z ch-gva-2 \
  -q "components[?component=='clickhouse_https']"
```

Rather than hard-coding the password in your source, read it into an environment
variable. The `--raw` flag returns the bare value, without JSON quotes or a
trailing newline:

```bash
export CH_PASS=$(exo x reveal-dbaas-clickhouse-user-password my-clickhouse avnadmin \
  -z ch-gva-2 -q 'password' --raw)
```

## Connect to the service

1.  Add the ClickHouse JDBC driver to your Maven dependencies.

    ```xml
    <dependency>
        <groupId>com.clickhouse</groupId>
        <artifactId>clickhouse-jdbc</artifactId>
        <version>0.3.2-patch11</version>
        <classifier>all</classifier>
        <exclusions>
            <exclusion>
                <groupId>*</groupId>
                <artifactId>*</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    ```

2.  Build the JDBC connection string, replacing `CLICKHOUSE_HTTPS_HOST` and
    `CLICKHOUSE_HTTPS_PORT` with the host and port of the `clickhouse_https`
    component of your service. You use this string in the code below.

    ```text
    jdbc:ch://CLICKHOUSE_HTTPS_HOST:CLICKHOUSE_HTTPS_PORT?ssl=true
    ```

3.  Replace `CLICKHOUSE_USER` in the code with the user of your service, then
    run the code. The password is read from the `CH_PASS` environment variable
    exported above, so it stays out of your source and out of your shell
    history.

    ```java
    import com.clickhouse.jdbc.ClickHouseConnection;
    import com.clickhouse.jdbc.ClickHouseDataSource;

    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    public class Main {
        public static void main(String[] args) throws SQLException {
            String connString = "jdbc:ch://CLICKHOUSE_HTTPS_HOST:CLICKHOUSE_HTTPS_PORT?ssl=true&sslmode=STRICT";
            ClickHouseDataSource database = new ClickHouseDataSource(connString);
            ClickHouseConnection connection = database.getConnection("CLICKHOUSE_USER", System.getenv("CH_PASS"));
            Statement statement = connection.createStatement();
            ResultSet result_set = statement.executeQuery("SELECT 1 AS one");
            while (result_set.next()) {
                System.out.println(result_set.getInt("one"));
            }
        }
    }
    ```

Now you have your service connection set up and you can proceed to
[uploading data into your database](/product/dbaas/service-specific/clickhouse/quick-start/#load-a-dataset).

For information on how to connect to the Managed ClickHouse service
with the ClickHouse client, see
[Connect with the ClickHouse client](/product/dbaas/service-specific/clickhouse/how-to/connect-with-clickhouse-cli/).

