11package com .clickhouse .examples .jdbc ;
22
33
4+ import com .clickhouse .client .api .ClientException ;
45import com .clickhouse .jdbc .ClickHouseDataSource ;
56import com .zaxxer .hikari .HikariConfig ;
67import com .zaxxer .hikari .HikariDataSource ;
@@ -61,9 +62,17 @@ public static void main(String[] args) {
6162 try {
6263 //Using HikariCP with ClickHouseDataSource
6364 usedPooledConnection (url , properties );
64- } catch (SQLException e ) {
65+ } catch (Exception e ) {
6566 e .printStackTrace ();
6667 }
68+
69+ try {
70+ //Customizing client settings
71+ setClientSettings ();
72+ } catch (ClientException e ) {
73+ // Ignore
74+ System .out .println (e .getMessage ());
75+ }
6776 }
6877
6978 static void usedPooledConnection (String url , Properties properties ) throws SQLException {
@@ -84,4 +93,26 @@ static void usedPooledConnection(String url, Properties properties) throws SQLEx
8493 System .out .println (rs .getInt (1 ));
8594 }
8695 }
96+
97+ static void setClientSettings () {
98+ String url = System .getProperty ("chUrl" , "jdbc:ch://localhost:18123?jdbc_ignore_unsupported_values=true&socket_timeout=1" );
99+
100+ // Set user and password if needed
101+ Properties properties = new Properties ();
102+ properties .setProperty ("user" , System .getProperty ("chUser" , "default" ));
103+ properties .setProperty ("password" , System .getProperty ("chPassword" , "" ));
104+
105+ try (Connection conn = DriverManager .getConnection (url , properties )) {
106+ try (Statement stmt = conn .createStatement ()) {
107+ try (ResultSet rs = stmt .executeQuery ("SELECT sleep(3)" )) {
108+ // this will throw an exception
109+ // because the query takes more than 1 second
110+ // and the socket timeout is set to 1 second
111+ System .out .println (rs .next ());
112+ }
113+ }
114+ } catch (SQLException e ) {
115+ System .out .println (e .getMessage ());
116+ }
117+ }
87118}
0 commit comments