You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ro...@apache.org on 2024/05/31 10:14:44 UTC
(iotdb) 02/02: Add example
This is an automated email from the ASF dual-hosted git repository.
rong pushed a commit to branch jdbc-charset
in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit d09621bf4ede5423da853835e328aa9fac4d4ecf
Author: Steve Yurong Su <ro...@apache.org>
AuthorDate: Fri May 31 18:14:29 2024 +0800
Add example
---
.../java/org/apache/iotdb/JDBCCharsetExample.java | 86 ++++++++++++++++++++++
.../java/org/apache/iotdb/jdbc/IoTDBStatement.java | 9 ++-
.../apache/iotdb/jdbc/IoTDBJDBCResultSetTest.java | 36 ++++-----
3 files changed, 113 insertions(+), 18 deletions(-)
diff --git a/example/jdbc/src/main/java/org/apache/iotdb/JDBCCharsetExample.java b/example/jdbc/src/main/java/org/apache/iotdb/JDBCCharsetExample.java
new file mode 100644
index 00000000000..143983d38c5
--- /dev/null
+++ b/example/jdbc/src/main/java/org/apache/iotdb/JDBCCharsetExample.java
@@ -0,0 +1,86 @@
+/*
+ * 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.jdbc.IoTDBSQLException;
+import org.apache.iotdb.jdbc.IoTDBStatement;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.UnsupportedEncodingException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+
+public class JDBCCharsetExample {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(JDBCCharsetExample.class);
+
+ public static void main(String[] args) throws Exception {
+ Class.forName("org.apache.iotdb.jdbc.IoTDBDriver");
+
+ try (final Connection connection =
+ DriverManager.getConnection(
+ "jdbc:iotdb://127.0.0.1:6667?charset=GB18030", "root", "root");
+ final IoTDBStatement statement = (IoTDBStatement) connection.createStatement()) {
+
+ final String insertSQLWithGB18030 =
+ "insert into root.测试(timestamp, 维语, 彝语, 繁体, 蒙文, 简体, 标点符号, 藏语) values(1, 'ئۇيغۇر تىلى', 'ꆈꌠꉙ', \"繁體\", 'ᠮᠣᠩᠭᠣᠯ ᠬᠡᠯᠡ', '简体', '——?!', \"བོད་སྐད།\");";
+ final byte[] insertSQLWithGB18030Bytes = insertSQLWithGB18030.getBytes("GB18030");
+ statement.execute(insertSQLWithGB18030Bytes);
+
+ ResultSet resultSet = statement.executeQuery("select ** from root");
+ outputResult(resultSet);
+ } catch (IoTDBSQLException e) {
+ LOGGER.error("IoTDB Jdbc example error", e);
+ }
+ }
+
+ @SuppressWarnings({"squid:S106"})
+ private static void outputResult(ResultSet resultSet)
+ throws SQLException, UnsupportedEncodingException {
+ if (resultSet != null) {
+ System.out.println("--------------------------");
+ final ResultSetMetaData metaData = resultSet.getMetaData();
+ final int columnCount = metaData.getColumnCount();
+ for (int i = 0; i < columnCount; i++) {
+ System.out.print(metaData.getColumnLabel(i + 1) + " ");
+ }
+ System.out.println();
+
+ while (resultSet.next()) {
+ for (int i = 1; ; i++) {
+ System.out.print(
+ resultSet.getString(i) + " (" + new String(resultSet.getBytes(i), "GB18030") + ")");
+ if (i < columnCount) {
+ System.out.print(", ");
+ } else {
+ System.out.println();
+ break;
+ }
+ }
+ }
+ System.out.println("--------------------------\n");
+ }
+ }
+}
diff --git a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java
index f321bd9d800..ffc79a41a72 100644
--- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java
+++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java
@@ -129,7 +129,14 @@ public class IoTDBStatement implements Statement {
ZoneId zoneId,
int seconds)
throws SQLException {
- this(connection, client, sessionId, Config.DEFAULT_FETCH_SIZE, zoneId, Charset.defaultCharset(), seconds);
+ this(
+ connection,
+ client,
+ sessionId,
+ Config.DEFAULT_FETCH_SIZE,
+ zoneId,
+ Charset.defaultCharset(),
+ seconds);
}
// Only for tests
diff --git a/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBJDBCResultSetTest.java b/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBJDBCResultSetTest.java
index 46595d3d79c..e69c7673d66 100644
--- a/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBJDBCResultSetTest.java
+++ b/iotdb-client/jdbc/src/test/java/org/apache/iotdb/jdbc/IoTDBJDBCResultSetTest.java
@@ -19,23 +19,6 @@
package org.apache.iotdb.jdbc;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import java.io.IOException;
-import java.nio.ByteBuffer;
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.Statement;
-import java.sql.Types;
-import java.time.ZoneId;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
import org.apache.iotdb.common.rpc.thrift.TSStatus;
import org.apache.iotdb.rpc.RpcUtils;
import org.apache.iotdb.service.rpc.thrift.IClientRPCService;
@@ -46,6 +29,7 @@ import org.apache.iotdb.service.rpc.thrift.TSFetchMetadataReq;
import org.apache.iotdb.service.rpc.thrift.TSFetchMetadataResp;
import org.apache.iotdb.service.rpc.thrift.TSFetchResultsReq;
import org.apache.iotdb.service.rpc.thrift.TSFetchResultsResp;
+
import org.apache.tsfile.enums.TSDataType;
import org.apache.tsfile.read.common.block.TsBlockBuilder;
import org.apache.tsfile.read.common.block.column.TsBlockSerde;
@@ -55,6 +39,24 @@ import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.Statement;
+import java.sql.Types;
+import java.time.ZoneId;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
/*
This class is designed to test the function of TsfileQueryResultSet.
This class also sheds light on the complete execution process of a query sql from the jdbc perspective.