Skip to content

Commit 6f8f311

Browse files
author
Paultagoras
committed
Update Basic.java
1 parent 6a89570 commit 6f8f311

File tree

1 file changed

+9
-5
lines changed
  • examples/jdbc/src/main/java/com/clickhouse/examples/jdbc

1 file changed

+9
-5
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.sql.ResultSet;
99
import java.sql.SQLException;
1010
import java.sql.Statement;
11+
import java.util.Collections;
1112
import java.util.Properties;
1213

1314
public class Basic {
@@ -24,25 +25,28 @@ public static void main(String[] args) {
2425
try (Connection conn = DriverManager.getConnection(url, properties)) {//Grab a connection using the jdbc DriverManager
2526
try (Statement stmt = conn.createStatement()) {//Create a statement
2627
stmt.execute("DROP TABLE IF EXISTS " + TABLE_NAME);//Execute a query to drop the table if it exists
27-
stmt.execute("CREATE TABLE " + TABLE_NAME + " (date Date, id UInt32, name String) ENGINE = Memory");
28+
stmt.execute("CREATE TABLE " + TABLE_NAME + " (`date` Date, `id` UInt32, `name` String, `attributes` Map(String, String)) " +
29+
"ENGINE = MergeTree() ORDER BY id");//Create a table with three columns: date, id, and name
2830

29-
try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO " + TABLE_NAME + " VALUES(?, ?, ?)")) {//Create a prepared statement
30-
pstmt.setDate(1, new Date(System.currentTimeMillis()));//Set the first parameter to the current date (using java.sql.Date)
31+
try (PreparedStatement pstmt = conn.prepareStatement("INSERT INTO " + TABLE_NAME + " VALUES(?, ?, ?, ?)")) {//Create a prepared statement
32+
pstmt.setDate(1, Date.valueOf("2025-01-01"));//Set the first parameter to '2025-01-01' (using java.sql.Date)
3133
pstmt.setInt(2, 1);//Set the second parameter to 1
3234
pstmt.setString(3, "Alice");//Set the third parameter to "Alice"
35+
pstmt.setObject(4, Collections.singletonMap("key1", "value1"));
3336
pstmt.addBatch();//Add the current parameters to the batch
3437

35-
pstmt.setDate(1, new Date(System.currentTimeMillis()));//Set the first parameter to the current date (using java.sql.Date)
38+
pstmt.setDate(1, Date.valueOf("2025-02-01"));//Set the first parameter to '2025-02-01'
3639
pstmt.setInt(2, 2);//Set the second parameter to 2
3740
pstmt.setString(3, "Bob");//Set the third parameter to "Bob"
41+
pstmt.setObject(4, Collections.singletonMap("key2", "value2"));
3842
pstmt.addBatch();//Add the current parameters to the batch
3943

4044
pstmt.executeBatch();//Execute the batch
4145
}
4246

4347
try (ResultSet rs = stmt.executeQuery("SELECT * FROM " + TABLE_NAME)) {
4448
while (rs.next()) {
45-
System.out.println(rs.getDate(1) + ", " + rs.getInt(2) + ", " + rs.getString(3));
49+
System.out.println(rs.getDate(1) + ", " + rs.getInt(2) + ", " + rs.getString(3) + ", " + rs.getObject(4));
4650
}
4751
}
4852
}

0 commit comments

Comments
 (0)