From 93a80ed8733b4dd67d0117de0316811655cd81f1 Mon Sep 17 00:00:00 2001 From: Paultagoras Date: Mon, 20 Jan 2025 02:59:24 -0500 Subject: [PATCH] Update Basic.java --- .../com/clickhouse/examples/jdbc/Basic.java | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/examples/jdbc/src/main/java/com/clickhouse/examples/jdbc/Basic.java b/examples/jdbc/src/main/java/com/clickhouse/examples/jdbc/Basic.java index cfb426966..4b8c1d402 100644 --- a/examples/jdbc/src/main/java/com/clickhouse/examples/jdbc/Basic.java +++ b/examples/jdbc/src/main/java/com/clickhouse/examples/jdbc/Basic.java @@ -1,6 +1,7 @@ package com.clickhouse.examples.jdbc; +import com.clickhouse.client.api.ClientException; import com.clickhouse.jdbc.ClickHouseDataSource; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; @@ -61,9 +62,17 @@ public static void main(String[] args) { try { //Using HikariCP with ClickHouseDataSource usedPooledConnection(url, properties); - } catch (SQLException e) { + } catch (Exception e) { e.printStackTrace(); } + + try { + //Customizing client settings + setClientSettings(); + } catch (ClientException e) { + // Ignore + System.out.println(e.getMessage()); + } } static void usedPooledConnection(String url, Properties properties) throws SQLException { @@ -84,4 +93,26 @@ static void usedPooledConnection(String url, Properties properties) throws SQLEx System.out.println(rs.getInt(1)); } } + + static void setClientSettings() { + String url = System.getProperty("chUrl", "jdbc:ch://localhost:18123?jdbc_ignore_unsupported_values=true&socket_timeout=1"); + + // Set user and password if needed + Properties properties = new Properties(); + properties.setProperty("user", System.getProperty("chUser", "default")); + properties.setProperty("password", System.getProperty("chPassword", "")); + + try (Connection conn = DriverManager.getConnection(url, properties)) { + try (Statement stmt = conn.createStatement()) { + try (ResultSet rs = stmt.executeQuery("SELECT sleep(3)")) { + // this will throw an exception + // because the query takes more than 1 second + // and the socket timeout is set to 1 second + System.out.println(rs.next()); + } + } + } catch (SQLException e) { + System.out.println(e.getMessage()); + } + } }