Skip to content

Commit bfb4140

Browse files
JDBC: add charset support in connection properties (#240)
1 parent 1162a3b commit bfb4140

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

src/UserGuide/Master/API/Programming-JDBC.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,86 @@ String url = "jdbc:iotdb://127.0.0.1:6667?version=V_1_0";
211211
````
212212
The parameter `version` represents the SQL semantic version used by the client, which is used in order to be compatible with the SQL semantics of `0.12` when upgrading to `0.13`.
213213
The possible values are: `V_0_12`, `V_0_13`, `V_1_0`.
214+
215+
In addition, IoTDB provides additional interfaces in JDBC for users to read and write the database using different character sets (e.g., GB18030) in the connection.
216+
The default character set for IoTDB is UTF-8. When users want to use a character set other than UTF-8, they need to specify the charset property in the JDBC connection. For example:
217+
1. Create a connection using the GB18030 charset:
218+
```java
219+
DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667?charset=GB18030", "root", "root");
220+
```
221+
2. When executing SQL with the `IoTDBStatement` interface, the SQL can be provided as a `byte[]` array, and it will be parsed into a string according to the specified charset.
222+
```java
223+
public boolean execute(byte[] sql) throws SQLException;
224+
```
225+
3. When outputting query results, the `getBytes` method of `ResultSet` can be used to get `byte[]`, which will be encoded using the charset specified in the connection.
226+
```java
227+
System.out.print(resultSet.getString(i) + " (" + new String(resultSet.getBytes(i), charset) + ")");
228+
```
229+
Here is a complete example:
230+
```java
231+
public class JDBCCharsetExample {
232+
233+
private static final Logger LOGGER = LoggerFactory.getLogger(JDBCCharsetExample.class);
234+
235+
public static void main(String[] args) throws Exception {
236+
Class.forName("org.apache.iotdb.jdbc.IoTDBDriver");
237+
238+
try (final Connection connection =
239+
DriverManager.getConnection(
240+
"jdbc:iotdb://127.0.0.1:6667?charset=GB18030", "root", "root");
241+
final IoTDBStatement statement = (IoTDBStatement) connection.createStatement()) {
242+
243+
final String insertSQLWithGB18030 =
244+
"insert into root.测试(timestamp, 维语, 彝语, 繁体, 蒙文, 简体, 标点符号, 藏语) values(1, 'ئۇيغۇر تىلى', 'ꆈꌠꉙ', \"繁體\", 'ᠮᠣᠩᠭᠣᠯ ᠬᠡᠯᠡ', '简体', '——?!', \"བོད་སྐད།\");";
245+
final byte[] insertSQLWithGB18030Bytes = insertSQLWithGB18030.getBytes("GB18030");
246+
statement.execute(insertSQLWithGB18030Bytes);
247+
} catch (IoTDBSQLException e) {
248+
LOGGER.error("IoTDB Jdbc example error", e);
249+
}
250+
251+
outputResult("GB18030");
252+
outputResult("UTF-8");
253+
outputResult("UTF-16");
254+
outputResult("GBK");
255+
outputResult("ISO-8859-1");
256+
}
257+
258+
private static void outputResult(String charset) throws SQLException {
259+
System.out.println("[Charset: " + charset + "]");
260+
try (final Connection connection =
261+
DriverManager.getConnection(
262+
"jdbc:iotdb://127.0.0.1:6667?charset=" + charset, "root", "root");
263+
final IoTDBStatement statement = (IoTDBStatement) connection.createStatement()) {
264+
outputResult(statement.executeQuery("select ** from root"), Charset.forName(charset));
265+
} catch (IoTDBSQLException e) {
266+
LOGGER.error("IoTDB Jdbc example error", e);
267+
}
268+
}
269+
270+
private static void outputResult(ResultSet resultSet, Charset charset) throws SQLException {
271+
if (resultSet != null) {
272+
System.out.println("--------------------------");
273+
final ResultSetMetaData metaData = resultSet.getMetaData();
274+
final int columnCount = metaData.getColumnCount();
275+
for (int i = 0; i < columnCount; i++) {
276+
System.out.print(metaData.getColumnLabel(i + 1) + " ");
277+
}
278+
System.out.println();
279+
280+
while (resultSet.next()) {
281+
for (int i = 1; ; i++) {
282+
System.out.print(
283+
resultSet.getString(i) + " (" + new String(resultSet.getBytes(i), charset) + ")");
284+
if (i < columnCount) {
285+
System.out.print(", ");
286+
} else {
287+
System.out.println();
288+
break;
289+
}
290+
}
291+
}
292+
System.out.println("--------------------------\n");
293+
}
294+
}
295+
}
296+
```

src/zh/UserGuide/Master/API/Programming-JDBC.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,86 @@ public class JDBCExample {
206206
String url = "jdbc:iotdb://127.0.0.1:6667?version=V_1_0";
207207
```
208208
version 表示客户端使用的 SQL 语义版本,用于升级 0.13 时兼容 0.12 的 SQL 语义,可能取值有:`V_0_12``V_0_13``V_1_0`
209+
210+
此外,IoTDB 在 JDBC 中提供了额外的接口,供用户在连接中使用不同的字符集(例如 GB18030)读写数据库。
211+
IoTDB 默认的字符集为 UTF-8。当用户期望使用 UTF-8 外的字符集时,需要在 JDBC 的连接中,指定 charset 属性。例如:
212+
1. 使用 GB18030 的 charset 创建连接:
213+
```java
214+
DriverManager.getConnection("jdbc:iotdb://127.0.0.1:6667?charset=GB18030", "root", "root")
215+
```
216+
2. 调用如下 `IoTDBStatement` 接口执行 SQL 时,可以接受 `byte[]` 编码的 SQL,该 SQL 将按照被指定的 charset 解析成字符串。
217+
```java
218+
public boolean execute(byte[] sql) throws SQLException;
219+
```
220+
3. 查询结果输出时,可使用 `ResultSet``getBytes` 方法得到的 `byte[]``byte[]` 的编码使用连接指定的 charset 进行。
221+
```java
222+
System.out.print(resultSet.getString(i) + " (" + new String(resultSet.getBytes(i), charset) + ")");
223+
```
224+
以下是完整示例:
225+
```java
226+
public class JDBCCharsetExample {
227+
228+
private static final Logger LOGGER = LoggerFactory.getLogger(JDBCCharsetExample.class);
229+
230+
public static void main(String[] args) throws Exception {
231+
Class.forName("org.apache.iotdb.jdbc.IoTDBDriver");
232+
233+
try (final Connection connection =
234+
DriverManager.getConnection(
235+
"jdbc:iotdb://127.0.0.1:6667?charset=GB18030", "root", "root");
236+
final IoTDBStatement statement = (IoTDBStatement) connection.createStatement()) {
237+
238+
final String insertSQLWithGB18030 =
239+
"insert into root.测试(timestamp, 维语, 彝语, 繁体, 蒙文, 简体, 标点符号, 藏语) values(1, 'ئۇيغۇر تىلى', 'ꆈꌠꉙ', \"繁體\", 'ᠮᠣᠩᠭᠣᠯ ᠬᠡᠯᠡ', '简体', '——?!', \"བོད་སྐད།\");";
240+
final byte[] insertSQLWithGB18030Bytes = insertSQLWithGB18030.getBytes("GB18030");
241+
statement.execute(insertSQLWithGB18030Bytes);
242+
} catch (IoTDBSQLException e) {
243+
LOGGER.error("IoTDB Jdbc example error", e);
244+
}
245+
246+
outputResult("GB18030");
247+
outputResult("UTF-8");
248+
outputResult("UTF-16");
249+
outputResult("GBK");
250+
outputResult("ISO-8859-1");
251+
}
252+
253+
private static void outputResult(String charset) throws SQLException {
254+
System.out.println("[Charset: " + charset + "]");
255+
try (final Connection connection =
256+
DriverManager.getConnection(
257+
"jdbc:iotdb://127.0.0.1:6667?charset=" + charset, "root", "root");
258+
final IoTDBStatement statement = (IoTDBStatement) connection.createStatement()) {
259+
outputResult(statement.executeQuery("select ** from root"), Charset.forName(charset));
260+
} catch (IoTDBSQLException e) {
261+
LOGGER.error("IoTDB Jdbc example error", e);
262+
}
263+
}
264+
265+
private static void outputResult(ResultSet resultSet, Charset charset) throws SQLException {
266+
if (resultSet != null) {
267+
System.out.println("--------------------------");
268+
final ResultSetMetaData metaData = resultSet.getMetaData();
269+
final int columnCount = metaData.getColumnCount();
270+
for (int i = 0; i < columnCount; i++) {
271+
System.out.print(metaData.getColumnLabel(i + 1) + " ");
272+
}
273+
System.out.println();
274+
275+
while (resultSet.next()) {
276+
for (int i = 1; ; i++) {
277+
System.out.print(
278+
resultSet.getString(i) + " (" + new String(resultSet.getBytes(i), charset) + ")");
279+
if (i < columnCount) {
280+
System.out.print(", ");
281+
} else {
282+
System.out.println();
283+
break;
284+
}
285+
}
286+
}
287+
System.out.println("--------------------------\n");
288+
}
289+
}
290+
}
291+
```

0 commit comments

Comments
 (0)