From a01439d95e0caafeb52125ac3fda7170d1ef833d Mon Sep 17 00:00:00 2001
From: sheyanjie-qq <249478495@qq.com>
Date: Thu, 1 Aug 2024 14:26:55 +0800
Subject: [PATCH] mod sample code
---
.../com/taos/example/JNIConnectExample.java | 6 +-
.../com/taos/example/RESTConnectExample.java | 6 +-
.../com/taos/example/WSConnectExample.java | 6 +-
docs/zh/08-develop/01-connect/index.md | 8 +-
docs/zh/08-develop/02-sql.md | 7 +-
docs/zh/08-develop/04-schemaless.md | 6 ++
docs/zh/08-develop/05-stmt.md | 9 +-
docs/zh/08-develop/07-tmq.md | 6 +-
docs/zh/14-reference/05-connector/14-java.mdx | 2 +-
.../com/taosdata/example/AbsConsumerLoop.java | 17 ++--
.../taosdata/example/AbsConsumerLoopFull.java | 4 +-
.../taosdata/example/AbsWsConsumerLoop.java | 5 +-
.../taosdata/example/ConsumerOffsetSeek.java | 6 +-
.../com/taosdata/example/JdbcCreatDBDemo.java | 7 +-
.../taosdata/example/JdbcInsertDataDemo.java | 10 +--
.../com/taosdata/example/JdbcQueryDemo.java | 11 ++-
.../com/taosdata/example/JdbcReqIdDemo.java | 16 ++--
.../example/ParameterBindingBasicDemo.java | 49 +++--------
.../example/ParameterBindingBatchDemo.java | 83 +++++++++++++++++++
.../taosdata/example/SchemalessJniTest.java | 11 ++-
.../taosdata/example/SchemalessWsTest.java | 7 +-
.../example/WSParameterBindingBasicDemo.java | 3 +-
22 files changed, 171 insertions(+), 114 deletions(-)
create mode 100644 examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ParameterBindingBatchDemo.java
diff --git a/docs/examples/java/src/main/java/com/taos/example/JNIConnectExample.java b/docs/examples/java/src/main/java/com/taos/example/JNIConnectExample.java
index 8905150a0a..8b9f27c5ab 100644
--- a/docs/examples/java/src/main/java/com/taos/example/JNIConnectExample.java
+++ b/docs/examples/java/src/main/java/com/taos/example/JNIConnectExample.java
@@ -20,15 +20,13 @@ public static void main(String[] args) throws SQLException {
connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
try (Connection conn = DriverManager.getConnection(jdbcUrl, connProps)) {
- System.out.println("Connected");
+ System.out.println("Connected to " + jdbcUrl + " successfully.");
// you can use the connection for execute SQL here
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
+ System.out.println("Failed to connect to " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
}
// ANCHOR_END: main
diff --git a/docs/examples/java/src/main/java/com/taos/example/RESTConnectExample.java b/docs/examples/java/src/main/java/com/taos/example/RESTConnectExample.java
index cd129db699..22bf1d61f4 100644
--- a/docs/examples/java/src/main/java/com/taos/example/RESTConnectExample.java
+++ b/docs/examples/java/src/main/java/com/taos/example/RESTConnectExample.java
@@ -9,15 +9,13 @@ public class RESTConnectExample {
public static void main(String[] args) throws SQLException {
String jdbcUrl = "jdbc:TAOS-RS://localhost:6041?user=root&password=taosdata";
try (Connection conn = DriverManager.getConnection(jdbcUrl)){
- System.out.println("Connected");
+ System.out.println("Connected to " + jdbcUrl + " successfully.");
// you can use the connection for execute SQL here
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
+ System.out.println("Failed to connect to " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
}
// ANCHOR_END: main
diff --git a/docs/examples/java/src/main/java/com/taos/example/WSConnectExample.java b/docs/examples/java/src/main/java/com/taos/example/WSConnectExample.java
index 3f417841b2..b355a28f6f 100644
--- a/docs/examples/java/src/main/java/com/taos/example/WSConnectExample.java
+++ b/docs/examples/java/src/main/java/com/taos/example/WSConnectExample.java
@@ -21,15 +21,13 @@ public static void main(String[] args) throws SQLException {
connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
try (Connection conn = DriverManager.getConnection(jdbcUrl, connProps)){
- System.out.println("Connected");
+ System.out.println("Connected to " + jdbcUrl + " successfully.");
// you can use the connection for execute SQL here
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("SQLState: " + ex.getSQLState());
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
+ System.out.println("Failed to connect to " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
}
// ANCHOR_END: main
diff --git a/docs/zh/08-develop/01-connect/index.md b/docs/zh/08-develop/01-connect/index.md
index 8b00bd4d49..5debb3ecea 100644
--- a/docs/zh/08-develop/01-connect/index.md
+++ b/docs/zh/08-develop/01-connect/index.md
@@ -322,7 +322,7 @@ URL 和 Properties 的详细参数说明和如何使用详见 [API 说明](../..
### Websocket 连接
-各语言连接器建立 Websocket 连接代码样例。
+下面是各语言连接器建立 Websocket 连接代码样例。演示了如何使用 Websocket 连接方式连接到 TDengine 数据库,并对连接设定一些参数。整个过程主要涉及到数据库连接的建立和异常处理。
@@ -362,7 +362,8 @@ URL 和 Properties 的详细参数说明和如何使用详见 [API 说明](../..
### 原生连接
-各语言连接器建立原生连接代码样例。
+下面是各语言连接器建立原生连接代码样例。演示了如何使用原生连接方式连接到 TDengine 数据库,并对连接设定一些参数。整个过程主要涉及到数据库连接的建立和异常处理。
+
```java
@@ -394,7 +395,8 @@ URL 和 Properties 的详细参数说明和如何使用详见 [API 说明](../..
### REST 连接
-各语言连接器建立 REST 连接代码样例。
+下面是各语言连接器建立 RESt 连接代码样例。演示了如何使用 REST 连接方式连接到 TDengine 数据库。整个过程主要涉及到数据库连接的建立和异常处理。
+
```java
diff --git a/docs/zh/08-develop/02-sql.md b/docs/zh/08-develop/02-sql.md
index abd59f1e77..7511dde2a2 100644
--- a/docs/zh/08-develop/02-sql.md
+++ b/docs/zh/08-develop/02-sql.md
@@ -12,7 +12,8 @@ TDengine 对 SQL 语言提供了全面的支持,允许用户以熟悉的 SQL
下面介绍使用各语言连接器通过执行 SQL 完成建库、建表、写入数据和查询数据。
## 建库和表
-以智能电表为例,展示如何使用连接器执行 SQL 来创建数据库和表。
+下面以智能电表为例,展示使用各语言连接器如何执行 SQL 命令创建一个名为 `power` 的数据库,然后使用 `power` 数据库为默认数据库。
+接着创建一个名为 `meters` 的超级表(STABLE),其表结构包含时间戳、电流、电压、相位等列,以及分组 ID 和位置作为标签。
@@ -44,7 +45,7 @@ TDengine 对 SQL 语言提供了全面的支持,允许用户以熟悉的 SQL
## 插入数据
-以智能电表为例,展示如何使用连接器执行 SQL 来插入数据。
+下面以智能电表为例,展示如何使用连接器执行 SQL 来插入数据到 `power` 数据库的 `meters` 超级表。样例使用 TDengine 自动建表 SQL 语法,写入 d1001 子表中 3 条数据,写入 d1002 子表中 1 条数据,然后打印出实际插入数据条数。
@@ -80,7 +81,7 @@ NOW 为系统内部函数,默认为客户端所在计算机当前时间。 NOW
## 查询数据
-以智能电表为例,展示如何使用各语言连接器执行 SQL 来查询数据,并将获取到的结果打印出来。
+下面以智能电表为例,展示如何使用各语言连接器执行 SQL 来查询数据,从 `power` 数据库 `meters` 超级表中查询最多 100 行数据,并将获取到的结果按行打印出来。
diff --git a/docs/zh/08-develop/04-schemaless.md b/docs/zh/08-develop/04-schemaless.md
index 816b3aa170..734e9558a2 100644
--- a/docs/zh/08-develop/04-schemaless.md
+++ b/docs/zh/08-develop/04-schemaless.md
@@ -153,6 +153,12 @@ st,t1=3,t2=4,t3=t3 c1=3i64,c6="passit" 1626006833640000000
## 无模式写入示例
下面以智能电表为例,介绍各语言连接器使用无模式写入接口写入数据的代码样例,包含了三种协议: InfluxDB 的行协议、OpenTSDB 的 TELNET 行协议和 OpenTSDB 的 JSON 格式协议。
+:::note
+- 因为无模式写入自动建表规则与之前执行 SQL 样例中不同,因此运行代码样例前请确保 `meters`、`metric_telnet` 和 `metric_json` 表不存在。
+- OpenTSDB 的 TELNET 行协议和 OpenTSDB 的 JSON 格式协议只支持一个数据列,因此我们采用了其他示例。
+
+:::
+
### Websocket 连接
diff --git a/docs/zh/08-develop/05-stmt.md b/docs/zh/08-develop/05-stmt.md
index 214984c86c..132a1b8850 100644
--- a/docs/zh/08-develop/05-stmt.md
+++ b/docs/zh/08-develop/05-stmt.md
@@ -13,7 +13,14 @@ import TabItem from "@theme/TabItem";
- 预编译:当使用参数绑定时,SQL 语句可以被预编译并缓存,后续使用不同的参数值执行时,可以直接使用预编译的版本,提高执行效率。
- 减少网络开销:参数绑定还可以减少发送到数据库的数据量,因为只需要发送参数值而不是完整的 SQL 语句,特别是在执行大量相似的插入或更新操作时,这种差异尤为明显。
-下面我们继续以智能电表为例,展示各语言连接器使用参数绑定高效写入的功能。
+下面我们继续以智能电表为例,展示各语言连接器使用参数绑定高效写入的功能:
+1. 准备一个参数化的 SQL 插入语句,用于向超级表 `meters` 中插入数据。这个语句允许动态地指定子表名、标签和列值。
+2. 循环生成多个子表及其对应的数据行。对于每个子表:
+ - 设置子表的名称和标签值(分组 ID 和位置)。
+ - 生成多行数据,每行包括一个时间戳、随机生成的电流、电压和相位值。
+ - 执行批量插入操作,将这些数据行插入到对应的子表中。
+3. 最后打印实际插入表中的行数。
+
## Websocket 连接
diff --git a/docs/zh/08-develop/07-tmq.md b/docs/zh/08-develop/07-tmq.md
index f548126e49..16a538757f 100644
--- a/docs/zh/08-develop/07-tmq.md
+++ b/docs/zh/08-develop/07-tmq.md
@@ -78,7 +78,8 @@ Java 连接器创建消费者的参数为 Properties, 可以设置的参数列
### Websocket 连接
-介绍各语言连接器使用 Websocket 连接方式创建消费者。
+介绍各语言连接器使用 Websocket 连接方式创建消费者。指定连接的服务器地址,设置自动提交,从最新消息开始消费,指定 `group.id` 和 `client.id` 等信息。有的语言的连接器还支持反序列化参数。
+
@@ -126,7 +127,8 @@ Java 连接器创建消费者的参数为 Properties, 可以设置的参数列
### 原生连接
-介绍各语言连接器使用原生连接方式创建消费者。
+介绍各语言连接器使用原生连接方式创建消费者。指定连接的服务器地址,设置自动提交,从最新消息开始消费,指定 `group.id` 和 `client.id` 等信息。有的语言的连接器还支持反序列化参数。
+
diff --git a/docs/zh/14-reference/05-connector/14-java.mdx b/docs/zh/14-reference/05-connector/14-java.mdx
index acb1dee674..33f63916d2 100644
--- a/docs/zh/14-reference/05-connector/14-java.mdx
+++ b/docs/zh/14-reference/05-connector/14-java.mdx
@@ -1365,7 +1365,7 @@ JDBC 标准不支持数据订阅,因此本章所有接口都是扩展接口。
| reconnectIntervalMs | 自动重连重试间隔,单位毫秒。仅在 enableCompression 为 true 时生效。 | 2000 ms |
| reconnectRetryCount | 自动重连重试次数。仅在 enableCompression 为 true 时生效。 | 3 |
-其他参数请参考:[Consumer 参数列表](../../develop/tmq/#数据订阅相关参数), 注意TDengine服务端自 3.2.0.0 版本开始消息订阅中的 auto.offset.reset 默认值发生变化。
+其他参数请参考:[Consumer 参数列表](../../../develop/tmq/#创建参数), 注意TDengine服务端自 3.2.0.0 版本开始消息订阅中的 auto.offset.reset 默认值发生变化。
- `public void subscribe(Collection topics) throws SQLException`
- **接口说明**:订阅一组主题。
diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/AbsConsumerLoop.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/AbsConsumerLoop.java
index 62e3939573..90e6a950cd 100644
--- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/AbsConsumerLoop.java
+++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/AbsConsumerLoop.java
@@ -39,8 +39,7 @@ try {
this.consumer = new TaosConsumer<>(config);
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
+ System.out.println("Failed to create jni consumer with " + config.getProperty("bootstrap.servers") + " ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to create consumer", ex);
}
// ANCHOR_END: create_consumer
@@ -66,9 +65,8 @@ try {
}
} catch (SQLException ex){
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
-
+ System.out.println("Failed to poll data; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
+ throw new SQLException("Failed to poll data", ex);
} finally {
consumer.close();
shutdownLatch.countDown();
@@ -94,9 +92,8 @@ try {
}
} catch (SQLException ex){
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
-
+ System.out.println("Failed to execute consumer functions. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
+ throw new SQLException("Failed to execute consumer functions", ex);
} finally {
consumer.close();
shutdownLatch.countDown();
@@ -110,8 +107,8 @@ try {
consumer.unsubscribe();
} catch (SQLException ex){
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
+ System.out.println("Failed to unsubscribe consumer. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
+ throw new SQLException("Failed to unsubscribe consumer", ex);
} finally {
consumer.close();
}
diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/AbsConsumerLoopFull.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/AbsConsumerLoopFull.java
index af3537ef0e..bd8f1799f6 100644
--- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/AbsConsumerLoopFull.java
+++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/AbsConsumerLoopFull.java
@@ -38,8 +38,8 @@ public abstract class AbsConsumerLoopFull {
try {
this.consumer = new TaosConsumer<>(config);
} catch (SQLException ex) {
- // handle exception
- System.out.println("SQLException: " + ex.getMessage());
+ // handle any errors, please refer to the JDBC specifications for detailed exceptions info
+ System.out.println("Failed to create jni consumer, host : " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to create consumer", ex);
}
diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/AbsWsConsumerLoop.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/AbsWsConsumerLoop.java
index 98ff5657d8..690c05841c 100644
--- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/AbsWsConsumerLoop.java
+++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/AbsWsConsumerLoop.java
@@ -39,13 +39,12 @@ try {
this.consumer = new TaosConsumer<>(config);
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
+ System.out.println("Failed to create ws consumer with " + config.getProperty("bootstrap.servers") + " ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to create consumer", ex);
}
// ANCHOR_END: create_consumer
- this.topics = Collections.singletonList("topic_speed");
+ this.topics = Collections.singletonList("topic_meters");
this.shutdown = new AtomicBoolean(false);
this.shutdownLatch = new CountDownLatch(1);
}
diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerOffsetSeek.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerOffsetSeek.java
index 3b1ff88b28..b9463d30f7 100644
--- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerOffsetSeek.java
+++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerOffsetSeek.java
@@ -54,9 +54,9 @@ try (TaosConsumer consumer = new TaosConsumer<>(conf
// you can handle data here
}
} catch (SQLException ex) {
- // handle exception
- System.out.println("SQLException: " + ex.getMessage());
- throw new SQLException("Failed to create consumer", ex);
+ // handle any errors, please refer to the JDBC specifications for detailed exceptions info
+ System.out.println("Failed to execute consumer functions. server: " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
+ throw new SQLException("Failed to execute consumer functions", ex);
}
// ANCHOR_END: consumer_seek
}
diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcCreatDBDemo.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcCreatDBDemo.java
index b2246e96cf..d02b3c8789 100644
--- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcCreatDBDemo.java
+++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcCreatDBDemo.java
@@ -15,7 +15,7 @@ public class JdbcCreatDBDemo {
public static void main(String[] args) throws SQLException {
-final String url = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
+final String jdbcUrl = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
// get connection
Properties properties = new Properties();
@@ -24,7 +24,7 @@ properties.setProperty("locale", "en_US.UTF-8");
properties.setProperty("timezone", "UTC-8");
System.out.println("get connection starting...");
// ANCHOR: create_db_and_table
-try (Connection connection = DriverManager.getConnection(url, properties);
+try (Connection connection = DriverManager.getConnection(jdbcUrl, properties);
Statement stmt = connection.createStatement()) {
// create database
@@ -44,8 +44,7 @@ try (Connection connection = DriverManager.getConnection(url, properties);
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
+ System.out.println("Failed to create db and table, url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
// ANCHOR_END: create_db_and_table
diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcInsertDataDemo.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcInsertDataDemo.java
index 76e16c9cee..7fc6d5137d 100644
--- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcInsertDataDemo.java
+++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcInsertDataDemo.java
@@ -15,7 +15,7 @@ public class JdbcInsertDataDemo {
public static void main(String[] args) throws SQLException {
-final String url = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
+final String jdbcUrl = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
// get connection
Properties properties = new Properties();
@@ -24,7 +24,7 @@ properties.setProperty("locale", "en_US.UTF-8");
properties.setProperty("timezone", "UTC-8");
System.out.println("get connection starting...");
// ANCHOR: insert_data
-try (Connection connection = DriverManager.getConnection(url, properties);
+try (Connection connection = DriverManager.getConnection(jdbcUrl, properties);
Statement stmt = connection.createStatement()) {
// insert data, please make sure the database and table are created before
@@ -39,11 +39,11 @@ try (Connection connection = DriverManager.getConnection(url, properties);
"(NOW + 1a, 10.30000, 218, 0.25000) ";
int affectedRows = stmt.executeUpdate(insertQuery);
// you can check affectedRows here
- System.out.println("insert " + affectedRows + " rows.");
+ System.out.println("inserted into " + affectedRows + " rows to power.meters successfully.");
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
+ System.out.println("Failed to insert data to power.meters, url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
+
}
// ANCHOR_END: insert_data
}
diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcQueryDemo.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcQueryDemo.java
index fdbab204c2..30a835bb11 100644
--- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcQueryDemo.java
+++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcQueryDemo.java
@@ -15,7 +15,7 @@ public class JdbcQueryDemo {
public static void main(String[] args) throws SQLException {
-final String url = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
+final String jdbcUrl = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
// get connection
Properties properties = new Properties();
@@ -24,10 +24,10 @@ properties.setProperty("locale", "en_US.UTF-8");
properties.setProperty("timezone", "UTC-8");
System.out.println("get connection starting...");
// ANCHOR: query_data
-try (Connection connection = DriverManager.getConnection(url, properties);
+try (Connection connection = DriverManager.getConnection(jdbcUrl, properties);
Statement stmt = connection.createStatement();
// query data, make sure the database and table are created before
- ResultSet resultSet = stmt.executeQuery("SELECT * FROM power.meters")) {
+ ResultSet resultSet = stmt.executeQuery("SELECT ts, current, location FROM power.meters limit 100")) {
Timestamp ts;
float current;
@@ -39,12 +39,11 @@ try (Connection connection = DriverManager.getConnection(url, properties);
location = resultSet.getString("location");
// you can check data here
- System.out.printf("%s, %f, %s\n", ts, current, location);
+ System.out.printf("ts: %s, current: %f, location: %s %n", ts, current, location);
}
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
+ System.out.println("Failed to query data from power.meters, url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
// ANCHOR_END: query_data
}
diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcReqIdDemo.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcReqIdDemo.java
index 5fb7e9760e..cc557c18c5 100644
--- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcReqIdDemo.java
+++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcReqIdDemo.java
@@ -15,7 +15,7 @@ public class JdbcReqIdDemo {
public static void main(String[] args) throws SQLException {
-final String url = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
+final String jdbcUrl = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
// get connection
Properties properties = new Properties();
@@ -25,17 +25,11 @@ properties.setProperty("timezone", "UTC-8");
System.out.println("get connection starting...");
// ANCHOR: with_reqid
-try (Connection connection = DriverManager.getConnection(url, properties);
+try (Connection connection = DriverManager.getConnection(jdbcUrl, properties);
// Create a statement that allows specifying a request ID
AbstractStatement aStmt = (AbstractStatement) connection.createStatement()) {
- boolean hasResultSet = aStmt.execute("CREATE DATABASE IF NOT EXISTS power", 1L);
- assert !hasResultSet;
-
- int rowsAffected = aStmt.executeUpdate("USE power", 2L);
- assert rowsAffected == 0;
-
- try (ResultSet rs = aStmt.executeQuery("SELECT * FROM meters limit 1", 3L)) {
+ try (ResultSet rs = aStmt.executeQuery("SELECT ts, current, location FROM power.meters limit 1", 3L)) {
while (rs.next()) {
Timestamp timestamp = rs.getTimestamp(1);
System.out.println("timestamp = " + timestamp);
@@ -43,8 +37,8 @@ try (Connection connection = DriverManager.getConnection(url, properties);
}
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
+ System.out.println("Failed to execute sql with reqId, url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
+
}
// ANCHOR_END: with_reqid
}
diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ParameterBindingBasicDemo.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ParameterBindingBasicDemo.java
index d5f547b50f..469316efc7 100644
--- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ParameterBindingBasicDemo.java
+++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ParameterBindingBasicDemo.java
@@ -1,14 +1,9 @@
package com.taosdata.example;
import com.taosdata.jdbc.TSDBPreparedStatement;
-import com.taosdata.jdbc.utils.StringUtils;
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.SQLException;
-import java.sql.Statement;
+import java.sql.*;
import java.util.ArrayList;
-import java.util.List;
import java.util.Random;
// ANCHOR: para_bind
@@ -29,7 +24,6 @@ public class ParameterBindingBasicDemo {
String sql = "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)";
try (TSDBPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSDBPreparedStatement.class)) {
-
for (int i = 1; i <= numOfSubTable; i++) {
// set table name
pstmt.setTableName("d_bind_" + i);
@@ -38,40 +32,23 @@ public class ParameterBindingBasicDemo {
pstmt.setTagInt(0, i);
pstmt.setTagString(1, "location_" + i);
- // set column ts
- ArrayList tsList = new ArrayList<>();
+ // set columns
long current = System.currentTimeMillis();
- for (int j = 0; j < numOfRow; j++)
- tsList.add(current + j);
- pstmt.setTimestamp(0, tsList);
-
- // set column current
- ArrayList f1List = new ArrayList<>();
- for (int j = 0; j < numOfRow; j++)
- f1List.add(random.nextFloat() * 30);
- pstmt.setFloat(1, f1List);
-
- // set column voltage
- ArrayList f2List = new ArrayList<>();
- for (int j = 0; j < numOfRow; j++)
- f2List.add(random.nextInt(300));
- pstmt.setInt(2, f2List);
-
- // set column phase
- ArrayList f3List = new ArrayList<>();
- for (int j = 0; j < numOfRow; j++)
- f3List.add(random.nextFloat());
- pstmt.setFloat(3, f3List);
- // add column
- pstmt.columnDataAddBatch();
+ for (int j = 0; j < numOfRow; j++) {
+ pstmt.setTimestamp(1, new Timestamp(current + j));
+ pstmt.setFloat(2, random.nextFloat() * 30);
+ pstmt.setInt(3, random.nextInt(300));
+ pstmt.setFloat(4, random.nextFloat());
+ pstmt.addBatch();
+ }
+ int [] exeResult = pstmt.executeBatch();
+ // you can check exeResult here
+ System.out.println("insert " + exeResult.length + " rows.");
}
- // execute column
- pstmt.columnDataExecuteBatch();
}
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
+ System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
}
diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ParameterBindingBatchDemo.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ParameterBindingBatchDemo.java
new file mode 100644
index 0000000000..60d76eee4f
--- /dev/null
+++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ParameterBindingBatchDemo.java
@@ -0,0 +1,83 @@
+package com.taosdata.example;
+
+import com.taosdata.jdbc.TSDBPreparedStatement;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Random;
+
+// ANCHOR: para_bind
+public class ParameterBindingBatchDemo {
+
+ // modify host to your own
+ private static final String host = "127.0.0.1";
+ private static final Random random = new Random(System.currentTimeMillis());
+ private static final int numOfSubTable = 10, numOfRow = 10;
+
+ public static void main(String[] args) throws SQLException {
+
+ String jdbcUrl = "jdbc:TAOS://" + host + ":6030/";
+ try (Connection conn = DriverManager.getConnection(jdbcUrl, "root", "taosdata")) {
+
+ init(conn);
+
+ String sql = "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)";
+
+ try (TSDBPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSDBPreparedStatement.class)) {
+
+ for (int i = 1; i <= numOfSubTable; i++) {
+ // set table name
+ pstmt.setTableName("d_bind_" + i);
+
+ // set tags
+ pstmt.setTagInt(0, i);
+ pstmt.setTagString(1, "location_" + i);
+
+ // set column ts
+ ArrayList tsList = new ArrayList<>();
+ long current = System.currentTimeMillis();
+ for (int j = 0; j < numOfRow; j++)
+ tsList.add(current + j);
+ pstmt.setTimestamp(0, tsList);
+
+ // set column current
+ ArrayList f1List = new ArrayList<>();
+ for (int j = 0; j < numOfRow; j++)
+ f1List.add(random.nextFloat() * 30);
+ pstmt.setFloat(1, f1List);
+
+ // set column voltage
+ ArrayList f2List = new ArrayList<>();
+ for (int j = 0; j < numOfRow; j++)
+ f2List.add(random.nextInt(300));
+ pstmt.setInt(2, f2List);
+
+ // set column phase
+ ArrayList f3List = new ArrayList<>();
+ for (int j = 0; j < numOfRow; j++)
+ f3List.add(random.nextFloat());
+ pstmt.setFloat(3, f3List);
+ // add column
+ pstmt.columnDataAddBatch();
+ }
+ // execute column
+ pstmt.columnDataExecuteBatch();
+ }
+ } catch (SQLException ex) {
+ // handle any errors, please refer to the JDBC specifications for detailed exceptions info
+ System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
+ }
+ }
+
+ private static void init(Connection conn) throws SQLException {
+ try (Statement stmt = conn.createStatement()) {
+ stmt.execute("CREATE DATABASE IF NOT EXISTS power");
+ stmt.execute("USE power");
+ stmt.execute("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))");
+ }
+ }
+}
+// ANCHOR_END: para_bind
diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/SchemalessJniTest.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/SchemalessJniTest.java
index 238ad35502..8f5fe0e7cf 100644
--- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/SchemalessJniTest.java
+++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/SchemalessJniTest.java
@@ -13,12 +13,12 @@ import java.sql.Statement;
public class SchemalessJniTest {
private static final String host = "127.0.0.1";
private static final String lineDemo = "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639";
- private static final String telnetDemo = "stb0_0 1707095283260 4 host=host0 interface=eth0";
- private static final String jsonDemo = "{\"metric\": \"meter_current\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, \"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}";
+ private static final String telnetDemo = "metric_telnet 1707095283260 4 host=host0 interface=eth0";
+ private static final String jsonDemo = "{\"metric\": \"metric_json\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, \"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}";
public static void main(String[] args) throws SQLException {
- final String url = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata";
- try (Connection connection = DriverManager.getConnection(url)) {
+ final String jdbcUrl = "jdbc:TAOS://" + host + ":6030/?user=root&password=taosdata";
+ try (Connection connection = DriverManager.getConnection(jdbcUrl)) {
init(connection);
AbstractConnection conn = connection.unwrap(AbstractConnection.class);
@@ -27,8 +27,7 @@ public class SchemalessJniTest {
conn.write(jsonDemo, SchemalessProtocolType.JSON, SchemalessTimestampType.NOT_CONFIGURED);
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
+ System.out.println("Failed to insert data with schemaless, host:" + host + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
}
diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/SchemalessWsTest.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/SchemalessWsTest.java
index ed5444a4ec..922781af56 100644
--- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/SchemalessWsTest.java
+++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/SchemalessWsTest.java
@@ -13,8 +13,8 @@ import java.sql.Statement;
public class SchemalessWsTest {
private static final String host = "127.0.0.1";
private static final String lineDemo = "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639";
- private static final String telnetDemo = "stb0_0 1707095283260 4 host=host0 interface=eth0";
- private static final String jsonDemo = "{\"metric\": \"meter_current\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, \"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}";
+ private static final String telnetDemo = "metric_telnet 1707095283260 4 host=host0 interface=eth0";
+ private static final String jsonDemo = "{\"metric\": \"metric_json\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, \"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}";
public static void main(String[] args) throws SQLException {
final String url = "jdbc:TAOS-RS://" + host + ":6041?user=root&password=taosdata&batchfetch=true";
@@ -27,8 +27,7 @@ public class SchemalessWsTest {
conn.write(jsonDemo, SchemalessProtocolType.JSON, SchemalessTimestampType.SECONDS);
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
+ System.out.println("Failed to insert data with schemaless, host:" + host + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
}
diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WSParameterBindingBasicDemo.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WSParameterBindingBasicDemo.java
index 9501d4adab..9eca3c973a 100644
--- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WSParameterBindingBasicDemo.java
+++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WSParameterBindingBasicDemo.java
@@ -49,8 +49,7 @@ public class WSParameterBindingBasicDemo {
}
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
- System.out.println("Error Code: " + ex.getErrorCode());
- System.out.println("Message: " + ex.getMessage());
+ System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
}
}