Help Center/ GaussDB(for MySQL)/ User Guide/ HTAP Analysis/ Connecting to an HTAP Instance/ Connecting to an HTAP Instance Through JDBC
Updated on 2024-03-01 GMT+08:00

Connecting to an HTAP Instance Through JDBC

Prerequisites

  • You are familiar with:
    • Computer basics
    • Java
    • JDBC knowledge
  • You have downloaded official ClickHouse JDBC driver
  • You can use the following command to connect to an HTAP instance through JDBC:

    jdbc:clickhouse://<instance_ip>:<instance_port>/<database_name>

    Parameter

    Description

    <instance_ip>

    IP address of the instance.

    NOTE:
    • If you are accessing the instance through ECS, instance_ip is the private IP address of the instance. You can view the private IP address in the Network Information area on the Basic Information.
    • If you are accessing the instance through a public network, instance_ip indicates the EIP that has been bound to the instance. You can view the private IP address in the Network Information area on the Basic Information.

    <instance_port>

    Database port of the instance. The default port is 8123.

    NOTE:

    You can view the private IP address in the Network Information area on the Basic Information.

    <database_name>

    Database name used for connecting to the instance.

Sample Code

The following is an example of the Java code you could use for connecting to an HTAP instance. For details about other languages, see ClickHouse official document.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
public class JDBCTest {
    static final String IP = "*.*.*.*"; //IP address of the instance
    //There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables.
    //In this example, the username and password are stored in the environment variables. Before running the code, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed.
    static final String USER =System.getenv("EXAMPLE_USERNAME_ENV"); //username
    static final String PASS = System.getenv("EXAMPLE_PASSWORD_ENV"); //password
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        String url = "jdbc:clickhouse://" + IP + ":8123";
        try {
            Class.forName("ru.yandex.clickhouse.ClickHouseDriver");
            conn = DriverManager.getConnection(url, USER, PASS);
            stmt = conn.createStatement();
            String sql = "show databases;";
            ResultSet rs = stmt.executeQuery(sql);
            int columns = rs.getMetaData().getColumnCount();
            for (int i = 1; i <= columns; i++) {
                System.out.print(rs.getMetaData().getColumnName(i));
                System.out.print("\t");
            }
            while (rs.next()) {
                System.out.println();
                for (int i = 1; i <= columns; i++) {
                    System.out.print(rs.getObject(i));
                    System.out.print("\t");
                }
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // release resource ....
        }
    }
}