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 afe00cf223..24cf3c98ef 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 @@ -8,27 +8,27 @@ import java.util.Properties; import com.taosdata.jdbc.TSDBDriver; public class JNIConnectExample { - public static void main(String[] args) throws SQLException { - // use - // String jdbcUrl = "jdbc:TAOS://localhost:6030/dbName?user=root&password=taosdata"; - // if you want to connect a specified database named "dbName". - String jdbcUrl = "jdbc:TAOS://localhost:6030?user=root&password=taosdata"; - Properties connProps = new Properties(); - connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); - connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); - connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); +// ANCHOR: main +public static void main(String[] args) throws SQLException { + // use + // String jdbcUrl = "jdbc:TAOS://localhost:6030/dbName?user=root&password=taosdata"; + // if you want to connect a specified database named "dbName". + String jdbcUrl = "jdbc:TAOS://localhost:6030?user=root&password=taosdata"; + Properties connProps = new Properties(); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); - try { - Connection conn = DriverManager.getConnection(jdbcUrl, connProps); - System.out.println("Connected"); + try (Connection conn = DriverManager.getConnection(jdbcUrl, connProps)) { + System.out.println("Connected"); - // you can use the connection for execute SQL here + // you can use the connection for execute SQL here - conn.close(); - } catch (SQLException ex) { - // handle any errors - System.out.println("SQLException: " + ex.getMessage()); - } + } catch (SQLException ex) { + // handle any errors + System.out.println("SQLException: " + 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 1c6973133b..1eb4c0755e 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 @@ -5,18 +5,18 @@ import java.sql.DriverManager; import java.sql.SQLException; public class RESTConnectExample { - // ANCHOR: main - 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"); +// ANCHOR: main +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"); - // you can use the connection for execute SQL here + // you can use the connection for execute SQL here - } catch (SQLException ex) { - // handle any errors - System.out.println("SQLException: " + ex.getMessage()); - } + } catch (SQLException ex) { + // handle any errors + System.out.println("SQLException: " + ex.getMessage()); } - // ANCHOR_END: main +} +// ANCHOR_END: main } \ No newline at end of file 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 58b1ed0f3d..b28f4c7ff8 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 @@ -8,23 +8,23 @@ import java.sql.SQLException; import java.util.Properties; public class WSConnectExample { - // ANCHOR: main - public static void main(String[] args) throws SQLException { - // use - // String jdbcUrl = "jdbc:TAOS-RS://localhost:6041/dbName?user=root&password=taosdata"; - // if you want to connect a specified database named "dbName". - String jdbcUrl = "jdbc:TAOS-RS://localhost:6041?user=root&password=taosdata"; - Properties connProps = new Properties(); - connProps.setProperty(TSDBDriver.PROPERTY_KEY_BATCH_LOAD, "true"); - try (Connection conn = DriverManager.getConnection(jdbcUrl, connProps)){ - System.out.println("Connected"); +// ANCHOR: main +public static void main(String[] args) throws SQLException { + // use + // String jdbcUrl = "jdbc:TAOS-RS://localhost:6041/dbName?user=root&password=taosdata"; + // if you want to connect a specified database named "dbName". + String jdbcUrl = "jdbc:TAOS-RS://localhost:6041?user=root&password=taosdata"; + Properties connProps = new Properties(); + connProps.setProperty(TSDBDriver.PROPERTY_KEY_BATCH_LOAD, "true"); + try (Connection conn = DriverManager.getConnection(jdbcUrl, connProps)){ + System.out.println("Connected"); - // you can use the connection for execute SQL here + // you can use the connection for execute SQL here - } catch (SQLException ex) { - // handle any errors - System.out.println("SQLException: " + ex.getMessage()); - } + } catch (SQLException ex) { + // handle any errors + System.out.println("SQLException: " + ex.getMessage()); } - // ANCHOR_END: main +} +// ANCHOR_END: main } diff --git a/docs/zh/08-develop/01-connect/index.md b/docs/zh/08-develop/01-connect/index.md index 160e132560..f7ee89ee11 100644 --- a/docs/zh/08-develop/01-connect/index.md +++ b/docs/zh/08-develop/01-connect/index.md @@ -388,7 +388,7 @@ properties 中的配置参数如下: ```java - {{#include docs/examples/java/src/main/java/com/taos/example/JNIConnectExample.java}} + {{#include docs/examples/java/src/main/java/com/taos/example/JNIConnectExample.java:main}} ``` 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 new file mode 100644 index 0000000000..0f12b6828c --- /dev/null +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcCreatDBDemo.java @@ -0,0 +1,57 @@ +package com.taosdata.example; + +import com.taosdata.jdbc.AbstractStatement; + +import java.sql.*; +import java.util.Properties; + +public class JdbcCreatDBDemo { + private static final String host = "localhost"; + private static final String dbName = "test"; + private static final String tbName = "weather"; + private static final String user = "root"; + private static final String password = "taosdata"; + + + public static void main(String[] args) throws SQLException { + +final String url = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password; + +// get connection +Properties properties = new Properties(); +properties.setProperty("charset", "UTF-8"); +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); + Statement stmt = connection.createStatement()) { + + // create database + int rowsAffected = stmt.executeUpdate("CREATE DATABASE IF NOT EXISTS power"); + // you can check rowsAffected here + assert rowsAffected == 0; + + // use database + rowsAffected = stmt.executeUpdate("USE power"); + // you can check rowsAffected here + assert rowsAffected == 0; + + // create table + rowsAffected = stmt.executeUpdate("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))"); + // you can check rowsAffected here + assert rowsAffected == 0; + +} catch (SQLException ex) { + // handle any errors + System.out.println("SQLException: " + ex.getMessage()); +} +// ANCHOR_END: create_db_and_table + + } + + private static void printResult(ResultSet resultSet) throws SQLException { + Util.printResult(resultSet); + } + +} 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 new file mode 100644 index 0000000000..e56a47279b --- /dev/null +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcInsertDataDemo.java @@ -0,0 +1,49 @@ +package com.taosdata.example; + +import com.taosdata.jdbc.AbstractStatement; + +import java.sql.*; +import java.util.Properties; + +public class JdbcInsertDataDemo { + private static final String host = "localhost"; + private static final String dbName = "test"; + private static final String tbName = "weather"; + private static final String user = "root"; + private static final String password = "taosdata"; + + + public static void main(String[] args) throws SQLException { + +final String url = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password; + +// get connection +Properties properties = new Properties(); +properties.setProperty("charset", "UTF-8"); +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); + Statement stmt = connection.createStatement()) { + + // insert data, please make sure the database and table are created before + String insertQuery = "INSERT INTO " + + "power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') " + + "VALUES " + + "(NOW + 1a, 10.30000, 219, 0.31000) " + + "(NOW + 2a, 12.60000, 218, 0.33000) " + + "(NOW + 3a, 12.30000, 221, 0.31000) " + + "power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') " + + "VALUES " + + "(NOW + 1a, 10.30000, 218, 0.25000) "; + int affectedRows = stmt.executeUpdate(insertQuery); + // you can check affectedRows here + System.out.println("insert " + affectedRows + " rows."); +} catch (SQLException ex) { + // handle any errors + System.out.println("SQLException: " + 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 new file mode 100644 index 0000000000..d0cc362bbe --- /dev/null +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcQueryDemo.java @@ -0,0 +1,55 @@ +package com.taosdata.example; + +import com.taosdata.jdbc.AbstractStatement; + +import java.sql.*; +import java.util.Properties; + +public class JdbcQueryDemo { + private static final String host = "localhost"; + private static final String dbName = "test"; + private static final String tbName = "weather"; + private static final String user = "root"; + private static final String password = "taosdata"; + + + public static void main(String[] args) throws SQLException { + +final String url = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password; + +// get connection +Properties properties = new Properties(); +properties.setProperty("charset", "UTF-8"); +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); + Statement stmt = connection.createStatement(); + // query data, make sure the database and table are created before + ResultSet resultSet = stmt.executeQuery("SELECT * FROM power.meters")) { + + Timestamp ts; + float current; + String location; + while (resultSet.next()) { + ts = resultSet.getTimestamp(1); + current = resultSet.getFloat(2); + // we recommend using the column name to get the value + location = resultSet.getString("location"); + + // you can check data here + System.out.printf("%s, %f, %s\n", ts, current, location); + } +} catch (SQLException ex) { + // handle any errors + System.out.println("SQLException: " + ex.getMessage()); +} +// ANCHOR_END: query_data + } + + private static void printResult(ResultSet resultSet) throws SQLException { + Util.printResult(resultSet); + } + +} 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 new file mode 100644 index 0000000000..1c44c2916e --- /dev/null +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcReqIdDemo.java @@ -0,0 +1,55 @@ +package com.taosdata.example; + +import com.taosdata.jdbc.AbstractStatement; + +import java.sql.*; +import java.util.Properties; + +public class JdbcReqIdDemo { + private static final String host = "localhost"; + private static final String dbName = "test"; + private static final String tbName = "weather"; + private static final String user = "root"; + private static final String password = "taosdata"; + + + public static void main(String[] args) throws SQLException { + +final String url = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password; + +// get connection +Properties properties = new Properties(); +properties.setProperty("charset", "UTF-8"); +properties.setProperty("locale", "en_US.UTF-8"); +properties.setProperty("timezone", "UTC-8"); +System.out.println("get connection starting..."); + +// ANCHOR: with_reqid +try (Connection connection = DriverManager.getConnection(url, 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)) { + while (rs.next()) { + Timestamp timestamp = rs.getTimestamp(1); + System.out.println("timestamp = " + timestamp); + } + } +} catch (SQLException ex) { + // handle any errors + System.out.println("SQLException: " + ex.getMessage()); +} +// ANCHOR_END: with_reqid + } + + private static void printResult(ResultSet resultSet) throws SQLException { + Util.printResult(resultSet); + } + +}