11package com .clickhouse .examples .jdbc ;
22
3-
4- import com .clickhouse .client .api .ClientException ;
53import com .clickhouse .jdbc .ClickHouseDataSource ;
64import com .zaxxer .hikari .HikariConfig ;
75import com .zaxxer .hikari .HikariDataSource ;
6+ import org .slf4j .Logger ;
7+ import org .slf4j .LoggerFactory ;
88
99import java .sql .Connection ;
1010import java .sql .Date ;
1313import java .sql .ResultSet ;
1414import java .sql .SQLException ;
1515import java .sql .Statement ;
16+ import java .time .ZonedDateTime ;
1617import java .util .Collections ;
1718import java .util .Properties ;
1819
1920public class Basic {
21+ private static final Logger log = LoggerFactory .getLogger (Basic .class );
2022 static final String TABLE_NAME = "jdbc_example_basic" ;
2123
2224 public static void main (String [] args ) {
23- String url = System .getProperty ("chUrl" , "jdbc:ch://localhost:18123 " );
25+ String url = System .getProperty ("chUrl" , "jdbc:ch://localhost:8123 " );
2426
2527 // Set user and password if needed
2628 Properties properties = new Properties ();
@@ -38,7 +40,7 @@ public static void main(String[] args) {
3840 //Using HikariCP with ClickHouseDataSource
3941 usedPooledConnection (url , properties );
4042 } catch (SQLException e ) {
41- e . printStackTrace ( );
43+ log . error ( "Error" , e );
4244 }
4345 }
4446
@@ -47,30 +49,28 @@ static void createTable(String url, Properties properties) throws SQLException {
4749 try (Statement stmt = conn .createStatement ()) {//Create a statement
4850 stmt .execute ("DROP TABLE IF EXISTS " + TABLE_NAME );//Execute a query to drop the table if it exists
4951 stmt .execute ("CREATE TABLE " + TABLE_NAME +
50- " (`date` Date , `id` UInt32, `name` String, `attributes` Map(String, String))" +
52+ " (`date` DateTime64(3) , `id` UInt32, `name` String, `attributes` Map(String, String))" +
5153 " ENGINE = MergeTree() ORDER BY id" );//Create a table with three columns: date, id, and name
5254 }
5355 }
5456 }
5557
5658 static void insertDateWithPreparedStatement (String url , Properties properties ) throws SQLException {
5759 try (Connection conn = DriverManager .getConnection (url , properties )) {//Grab a connection using the jdbc DriverManager
58- try (Statement stmt = conn .createStatement ()) {
59- try (PreparedStatement pstmt = conn .prepareStatement ("INSERT INTO " + TABLE_NAME + " VALUES(?, ?, ?, ?)" )) {//Create a prepared statement
60- pstmt .setDate (1 , Date .valueOf ("2025-01-01" ));//Set the first parameter to '2025-01-01' (using java.sql.Date)
61- pstmt .setInt (2 , 1 );//Set the second parameter to 1
62- pstmt .setString (3 , "Alice" );//Set the third parameter to "Alice"
63- pstmt .setObject (4 , Collections .singletonMap ("key1" , "value1" ));
64- pstmt .addBatch ();//Add the current parameters to the batch
65-
66- pstmt .setDate (1 , Date .valueOf ("2025-02-01" ));//Set the first parameter to '2025-02-01'
67- pstmt .setInt (2 , 2 );//Set the second parameter to 2
68- pstmt .setString (3 , "Bob" );//Set the third parameter to "Bob"
69- pstmt .setObject (4 , Collections .singletonMap ("key2" , "value2" ));
70- pstmt .addBatch ();//Add the current parameters to the batch
71-
72- pstmt .executeBatch ();//Execute the batch
73- }
60+ try (PreparedStatement pstmt = conn .prepareStatement ("INSERT INTO " + TABLE_NAME + " VALUES(?, ?, ?, ?)" )) {//Create a prepared statement
61+ pstmt .setDate (1 , Date .valueOf ("2025-01-01" ));//Set the first parameter to '2025-01-01' (using java.sql.Date)
62+ pstmt .setInt (2 , 1 );//Set the second parameter to 1
63+ pstmt .setString (3 , "Alice" );//Set the third parameter to "Alice"
64+ pstmt .setObject (4 , Collections .singletonMap ("key1" , "value1" ));
65+ pstmt .addBatch ();//Add the current parameters to the batch
66+
67+ pstmt .setObject (1 , ZonedDateTime .now ());
68+ pstmt .setInt (2 , 2 );//Set the second parameter to 2
69+ pstmt .setString (3 , "Bob" );//Set the third parameter to "Bob"
70+ pstmt .setObject (4 , Collections .singletonMap ("key2" , "value2" ));
71+ pstmt .addBatch ();//Add the current parameters to the batch
72+
73+ pstmt .executeBatch ();//Execute the batch
7474 }
7575 }
7676 }
@@ -80,7 +80,9 @@ static void printInsertedData(String url, Properties properties) throws SQLExcep
8080 try (Statement stmt = conn .createStatement ()) {
8181 try (ResultSet rs = stmt .executeQuery ("SELECT * FROM " + TABLE_NAME )) {
8282 while (rs .next ()) {
83- System .out .println (rs .getDate (1 ) + ", " + rs .getInt (2 ) + ", " + rs .getString (3 ) + ", " + rs .getObject (4 ));
83+ //Print the values of the current row
84+ log .info ("DateTime: {}, Int: {}, String: {}, Object: {}" ,
85+ rs .getObject (1 , ZonedDateTime .class ), rs .getInt (2 ), rs .getString (3 ), rs .getObject (4 ));
8486 }
8587 }
8688 }
@@ -101,9 +103,11 @@ static void usedPooledConnection(String url, Properties properties) throws SQLEx
101103 try (HikariDataSource ds = new HikariDataSource (poolConfig );
102104 Connection conn = ds .getConnection ();
103105 Statement s = conn .createStatement ();
104- ResultSet rs = s .executeQuery ("SELECT 123" )) {
105- System .out .println (rs .next ());
106- System .out .println (rs .getInt (1 ));
106+ ResultSet rs = s .executeQuery ("SELECT * FROM system.numbers LIMIT 3" )) {
107+ while (rs .next ()) {
108+ // handle row
109+ log .info ("Integer: {}, String: {}" , rs .getInt (1 ), rs .getString (1 ));//Same column but different types
110+ }
107111 }
108112 }
109113
@@ -113,10 +117,10 @@ static void setClientSettings(Properties properties){
113117 try (Connection conn = DriverManager .getConnection (url , properties )) {
114118 try (Statement stmt = conn .createStatement ()) {
115119 try (ResultSet rs = stmt .executeQuery ("SELECT 1, 'Hello, world!'" )) {
116- 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
120+ while (rs .next ()) {
121+ log . info ( "Integer: {}" , rs .getInt (1 ));
122+ log . info ( "String: {}" , rs .getString (2 ));
123+ }
120124 }
121125 }
122126 } catch (SQLException e ) {
0 commit comments