DoubleCloud is winding down. Migrate to ClickHouse with limited-time free migration services. Contact us today ->->

Maven

    <dependency>
        <groupId>com.clickhouse</groupId>
        <artifactId>clickhouse-jdbc</artifactId>
        <version>0.3.2-patch11</version>
    </dependency>

Open a connection

    Properties prop = new Properties();
    prop.setProperty("username", "default");
    prop.setProperty("password", "<password>");
    Connection conn = DriverManager.getConnection("jdbc:ch:https://<host>:8443?insert_quorum=auto", prop);

Create a table

    Statement stmt = conn.createStatement();
    stmt.execute("CREATE TABLE IF NOT EXISTS jdbc_test(idx Int8, str String) ENGINE = MergeTree ORDER BY idx");

Write data to a table

    try (PreparedStatement ps = conn.prepareStatement(
            "insert into jdbc_test select col1, col2 from input('col1 Int8, col2 String')")) {
        for (int i = 0; i < 10; i++) {
            ps.setInt(1, i);
            ps.setString(2, "test:" + i); // col1
            // parameters will be write into buffered stream immediately in binary format
            ps.addBatch(); 
        }
        // stream everything on-hand into ClickHouse
        ps.executeBatch(); 
    }

Read data from a table

    ResultSet rs = stmt.executeQuery("select * from jdbc_test");
    while (rs.next()) {
        System.out.println(String.format("idx: %s str: %s", rs.getString(1), rs.getString(2)));
    }

Other integrations

Get started with ClickHouse Cloud for free

We'll get you started on a 30 day trial and $300 credits to spend at your own pace.