This is an automated email from the ASF dual-hosted git repository.

jackietien pushed a commit to branch ty/useXX
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit b495005747cbc1fe1cbcb846a7907a37e09955bd
Author: JackieTien97 <[email protected]>
AuthorDate: Mon Jul 8 21:21:32 2024 +0800

    Fix IoTDBConnection bug and add TableModelSessionExample
---
 .../org/apache/iotdb/TableModelJDBCExample.java    |  17 ++-
 .../org/apache/iotdb/TableModelSessionExample.java | 136 +++++++++++++++++++++
 .../apache/iotdb/jdbc/IoTDBConnectionParams.java   |   2 +-
 .../src/main/java/org/apache/iotdb/jdbc/Utils.java |  10 +-
 4 files changed, 160 insertions(+), 5 deletions(-)

diff --git 
a/example/jdbc/src/main/java/org/apache/iotdb/TableModelJDBCExample.java 
b/example/jdbc/src/main/java/org/apache/iotdb/TableModelJDBCExample.java
index 2fb2ae428e3..ff326d97340 100644
--- a/example/jdbc/src/main/java/org/apache/iotdb/TableModelJDBCExample.java
+++ b/example/jdbc/src/main/java/org/apache/iotdb/TableModelJDBCExample.java
@@ -37,6 +37,8 @@ public class TableModelJDBCExample {
 
   public static void main(String[] args) throws ClassNotFoundException, 
SQLException {
     Class.forName("org.apache.iotdb.jdbc.IoTDBDriver");
+
+    // don't specify database in url
     try (Connection connection =
             DriverManager.getConnection(
                 "jdbc:iotdb://127.0.0.1:6667?sql_dialect=table", "root", 
"root");
@@ -64,7 +66,7 @@ public class TableModelJDBCExample {
       }
 
       // show tables by specifying another database
-      // using SHOW tables in
+      // using SHOW tables FROM
       try (ResultSet resultSet = statement.executeQuery("SHOW TABLES FROM 
test1")) {
         ResultSetMetaData metaData = resultSet.getMetaData();
         System.out.println(metaData.getColumnCount());
@@ -82,7 +84,18 @@ public class TableModelJDBCExample {
             DriverManager.getConnection(
                 "jdbc:iotdb://127.0.0.1:6667/test1?sql_dialect=table", "root", 
"root");
         Statement statement = connection.createStatement()) {
-      // show tables from current database
+      // show tables from current database test1
+      try (ResultSet resultSet = statement.executeQuery("SHOW TABLES")) {
+        ResultSetMetaData metaData = resultSet.getMetaData();
+        System.out.println(metaData.getColumnCount());
+        while (resultSet.next()) {
+          System.out.println(resultSet.getString(1) + ", " + 
resultSet.getInt(2));
+        }
+      }
+
+      // change database to test2
+      statement.execute("use test2");
+
       try (ResultSet resultSet = statement.executeQuery("SHOW TABLES")) {
         ResultSetMetaData metaData = resultSet.getMetaData();
         System.out.println(metaData.getColumnCount());
diff --git 
a/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java 
b/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java
new file mode 100644
index 00000000000..b57b5903e07
--- /dev/null
+++ 
b/example/session/src/main/java/org/apache/iotdb/TableModelSessionExample.java
@@ -0,0 +1,136 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb;
+
+import org.apache.iotdb.isession.SessionDataSet;
+import org.apache.iotdb.isession.util.Version;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.Session;
+
+public class TableModelSessionExample {
+
+  private static final String LOCAL_HOST = "127.0.0.1";
+
+  public static void main(String[] args) {
+
+    // don't specify database in constructor
+    Session session =
+        new Session.Builder()
+            .host(LOCAL_HOST)
+            .port(6667)
+            .username("root")
+            .password("root")
+            .version(Version.V_1_0)
+            .sqlDialect("table")
+            .build();
+
+    try {
+      session.open(false);
+
+      session.executeNonQueryStatement("CREATE DATABASE test1");
+      session.executeNonQueryStatement("CREATE DATABASE test2");
+
+      session.executeNonQueryStatement("use test2");
+
+      // or use full qualified table name
+      session.executeNonQueryStatement(
+          "create table test1.table1(region_id STRING ID, plant_id STRING ID, 
device_id STRING ID, model STRING ATTRIBUTE, temperature FLOAT MEASUREMENT, 
humidity DOUBLE MEASUREMENT) with (TTL=3600000)");
+
+      session.executeNonQueryStatement(
+          "create table table2(region_id STRING ID, plant_id STRING ID, color 
STRING ATTRIBUTE, temperature FLOAT MEASUREMENT, speed DOUBLE MEASUREMENT) with 
(TTL=6600000)");
+
+      // show tables from current database
+      try (SessionDataSet dataSet = session.executeQueryStatement("SHOW 
TABLES")) {
+        System.out.println(dataSet.getColumnNames());
+        while (dataSet.hasNext()) {
+          System.out.println(dataSet.next());
+        }
+      }
+
+      // show tables by specifying another database
+      // using SHOW tables FROM
+      try (SessionDataSet dataSet = session.executeQueryStatement("SHOW TABLES 
FROM test1")) {
+        System.out.println(dataSet.getColumnNames());
+        while (dataSet.hasNext()) {
+          System.out.println(dataSet.next());
+        }
+      }
+
+    } catch (IoTDBConnectionException e) {
+      e.printStackTrace();
+    } catch (StatementExecutionException e) {
+      e.printStackTrace();
+    } finally {
+      try {
+        session.close();
+      } catch (IoTDBConnectionException e) {
+        e.printStackTrace();
+      }
+    }
+
+    // specify database in constructor
+    session =
+        new Session.Builder()
+            .host(LOCAL_HOST)
+            .port(6667)
+            .username("root")
+            .password("root")
+            .version(Version.V_1_0)
+            .sqlDialect("table")
+            .database("test1")
+            .build();
+
+    try {
+      session.open(false);
+
+      // show tables from current database
+      try (SessionDataSet dataSet = session.executeQueryStatement("SHOW 
TABLES")) {
+        System.out.println(dataSet.getColumnNames());
+        while (dataSet.hasNext()) {
+          System.out.println(dataSet.next());
+        }
+      }
+
+      // change database to test2
+      session.executeNonQueryStatement("use test2");
+
+      // show tables by specifying another database
+      // using SHOW tables FROM
+      try (SessionDataSet dataSet = session.executeQueryStatement("SHOW 
TABLES")) {
+        System.out.println(dataSet.getColumnNames());
+        while (dataSet.hasNext()) {
+          System.out.println(dataSet.next());
+        }
+      }
+
+    } catch (IoTDBConnectionException e) {
+      e.printStackTrace();
+    } catch (StatementExecutionException e) {
+      e.printStackTrace();
+    } finally {
+      try {
+        session.close();
+      } catch (IoTDBConnectionException e) {
+        e.printStackTrace();
+      }
+    }
+  }
+}
diff --git 
a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnectionParams.java
 
b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnectionParams.java
index 71d4178c61a..a31d2198d27 100644
--- 
a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnectionParams.java
+++ 
b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnectionParams.java
@@ -191,7 +191,7 @@ public class IoTDBConnectionParams {
   }
 
   public Optional<String> getDb() {
-    return Optional.of(db);
+    return Optional.ofNullable(db);
   }
 
   public void setDb(String db) {
diff --git a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Utils.java 
b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Utils.java
index 7f90df75145..009ae03decc 100644
--- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Utils.java
+++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Utils.java
@@ -62,6 +62,7 @@ public class Utils {
       params.setHost(host);
       suffixURL = subURL.substring(i + 1);
 
+      i++;
       // parse port
       int port = 0;
       for (; i < subURL.length() && Character.isDigit(subURL.charAt(i)); i++) {
@@ -75,7 +76,11 @@ public class Utils {
           int endIndex = subURL.indexOf(PARAMETER_SEPARATOR, i + 1);
           String database;
           if (endIndex <= i + 1) {
-            database = subURL.substring(i + 1);
+            if (i + 1 == subURL.length()) {
+              database = null;
+            } else {
+              database = subURL.substring(i + 1);
+            }
             suffixURL = "";
           } else {
             database = subURL.substring(i + 1, endIndex);
@@ -92,7 +97,8 @@ public class Utils {
     }
     if (!isUrlLegal) {
       throw new IoTDBURLException(
-          "Error url format, url should be 
jdbc:iotdb://anything:port/[database] or 
jdbc:iotdb://anything:port[/database]?property1=value1&property2=value2");
+          "Error url format, url should be 
jdbc:iotdb://anything:port/[database] or 
jdbc:iotdb://anything:port[/database]?property1=value1&property2=value2, 
current url is "
+              + url);
     }
 
     if (info.containsKey(Config.AUTH_USER)) {

Reply via email to