@@ -27,12 +27,35 @@ public static void main(String[] args) {
2727 properties .setProperty ("user" , System .getProperty ("chUser" , "default" ));
2828 properties .setProperty ("password" , System .getProperty ("chPassword" , "" ));
2929
30+ try {
31+ createTable (url , properties );
32+ insertDateWithPreparedStatement (url , properties );
33+ printInsertedData (url , properties );
34+
35+ //Customizing client settings
36+ setClientSettings (properties );
37+
38+ //Using HikariCP with ClickHouseDataSource
39+ usedPooledConnection (url , properties );
40+ } catch (SQLException e ) {
41+ e .printStackTrace ();
42+ }
43+ }
44+
45+ static void createTable (String url , Properties properties ) throws SQLException {
3046 try (Connection conn = DriverManager .getConnection (url , properties )) {//Grab a connection using the jdbc DriverManager
3147 try (Statement stmt = conn .createStatement ()) {//Create a statement
3248 stmt .execute ("DROP TABLE IF EXISTS " + TABLE_NAME );//Execute a query to drop the table if it exists
33- stmt .execute ("CREATE TABLE " + TABLE_NAME + " (`date` Date, `id` UInt32, `name` String, `attributes` Map(String, String)) " +
34- "ENGINE = MergeTree() ORDER BY id" );//Create a table with three columns: date, id, and name
49+ stmt .execute ("CREATE TABLE " + TABLE_NAME +
50+ " (`date` Date, `id` UInt32, `name` String, `attributes` Map(String, String))" +
51+ " ENGINE = MergeTree() ORDER BY id" );//Create a table with three columns: date, id, and name
52+ }
53+ }
54+ }
3555
56+ static void insertDateWithPreparedStatement (String url , Properties properties ) throws SQLException {
57+ try (Connection conn = DriverManager .getConnection (url , properties )) {//Grab a connection using the jdbc DriverManager
58+ try (Statement stmt = conn .createStatement ()) {
3659 try (PreparedStatement pstmt = conn .prepareStatement ("INSERT INTO " + TABLE_NAME + " VALUES(?, ?, ?, ?)" )) {//Create a prepared statement
3760 pstmt .setDate (1 , Date .valueOf ("2025-01-01" ));//Set the first parameter to '2025-01-01' (using java.sql.Date)
3861 pstmt .setInt (2 , 1 );//Set the second parameter to 1
@@ -48,33 +71,23 @@ public static void main(String[] args) {
4871
4972 pstmt .executeBatch ();//Execute the batch
5073 }
74+ }
75+ }
76+ }
5177
78+ static void printInsertedData (String url , Properties properties ) throws SQLException {
79+ try (Connection conn = DriverManager .getConnection (url , properties )) {//Grab a connection using the jdbc DriverManager
80+ try (Statement stmt = conn .createStatement ()) {
5281 try (ResultSet rs = stmt .executeQuery ("SELECT * FROM " + TABLE_NAME )) {
5382 while (rs .next ()) {
5483 System .out .println (rs .getDate (1 ) + ", " + rs .getInt (2 ) + ", " + rs .getString (3 ) + ", " + rs .getObject (4 ));
5584 }
5685 }
5786 }
58- } catch (SQLException e ) {
59- e .printStackTrace ();
60- }
61-
62- try {
63- //Using HikariCP with ClickHouseDataSource
64- usedPooledConnection (url , properties );
65- } catch (Exception e ) {
66- e .printStackTrace ();
67- }
68-
69- try {
70- //Customizing client settings
71- setClientSettings ();
72- } catch (ClientException e ) {
73- // Ignore
74- System .out .println (e .getMessage ());
7587 }
7688 }
7789
90+
7891 static void usedPooledConnection (String url , Properties properties ) throws SQLException {
7992 // connection pooling won't help much in terms of performance,
8093 // because the underlying implementation has its own pool.
@@ -94,21 +107,16 @@ static void usedPooledConnection(String url, Properties properties) throws SQLEx
94107 }
95108 }
96109
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" , "" ));
110+ static void setClientSettings (Properties properties ){
111+ String url = System .getProperty ("chUrl" , "jdbc:ch://localhost:18123?jdbc_ignore_unsupported_values=true&socket_timeout=10" );
104112
105113 try (Connection conn = DriverManager .getConnection (url , properties )) {
106114 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
115+ try (ResultSet rs = stmt .executeQuery ("SELECT 1, 'Hello, world!'" )) {
111116 System .out .println (rs .next ());
117+ System .out .println (rs .getInt (1 ));
118+ System .out .println (rs .getString (2 ));
119+ System .out .println (rs .getClob (2 ));//Unsupported type, but ignore_unsupported_values=true, so it won't throw an exception
112120 }
113121 }
114122 } catch (SQLException e ) {
0 commit comments