提交 634bf6bc 编写于 作者: D dingbo

add java query example

上级 60b68dcf
......@@ -118,6 +118,6 @@ TDengine 也提供了支持参数绑定的 Prepare API,与 MySQL 一样,这
:::note
只有使用本地驱动方式连接连接,才能使用动态绑定工具
只有使用本地驱动方式连接连接,才能使用动态绑定功能
:::
```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}}
```
```java
{{#include docs-examples/java/src/main/java/com/taos/example/RestQueryExample.java}}
```
......@@ -123,25 +123,25 @@ Query OK, 5 row(s) in set (0.001521s)
<Tabs defaultValue="java" groupId="lang">
<TabItem label="Java" value="java">
<JavaStmt />
<JavaQuery />
</TabItem>
<TabItem label="Python" value="python">
<PyStmt />
<PyQuery />
</TabItem>
<TabItem label="Go" value="go">
<GoStmt />
<GoQuery />
</TabItem>
<TabItem label="Rust" value="rust">
<RustStmt />
<RustQuery />
</TabItem>
<TabItem label="Node.js" value="nodejs">
<NodeStmt />
<NodeQuery />
</TabItem>
<TabItem label="C#" value="csharp">
<CsStmt />
<CsQuery />
</TabItem>
<TabItem label="C" value="c">
<CStmt />
<CQuery />
</TabItem>
</Tabs>
......@@ -177,4 +177,3 @@ Query OK, 5 row(s) in set (0.001521s)
只有使用本地驱动方式连接连接,才能使用异步查询功能。
:::
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
......@@ -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<Long> tsToLongArray(String ts) {
ArrayList<Long> 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 <T> ArrayList<T> toArray(T v) {
ArrayList<T> result = new ArrayList<>();
result.add(v);
return result;
}
private static List<String> getRawData() {
......@@ -32,21 +41,11 @@ public class StmtInsertExample {
);
}
private static ArrayList<Long> tsToLongArray(String ts) {
ArrayList<Long> 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 <T> ArrayList<T> toArray(T v) {
ArrayList<T> 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();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册