Skip to content

Commit

Permalink
Update Basic.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Paultagoras committed Jan 20, 2025
1 parent 0ce7eee commit 93a80ed
Showing 1 changed file with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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());
}
}
}

0 comments on commit 93a80ed

Please sign in to comment.