From 634bf6bc498089c0939e9ab29462aa2ab088a697 Mon Sep 17 00:00:00 2001 From: dingbo Date: Sat, 26 Mar 2022 17:28:50 +0800 Subject: [PATCH] add java query example --- docs-cn/06-insert-data/01-sql-writing.mdx | 2 +- docs-cn/06-insert-data/_java_stmt.mdx | 2 +- docs-cn/07-query-data/_java.mdx | 3 + docs-cn/07-query-data/index.mdx | 15 +++-- .../com/taos/example/RestQueryExample.java | 55 +++++++++++++++++++ .../com/taos/example/StmtInsertExample.java | 34 ++++++------ 6 files changed, 83 insertions(+), 28 deletions(-) create mode 100644 docs-examples/java/src/main/java/com/taos/example/RestQueryExample.java diff --git a/docs-cn/06-insert-data/01-sql-writing.mdx b/docs-cn/06-insert-data/01-sql-writing.mdx index 16616487bc..c49ddc025e 100644 --- a/docs-cn/06-insert-data/01-sql-writing.mdx +++ b/docs-cn/06-insert-data/01-sql-writing.mdx @@ -118,6 +118,6 @@ TDengine 也提供了支持参数绑定的 Prepare API,与 MySQL 一样,这 :::note -只有使用本地驱动方式连接连接,才能使用动态绑定工具。 +只有使用本地驱动方式连接连接,才能使用动态绑定功能。 ::: diff --git a/docs-cn/06-insert-data/_java_stmt.mdx b/docs-cn/06-insert-data/_java_stmt.mdx index 9f3e7a4466..54443e535f 100644 --- a/docs-cn/06-insert-data/_java_stmt.mdx +++ b/docs-cn/06-insert-data/_java_stmt.mdx @@ -1,3 +1,3 @@ ```java -{{#include docs-examples/java/src/main/java/com/taos/example/StmtInsertExample.java:insert}} +{{#include docs-examples/java/src/main/java/com/taos/example/StmtInsertExample.java}} ``` diff --git a/docs-cn/07-query-data/_java.mdx b/docs-cn/07-query-data/_java.mdx index e69de29bb2..519b926614 100644 --- a/docs-cn/07-query-data/_java.mdx +++ b/docs-cn/07-query-data/_java.mdx @@ -0,0 +1,3 @@ +```java +{{#include docs-examples/java/src/main/java/com/taos/example/RestQueryExample.java}} +``` diff --git a/docs-cn/07-query-data/index.mdx b/docs-cn/07-query-data/index.mdx index 1bea16873d..7f492a971d 100644 --- a/docs-cn/07-query-data/index.mdx +++ b/docs-cn/07-query-data/index.mdx @@ -123,25 +123,25 @@ Query OK, 5 row(s) in set (0.001521s) - + - + - + - + - + - + - + @@ -177,4 +177,3 @@ Query OK, 5 row(s) in set (0.001521s) 只有使用本地驱动方式连接连接,才能使用异步查询功能。 ::: - diff --git a/docs-examples/java/src/main/java/com/taos/example/RestQueryExample.java b/docs-examples/java/src/main/java/com/taos/example/RestQueryExample.java new file mode 100644 index 0000000000..959e003fe8 --- /dev/null +++ b/docs-examples/java/src/main/java/com/taos/example/RestQueryExample.java @@ -0,0 +1,55 @@ +package com.taos.example; + +import java.sql.*; + +public class RestQueryExample { + private static Connection getConnection() throws SQLException { + String jdbcUrl = "jdbc:TAOS-RS://localhost:6041/power?user=root&password=taosdata"; + return DriverManager.getConnection(jdbcUrl); + } + + private static void printRow(ResultSet rs) throws SQLException { + ResultSetMetaData meta = rs.getMetaData(); + for (int i = 1; i <= meta.getColumnCount(); i++) { + String value = rs.getString(i); + System.out.print(value); + System.out.print("\t"); + } + System.out.println(); + } + + private static void printColName(ResultSet rs) throws SQLException { + ResultSetMetaData meta = rs.getMetaData(); + for (int i = 1; i <= meta.getColumnCount(); i++) { + String colLabel = meta.getColumnLabel(i); + System.out.print(colLabel); + System.out.print("\t"); + } + System.out.println(); + } + + private static void processResult(ResultSet rs) throws SQLException { + printColName(rs); + while (rs.next()) { + printRow(rs); + } + } + + private static void queryData() throws SQLException { + try (Connection conn = getConnection()) { + try (Statement stmt = conn.createStatement()) { + ResultSet rs = stmt.executeQuery("SELECT AVG(voltage) FROM meters GROUP BY location"); + processResult(rs); + } + } + } + + public static void main(String[] args) throws SQLException { + queryData(); + } +} + +// possible stdout: +// avg(voltage) location +// 222.0 Beijing.Haidian +// 219.0 Beijing.Chaoyang diff --git a/docs-examples/java/src/main/java/com/taos/example/StmtInsertExample.java b/docs-examples/java/src/main/java/com/taos/example/StmtInsertExample.java index 2e89e9290a..2a7ccebf41 100644 --- a/docs-examples/java/src/main/java/com/taos/example/StmtInsertExample.java +++ b/docs-examples/java/src/main/java/com/taos/example/StmtInsertExample.java @@ -14,9 +14,18 @@ import java.util.Arrays; import java.util.List; public class StmtInsertExample { - private static Connection getConnection() throws SQLException { - String jdbcUrl = "jdbc:TAOS://localhost:6030?user=root&password=taosdata"; - return DriverManager.getConnection(jdbcUrl); + private static ArrayList tsToLongArray(String ts) { + ArrayList result = new ArrayList<>(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); + LocalDateTime localDateTime = LocalDateTime.parse(ts, formatter); + result.add(localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli()); + return result; + } + + private static ArrayList toArray(T v) { + ArrayList result = new ArrayList<>(); + result.add(v); + return result; } private static List getRawData() { @@ -32,21 +41,11 @@ public class StmtInsertExample { ); } - private static ArrayList tsToLongArray(String ts) { - ArrayList result = new ArrayList<>(); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); - LocalDateTime localDateTime = LocalDateTime.parse(ts, formatter); - result.add(localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli()); - return result; - } - - private static ArrayList toArray(T v) { - ArrayList result = new ArrayList<>(); - result.add(v); - return result; + private static Connection getConnection() throws SQLException { + String jdbcUrl = "jdbc:TAOS://localhost:6030?user=root&password=taosdata"; + return DriverManager.getConnection(jdbcUrl); } - // ANCHOR: insert private static void createTable(Connection conn) throws SQLException { try (Statement stmt = conn.createStatement()) { stmt.execute("CREATE DATABASE power KEEP 3650"); @@ -68,7 +67,7 @@ public class StmtInsertExample { pst.setTagString(0, ps[5]); pst.setTagInt(1, Integer.valueOf(ps[6])); // bind values - pst.setTimestamp(0, tsToLongArray(ps[1])); + pst.setTimestamp(0, tsToLongArray(ps[1])); //ps[1] looks like: 2018-10-03 14:38:05.000 pst.setFloat(1, toArray(Float.valueOf(ps[2]))); pst.setInt(2, toArray(Integer.valueOf(ps[3]))); pst.setFloat(3, toArray(Float.valueOf(ps[4]))); @@ -78,7 +77,6 @@ public class StmtInsertExample { } } } - // ANCHOR_END: insert public static void main(String[] args) throws SQLException { insertData(); -- GitLab