Skip to content

Commit b07d20b

Browse files
author
Paultagoras
committed
Updating for accuracy and helpfulness
1 parent 6e7d4ac commit b07d20b

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

examples/jdbc/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<name>JDBC Examples</name>
1313
<description>JDBC Examples</description>
1414
<url>https://github.com/ClickHouse/clickhouse-java</url>
15-
<inceptionYear>2022</inceptionYear>
15+
<inceptionYear>2025</inceptionYear>
1616

1717
<organization>
1818
<name>ClickHouse, Inc.</name>
@@ -43,7 +43,7 @@
4343
</repositories>
4444

4545
<properties>
46-
<project.current.year>2024</project.current.year>
46+
<project.current.year>2025</project.current.year>
4747
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4848
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
4949

@@ -74,7 +74,7 @@
7474
<dependency>
7575
<groupId>org.slf4j</groupId>
7676
<artifactId>slf4j-simple</artifactId>
77-
<version>2.0.13</version>
77+
<version>2.0.16</version>
7878
<scope>runtime</scope>
7979
</dependency>
8080
</dependencies>

examples/jdbc/src/main/java/com/clickhouse/examples/jdbc/Basic.java

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.clickhouse.examples.jdbc;
22

3-
4-
import com.clickhouse.client.api.ClientException;
53
import com.clickhouse.jdbc.ClickHouseDataSource;
64
import com.zaxxer.hikari.HikariConfig;
75
import com.zaxxer.hikari.HikariDataSource;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
88

99
import java.sql.Connection;
1010
import java.sql.Date;
@@ -13,14 +13,16 @@
1313
import java.sql.ResultSet;
1414
import java.sql.SQLException;
1515
import java.sql.Statement;
16+
import java.time.ZonedDateTime;
1617
import java.util.Collections;
1718
import java.util.Properties;
1819

1920
public 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

Comments
 (0)