commit
2b7e6d975f
Binary file not shown.
After Width: | Height: | Size: 42 KiB |
|
@ -1,62 +1,62 @@
|
|||
# Java Connector
|
||||
|
||||
Java连接器支持的系统有:
|
||||
| **CPU类型** | x64(64bit) | | | ARM64 | ARM32 |
|
||||
| ------------ | ------------ | -------- | -------- | -------- | -------- |
|
||||
| **OS类型** | Linux | Win64 | Win32 | Linux | Linux |
|
||||
| **支持与否** | **支持** | **支持** | **支持** | **支持** | **支持** |
|
||||
TDengine 提供了遵循 JDBC 标准(3.0)API 规范的 `taos-jdbcdriver` 实现,可在 maven 的中央仓库 [Sonatype Repository][1] 搜索下载。
|
||||
|
||||
Java连接器的使用请参见<a href=https://www.taosdata.com/blog/2020/11/11/1955.html>视频教程</a>。
|
||||
`taos-jdbcdriver的` 实现包括 2 种形式: JDBC-JNI 和 JDBC-RESTful(taos-jdbcdriver-2.0.16 开始支持 JDBC-RESTful)。 JDBC-JNI 通过调用客户端 libtaos.so(或 taos.dll )的本地方法实现, JDBC-RESTful 则在内部封装了 RESTful 接口实现。
|
||||
|
||||
TDengine 为了方便 Java 应用使用,提供了遵循 JDBC 标准(3.0)API 规范的 `taos-jdbcdriver` 实现。目前可以通过 [Sonatype Repository][1] 搜索并下载。
|
||||

|
||||
|
||||
由于 TDengine 是使用 c 语言开发的,使用 taos-jdbcdriver 驱动包时需要依赖系统对应的本地函数库。
|
||||
上图显示了 3 种 Java 应用使用连接器访问 TDengine 的方式:
|
||||
|
||||
* libtaos.so
|
||||
在 linux 系统中成功安装 TDengine 后,依赖的本地函数库 libtaos.so 文件会被自动拷贝至 /usr/lib/libtaos.so,该目录包含在 Linux 自动扫描路径上,无需单独指定。
|
||||
* JDBC-JNI:Java 应用在物理节点1(pnode1)上使用 JDBC-JNI 的 API ,直接调用客户端 API(libtaos.so 或 taos.dll)将写入和查询请求发送到位于物理节点2(pnode2)上的 taosd 实例。
|
||||
* RESTful:应用将 SQL 发送给位于物理节点2(pnode2)上的 RESTful 连接器,再调用客户端 API(libtaos.so)。
|
||||
* JDBC-RESTful:Java 应用通过 JDBC-RESTful 的 API ,将 SQL 封装成一个 RESTful 请求,发送给物理节点2的 RESTful 连接器。
|
||||
|
||||
* taos.dll
|
||||
在 windows 系统中安装完客户端之后,驱动包依赖的 taos.dll 文件会自动拷贝到系统默认搜索路径 C:/Windows/System32 下,同样无需要单独指定。
|
||||
TDengine 的 JDBC 驱动实现尽可能与关系型数据库驱动保持一致,但时序空间数据库与关系对象型数据库服务的对象和技术特征存在差异,导致 `taos-jdbcdriver` 与传统的 JDBC driver 也存在一定差异。在使用时需要注意以下几点:
|
||||
|
||||
> 注意:在 windows 环境开发时需要安装 TDengine 对应的 [windows 客户端][14],Linux 服务器安装完 TDengine 之后默认已安装 client,也可以单独安装 [Linux 客户端][15] 连接远程 TDengine Server。
|
||||
|
||||
TDengine 的 JDBC 驱动实现尽可能的与关系型数据库驱动保持一致,但时序空间数据库与关系对象型数据库服务的对象和技术特征的差异导致 taos-jdbcdriver 并未完全实现 JDBC 标准规范。在使用时需要注意以下几点:
|
||||
|
||||
* TDengine 不提供针对单条数据记录的删除和修改的操作,驱动中也没有支持相关方法。
|
||||
* 由于不支持删除和修改,所以也不支持事务操作。
|
||||
* TDengine 目前不支持针对单条数据记录的删除操作。
|
||||
* 目前不支持事务操作。
|
||||
* 目前不支持表间的 union 操作。
|
||||
* 目前不支持嵌套查询(nested query),对每个 Connection 的实例,至多只能有一个打开的 ResultSet 实例;如果在 ResultSet还没关闭的情况下执行了新的查询,TSDBJDBCDriver 则会自动关闭上一个 ResultSet。
|
||||
* 目前不支持嵌套查询(nested query)。
|
||||
* 对每个 Connection 的实例,至多只能有一个打开的 ResultSet 实例;如果在 ResultSet 还没关闭的情况下执行了新的查询,taos-jdbcdriver 会自动关闭上一个 ResultSet。
|
||||
|
||||
## TAOS-JDBCDriver 版本以及支持的 TDengine 版本和 JDK 版本
|
||||
|
||||
| taos-jdbcdriver 版本 | TDengine 版本 | JDK 版本 |
|
||||
| --- | --- | --- |
|
||||
| 2.0.12 及以上 | 2.0.8.0 及以上 | 1.8.x |
|
||||
| 2.0.4 - 2.0.11 | 2.0.0.0 - 2.0.7.x | 1.8.x |
|
||||
| 1.0.3 | 1.6.1.x 及以上 | 1.8.x |
|
||||
| 1.0.2 | 1.6.1.x 及以上 | 1.8.x |
|
||||
| 1.0.1 | 1.6.1.x 及以上 | 1.8.x |
|
||||
## JDBC-JNI和JDBC-RESTful的对比
|
||||
|
||||
## TDengine DataType 和 Java DataType
|
||||
<table >
|
||||
<tr align="center"><th>对比项</th><th>JDBC-JNI</th><th>JDBC-RESTful</th></tr>
|
||||
<tr align="center">
|
||||
<td>支持的操作系统</td>
|
||||
<td>linux、windows</td>
|
||||
<td>全平台</td>
|
||||
</tr>
|
||||
<tr align="center">
|
||||
<td>是否需要安装 client</td>
|
||||
<td>需要</td>
|
||||
<td>不需要</td>
|
||||
</tr>
|
||||
<tr align="center">
|
||||
<td>server 升级后是否需要升级 client</td>
|
||||
<td>需要</td>
|
||||
<td>不需要</td>
|
||||
</tr>
|
||||
<tr align="center">
|
||||
<td>写入性能</td>
|
||||
<td colspan="2">JDBC-RESTful 是 JDBC-JNI 的 50%~90% </td>
|
||||
</tr>
|
||||
<tr align="center">
|
||||
<td>查询性能</td>
|
||||
<td colspan="2">JDBC-RESTful 与 JDBC-JNI 没有差别</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
TDengine 目前支持时间戳、数字、字符、布尔类型,与 Java 对应类型转换如下:
|
||||
|
||||
| TDengine DataType | Java DataType |
|
||||
| --- | --- |
|
||||
| TIMESTAMP | java.sql.Timestamp |
|
||||
| INT | java.lang.Integer |
|
||||
| BIGINT | java.lang.Long |
|
||||
| FLOAT | java.lang.Float |
|
||||
| DOUBLE | java.lang.Double |
|
||||
| SMALLINT, TINYINT |java.lang.Short |
|
||||
| BOOL | java.lang.Boolean |
|
||||
| BINARY, NCHAR | java.lang.String |
|
||||
|
||||
## 如何获取 TAOS-JDBCDriver
|
||||
## 如何获取 taos-jdbcdriver
|
||||
|
||||
### maven 仓库
|
||||
|
||||
目前 taos-jdbcdriver 已经发布到 [Sonatype Repository][1] 仓库,且各大仓库都已同步。
|
||||
|
||||
* [sonatype][8]
|
||||
* [mvnrepository][9]
|
||||
* [maven.aliyun][10]
|
||||
|
@ -67,30 +67,63 @@ maven 项目中使用如下 pom.xml 配置即可:
|
|||
<dependency>
|
||||
<groupId>com.taosdata.jdbc</groupId>
|
||||
<artifactId>taos-jdbcdriver</artifactId>
|
||||
<version>2.0.4</version>
|
||||
<version>2.0.16</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
### 源码编译打包
|
||||
|
||||
下载 [TDengine][3] 源码之后,进入 taos-jdbcdriver 源码目录 `src/connector/jdbc` 执行 `mvn clean package` 即可生成相应 jar 包。
|
||||
下载 [TDengine][3] 源码之后,进入 taos-jdbcdriver 源码目录 `src/connector/jdbc` 执行 `mvn clean package -Dmaven.test.skip=true` 即可生成相应 jar 包。
|
||||
|
||||
## 使用说明
|
||||
|
||||
|
||||
## JDBC的使用说明
|
||||
|
||||
### 获取连接
|
||||
|
||||
#### 通过JdbcUrl获取连接
|
||||
#### 指定URL获取连接
|
||||
|
||||
通过指定URL获取连接,如下所示:
|
||||
|
||||
```java
|
||||
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
|
||||
String jdbcUrl = "jdbc:TAOS-RS://taosdemo.com:6041/test?user=root&password=taosdata";
|
||||
Connection conn = DriverManager.getConnection(jdbcUrl);
|
||||
```
|
||||
|
||||
以上示例,使用 **JDBC-RESTful** 的 driver,建立了到 hostname 为 taosdemo.com,端口为 6041,数据库名为 test 的连接。这个 URL 中指定用户名(user)为 root,密码(password)为 taosdata。
|
||||
|
||||
使用 JDBC-RESTful 接口,不需要依赖本地函数库。与 JDBC-JNI 相比,仅需要:
|
||||
|
||||
1. driverClass 指定为“com.taosdata.jdbc.rs.RestfulDriver”;
|
||||
2. jdbcUrl 以“jdbc:TAOS-RS://”开头;
|
||||
3. 使用 6041 作为连接端口。
|
||||
|
||||
如果希望获得更好的写入和查询性能,Java 应用可以使用 **JDBC-JNI** 的driver,如下所示:
|
||||
|
||||
通过指定的jdbcUrl获取连接,如下所示:
|
||||
```java
|
||||
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
||||
String jdbcUrl = "jdbc:TAOS://taosdemo.com:6030/test?user=root&password=taosdata";
|
||||
Connection conn = DriverManager.getConnection(jdbcUrl);
|
||||
```
|
||||
以上示例,建立了到hostname为taosdemo.com,端口为6030(TDengine的默认端口),数据库名为test的连接。这个url中指定用户名(user)为root,密码(password)为taosdata。
|
||||
|
||||
以上示例,使用了 JDBC-JNI 的 driver,建立了到 hostname 为 taosdemo.com,端口为 6030(TDengine 的默认端口),数据库名为 test 的连接。这个 URL 中指定用户名(user)为 root,密码(password)为 taosdata。
|
||||
|
||||
**注意**:使用 JDBC-JNI 的 driver,taos-jdbcdriver 驱动包时需要依赖系统对应的本地函数库。
|
||||
|
||||
* libtaos.so
|
||||
在 linux 系统中成功安装 TDengine 后,依赖的本地函数库 libtaos.so 文件会被自动拷贝至 /usr/lib/libtaos.so,该目录包含在 Linux 自动扫描路径上,无需单独指定。
|
||||
|
||||
* taos.dll
|
||||
在 windows 系统中安装完客户端之后,驱动包依赖的 taos.dll 文件会自动拷贝到系统默认搜索路径 C:/Windows/System32 下,同样无需要单独指定。
|
||||
|
||||
> 在 windows 环境开发时需要安装 TDengine 对应的 [windows 客户端][14],Linux 服务器安装完 TDengine 之后默认已安装 client,也可以单独安装 [Linux 客户端][15] 连接远程 TDengine Server。
|
||||
|
||||
JDBC-JNI 的使用请参见<a href=https://www.taosdata.com/blog/2020/11/11/1955.html>视频教程</a>。
|
||||
|
||||
TDengine 的 JDBC URL 规范格式为:
|
||||
`jdbc:TAOS://[host_name]:[port]/[database_name]?[user={user}|&password={password}|&charset={charset}|&cfgdir={config_dir}|&locale={locale}|&timezone={timezone}]`
|
||||
`jdbc:[TAOS|TAOS-RS]://[host_name]:[port]/[database_name]?[user={user}|&password={password}|&charset={charset}|&cfgdir={config_dir}|&locale={locale}|&timezone={timezone}]`
|
||||
|
||||
url中的配置参数如下:
|
||||
* user:登录 TDengine 用户名,默认值 root。
|
||||
* password:用户登录密码,默认值 taosdata。
|
||||
|
@ -99,13 +132,17 @@ url中的配置参数如下:
|
|||
* locale:客户端语言环境,默认值系统当前 locale。
|
||||
* timezone:客户端使用的时区,默认值为系统当前时区。
|
||||
|
||||
#### 使用JdbcUrl和Properties获取连接
|
||||
|
||||
除了通过指定的jdbcUrl获取连接,还可以使用Properties指定建立连接时的参数,如下所示:
|
||||
|
||||
#### 指定URL和Properties获取连接
|
||||
|
||||
除了通过指定的 URL 获取连接,还可以使用 Properties 指定建立连接时的参数,如下所示:
|
||||
```java
|
||||
public Connection getConn() throws Exception{
|
||||
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
||||
// Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
|
||||
String jdbcUrl = "jdbc:TAOS://taosdemo.com:6030/test?user=root&password=taosdata";
|
||||
// String jdbcUrl = "jdbc:TAOS-RS://taosdemo.com:6041/test?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");
|
||||
|
@ -114,9 +151,10 @@ public Connection getConn() throws Exception{
|
|||
return conn;
|
||||
}
|
||||
```
|
||||
以上示例,建立一个到hostname为taosdemo.com,端口为6030,数据库名为test的连接。这个连接在url中指定了用户名(user)为root,密码(password)为taosdata,并在connProps中指定了使用的字符集、语言环境、时区等信息。
|
||||
|
||||
properties中的配置参数如下:
|
||||
以上示例,建立一个到 hostname 为 taosdemo.com,端口为 6030,数据库名为 test 的连接。注释为使用 JDBC-RESTful 时的方法。这个连接在 url 中指定了用户名(user)为 root,密码(password)为 taosdata,并在 connProps 中指定了使用的字符集、语言环境、时区等信息。
|
||||
|
||||
properties 中的配置参数如下:
|
||||
* TSDBDriver.PROPERTY_KEY_USER:登录 TDengine 用户名,默认值 root。
|
||||
* TSDBDriver.PROPERTY_KEY_PASSWORD:用户登录密码,默认值 taosdata。
|
||||
* TSDBDriver.PROPERTY_KEY_CONFIG_DIR:客户端配置文件目录路径,Linux OS 上默认值 /etc/taos ,Windows OS 上默认值 C:/TDengine/cfg。
|
||||
|
@ -124,10 +162,14 @@ properties中的配置参数如下:
|
|||
* TSDBDriver.PROPERTY_KEY_LOCALE:客户端语言环境,默认值系统当前 locale。
|
||||
* TSDBDriver.PROPERTY_KEY_TIME_ZONE:客户端使用的时区,默认值为系统当前时区。
|
||||
|
||||
|
||||
|
||||
#### 使用客户端配置文件建立连接
|
||||
当使用JDBC连接TDengine集群时,可以使用客户端配置文件,在客户端配置文件中指定集群的firstEp、secondEp参数。
|
||||
|
||||
当使用 JDBC-JNI 连接 TDengine 集群时,可以使用客户端配置文件,在客户端配置文件中指定集群的 firstEp、secondEp参数。
|
||||
如下所示:
|
||||
1. 在java中不指定hostname和port
|
||||
|
||||
1. 在 Java 应用中不指定 hostname 和 port
|
||||
```java
|
||||
public Connection getConn() throws Exception{
|
||||
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
||||
|
@ -140,7 +182,7 @@ public Connection getConn() throws Exception{
|
|||
return conn;
|
||||
}
|
||||
```
|
||||
2. 在配置文件中指定firstEp和secondEp
|
||||
2. 在配置文件中指定 firstEp 和 secondEp
|
||||
```
|
||||
# first fully qualified domain name (FQDN) for TDengine system
|
||||
firstEp cluster_node1:6030
|
||||
|
@ -155,17 +197,19 @@ secondEp cluster_node2:6030
|
|||
# locale en_US.UTF-8
|
||||
```
|
||||
|
||||
以上示例,jdbc会使用客户端的配置文件,建立到hostname为cluster_node1,端口为6030,数据库名为test的连接。当集群中firstEp节点失效时,JDBC会尝试使用secondEp连接集群。
|
||||
TDengine中,只要保证firstEp和secondEp中一个节点有效,就可以正常建立到集群的连接。
|
||||
以上示例,jdbc 会使用客户端的配置文件,建立到 hostname 为 cluster_node1、端口为 6030、数据库名为 test 的连接。当集群中 firstEp 节点失效时,JDBC 会尝试使用 secondEp 连接集群。
|
||||
TDengine 中,只要保证 firstEp 和 secondEp 中一个节点有效,就可以正常建立到集群的连接。
|
||||
|
||||
> 注意:这里的配置文件指的是调用JDBC Connector的应用程序所在机器上的配置文件,Linux OS 上默认值 /etc/taos/taos.cfg ,Windows OS 上默认值 C://TDengine/cfg/taos.cfg。
|
||||
> 注意:这里的配置文件指的是调用 JDBC Connector 的应用程序所在机器上的配置文件,Linux OS 上默认值 /etc/taos/taos.cfg ,Windows OS 上默认值 C://TDengine/cfg/taos.cfg。
|
||||
|
||||
#### 配置参数的优先级
|
||||
通过以上3种方式获取连接,如果配置参数在url、Properties、客户端配置文件中有重复,则参数的`优先级由高到低`分别如下:
|
||||
|
||||
通过以上 3 种方式获取连接,如果配置参数在 url、Properties、客户端配置文件中有重复,则参数的`优先级由高到低`分别如下:
|
||||
1. JDBC URL 参数,如上所述,可以在 JDBC URL 的参数中指定。
|
||||
2. Properties connProps
|
||||
3. 客户端配置文件 taos.cfg
|
||||
例如:在url中指定了password为taosdata,在Properties中指定了password为taosdemo,那么,JDBC会使用url中的password建立连接。
|
||||
|
||||
例如:在 url 中指定了 password 为 taosdata,在 Properties 中指定了 password 为 taosdemo,那么,JDBC 会使用 url 中的 password 建立连接。
|
||||
|
||||
> 更多详细配置请参考[客户端配置][13]
|
||||
|
||||
|
@ -183,6 +227,7 @@ stmt.executeUpdate("use db");
|
|||
// create table
|
||||
stmt.executeUpdate("create table if not exists tb (ts timestamp, temperature int, humidity float)");
|
||||
```
|
||||
|
||||
> 注意:如果不使用 `use db` 指定数据库,则后续对表的操作都需要增加数据库名称作为前缀,如 db.tb。
|
||||
|
||||
### 插入数据
|
||||
|
@ -193,6 +238,7 @@ int affectedRows = stmt.executeUpdate("insert into tb values(now, 23, 10.3) (now
|
|||
|
||||
System.out.println("insert " + affectedRows + " rows.");
|
||||
```
|
||||
|
||||
> now 为系统内部函数,默认为服务器当前时间。
|
||||
> `now + 1s` 代表服务器当前时间往后加 1 秒,数字后面代表时间单位:a(毫秒), s(秒), m(分), h(小时), d(天),w(周), n(月), y(年)。
|
||||
|
||||
|
@ -214,6 +260,7 @@ while(resultSet.next()){
|
|||
System.out.printf("%s, %d, %s\n", ts, temperature, humidity);
|
||||
}
|
||||
```
|
||||
|
||||
> 查询和操作关系型数据库一致,使用下标获取返回字段内容时从 1 开始,建议使用字段名称获取。
|
||||
|
||||
### 订阅
|
||||
|
@ -248,7 +295,7 @@ while(true) {
|
|||
}
|
||||
```
|
||||
|
||||
`consume` 方法返回一个结果集,其中包含从上次 `consume` 到目前为止的所有新数据。请务必按需选择合理的调用 `consume` 的频率(如例子中的`Thread.sleep(1000)`),否则会给服务端造成不必要的压力。
|
||||
`consume` 方法返回一个结果集,其中包含从上次 `consume` 到目前为止的所有新数据。请务必按需选择合理的调用 `consume` 的频率(如例子中的 `Thread.sleep(1000)`),否则会给服务端造成不必要的压力。
|
||||
|
||||
#### 关闭订阅
|
||||
|
||||
|
@ -265,8 +312,11 @@ resultSet.close();
|
|||
stmt.close();
|
||||
conn.close();
|
||||
```
|
||||
|
||||
> `注意务必要将 connection 进行关闭`,否则会出现连接泄露。
|
||||
|
||||
|
||||
|
||||
## 与连接池使用
|
||||
|
||||
**HikariCP**
|
||||
|
@ -306,6 +356,7 @@ conn.close();
|
|||
connection.close(); // put back to conneciton pool
|
||||
}
|
||||
```
|
||||
|
||||
> 通过 HikariDataSource.getConnection() 获取连接后,使用完成后需要调用 close() 方法,实际上它并不会关闭连接,只是放回连接池中。
|
||||
> 更多 HikariCP 使用问题请查看[官方说明][5]
|
||||
|
||||
|
@ -356,6 +407,7 @@ public static void main(String[] args) throws Exception {
|
|||
connection.close(); // put back to conneciton pool
|
||||
}
|
||||
```
|
||||
|
||||
> 更多 druid 使用问题请查看[官方说明][6]
|
||||
|
||||
**注意事项**
|
||||
|
@ -370,10 +422,43 @@ server_status()|
|
|||
Query OK, 1 row(s) in set (0.000141s)
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 与框架使用
|
||||
|
||||
* Spring JdbcTemplate 中使用 taos-jdbcdriver,可参考 [SpringJdbcTemplate][11]
|
||||
* Springboot + Mybatis 中使用,可参考 [springbootdemo][12]
|
||||
* Springboot + Mybatis 中使用,可参考 [springbootdemo
|
||||
|
||||
|
||||
|
||||
## TAOS-JDBCDriver 版本以及支持的 TDengine 版本和 JDK 版本
|
||||
|
||||
| taos-jdbcdriver 版本 | TDengine 版本 | JDK 版本 |
|
||||
| -------------------- | ----------------- | -------- |
|
||||
| 2.0.12 及以上 | 2.0.8.0 及以上 | 1.8.x |
|
||||
| 2.0.4 - 2.0.11 | 2.0.0.0 - 2.0.7.x | 1.8.x |
|
||||
| 1.0.3 | 1.6.1.x 及以上 | 1.8.x |
|
||||
| 1.0.2 | 1.6.1.x 及以上 | 1.8.x |
|
||||
| 1.0.1 | 1.6.1.x 及以上 | 1.8.x |
|
||||
|
||||
|
||||
|
||||
## TDengine DataType 和 Java DataType
|
||||
|
||||
TDengine 目前支持时间戳、数字、字符、布尔类型,与 Java 对应类型转换如下:
|
||||
|
||||
| TDengine DataType | Java DataType |
|
||||
| ----------------- | ------------------ |
|
||||
| TIMESTAMP | java.sql.Timestamp |
|
||||
| INT | java.lang.Integer |
|
||||
| BIGINT | java.lang.Long |
|
||||
| FLOAT | java.lang.Float |
|
||||
| DOUBLE | java.lang.Double |
|
||||
| SMALLINT, TINYINT | java.lang.Short |
|
||||
| BOOL | java.lang.Boolean |
|
||||
| BINARY, NCHAR | java.lang.String |
|
||||
|
||||
|
||||
|
||||
## 常见问题
|
||||
|
||||
|
@ -381,7 +466,7 @@ Query OK, 1 row(s) in set (0.000141s)
|
|||
|
||||
**原因**:程序没有找到依赖的本地函数库 taos。
|
||||
|
||||
**解决方法**:windows 下可以将 C:\TDengine\driver\taos.dll 拷贝到 C:\Windows\System32\ 目录下,linux 下将建立如下软链 ` ln -s /usr/local/taos/driver/libtaos.so.x.x.x.x /usr/lib/libtaos.so` 即可。
|
||||
**解决方法**:windows 下可以将 C:\TDengine\driver\taos.dll 拷贝到 C:\Windows\System32\ 目录下,linux 下将建立如下软链 `ln -s /usr/local/taos/driver/libtaos.so.x.x.x.x /usr/lib/libtaos.so` 即可。
|
||||
|
||||
* java.lang.UnsatisfiedLinkError: taos.dll Can't load AMD 64 bit on a IA 32-bit platform
|
||||
|
||||
|
@ -406,3 +491,4 @@ Query OK, 1 row(s) in set (0.000141s)
|
|||
[13]: https://www.taosdata.com/cn/documentation20/administrator/#%E5%AE%A2%E6%88%B7%E7%AB%AF%E9%85%8D%E7%BD%AE
|
||||
[14]: https://www.taosdata.com/cn/all-downloads/#TDengine-Windows-Client
|
||||
[15]: https://www.taosdata.com/cn/getting-started/#%E5%BF%AB%E9%80%9F%E4%B8%8A%E6%89%8B
|
||||
|
||||
|
|
|
@ -535,6 +535,12 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Param catalog : database名称,"" 表示不属于任何database的table,null表示不使用database来缩小范围
|
||||
* @Param schemaPattern : schema名称,""表示
|
||||
* @Param tableNamePattern : 表名满足tableNamePattern的表, null表示返回所有表
|
||||
* @Param types : 表类型,null表示返回所有类型
|
||||
*/
|
||||
public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) throws SQLException {
|
||||
if (conn == null || conn.isClosed()) {
|
||||
throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL));
|
||||
|
@ -544,10 +550,92 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData {
|
|||
if (catalog == null || catalog.isEmpty())
|
||||
return null;
|
||||
|
||||
stmt.executeUpdate("use " + catalog);
|
||||
ResultSet resultSet0 = stmt.executeQuery("show tables");
|
||||
GetTablesResultSet getTablesResultSet = new GetTablesResultSet(resultSet0, catalog, schemaPattern, tableNamePattern, types);
|
||||
return getTablesResultSet;
|
||||
ResultSet databases = stmt.executeQuery("show databases");
|
||||
String dbname = null;
|
||||
while (databases.next()) {
|
||||
dbname = databases.getString("name");
|
||||
if (dbname.equalsIgnoreCase(catalog))
|
||||
break;
|
||||
}
|
||||
databases.close();
|
||||
if (dbname == null)
|
||||
return null;
|
||||
|
||||
stmt.execute("use " + dbname);
|
||||
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
|
||||
List<ColumnMetaData> columnMetaDataList = new ArrayList<>();
|
||||
ColumnMetaData col1 = new ColumnMetaData();
|
||||
col1.setColIndex(1);
|
||||
col1.setColName("TABLE_CAT");
|
||||
col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col1);
|
||||
ColumnMetaData col2 = new ColumnMetaData();
|
||||
col2.setColIndex(2);
|
||||
col2.setColName("TABLE_SCHEM");
|
||||
col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col2);
|
||||
ColumnMetaData col3 = new ColumnMetaData();
|
||||
col3.setColIndex(3);
|
||||
col3.setColName("TABLE_NAME");
|
||||
col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col3);
|
||||
ColumnMetaData col4 = new ColumnMetaData();
|
||||
col4.setColIndex(4);
|
||||
col4.setColName("TABLE_TYPE");
|
||||
col4.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col4);
|
||||
ColumnMetaData col5 = new ColumnMetaData();
|
||||
col5.setColIndex(5);
|
||||
col5.setColName("REMARKS");
|
||||
col5.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col5);
|
||||
ColumnMetaData col6 = new ColumnMetaData();
|
||||
col6.setColIndex(6);
|
||||
col6.setColName("TYPE_CAT");
|
||||
col6.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col6);
|
||||
ColumnMetaData col7 = new ColumnMetaData();
|
||||
col7.setColIndex(7);
|
||||
col7.setColName("TYPE_SCHEM");
|
||||
col7.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col7);
|
||||
ColumnMetaData col8 = new ColumnMetaData();
|
||||
col8.setColIndex(8);
|
||||
col8.setColName("TYPE_NAME");
|
||||
col8.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col8);
|
||||
ColumnMetaData col9 = new ColumnMetaData();
|
||||
col9.setColIndex(9);
|
||||
col9.setColName("SELF_REFERENCING_COL_NAME");
|
||||
col9.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col9);
|
||||
ColumnMetaData col10 = new ColumnMetaData();
|
||||
col10.setColIndex(10);
|
||||
col10.setColName("REF_GENERATION");
|
||||
col10.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col10);
|
||||
resultSet.setColumnMetaDataList(columnMetaDataList);
|
||||
|
||||
List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
|
||||
ResultSet tables = stmt.executeQuery("show tables");
|
||||
while (tables.next()) {
|
||||
TSDBResultSetRowData rowData = new TSDBResultSetRowData(10);
|
||||
rowData.setString(2, tables.getString("table_name"));
|
||||
rowData.setString(3, "TABLE");
|
||||
rowData.setString(4, "");
|
||||
rowDataList.add(rowData);
|
||||
}
|
||||
|
||||
ResultSet stables = stmt.executeQuery("show stables");
|
||||
while (stables.next()) {
|
||||
TSDBResultSetRowData rowData = new TSDBResultSetRowData(10);
|
||||
rowData.setString(2, stables.getString("name"));
|
||||
rowData.setString(3, "TABLE");
|
||||
rowData.setString(4, "STABLE");
|
||||
rowDataList.add(rowData);
|
||||
}
|
||||
resultSet.setRowDataList(rowDataList);
|
||||
return resultSet;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -560,125 +648,172 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData {
|
|||
throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL));
|
||||
|
||||
try (Statement stmt = conn.createStatement()) {
|
||||
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
|
||||
// set up ColumnMetaDataList
|
||||
List<ColumnMetaData> columnMetaDataList = new ArrayList<>(24);
|
||||
// TABLE_CAT
|
||||
ColumnMetaData col1 = new ColumnMetaData();
|
||||
col1.setColIndex(1);
|
||||
col1.setColName("TABLE_CAT");
|
||||
col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col1);
|
||||
resultSet.setColumnMetaDataList(columnMetaDataList);
|
||||
|
||||
List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
|
||||
ResultSet rs = stmt.executeQuery("show databases");
|
||||
return new CatalogResultSet(rs);
|
||||
while (rs.next()) {
|
||||
TSDBResultSetRowData rowData = new TSDBResultSetRowData(1);
|
||||
rowData.setString(0, rs.getString("name"));
|
||||
rowDataList.add(rowData);
|
||||
}
|
||||
resultSet.setRowDataList(rowDataList);
|
||||
return resultSet;
|
||||
}
|
||||
}
|
||||
|
||||
public ResultSet getTableTypes() throws SQLException {
|
||||
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
|
||||
if (conn == null || conn.isClosed())
|
||||
throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL));
|
||||
|
||||
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
|
||||
// set up ColumnMetaDataList
|
||||
List<ColumnMetaData> columnMetaDataList = new ArrayList<>(1);
|
||||
List<ColumnMetaData> columnMetaDataList = new ArrayList<>();
|
||||
ColumnMetaData colMetaData = new ColumnMetaData();
|
||||
colMetaData.setColIndex(0);
|
||||
colMetaData.setColName("TABLE_TYPE");
|
||||
colMetaData.setColSize(10);
|
||||
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_BINARY);
|
||||
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(colMetaData);
|
||||
resultSet.setColumnMetaDataList(columnMetaDataList);
|
||||
|
||||
// set up rowDataList
|
||||
List<TSDBResultSetRowData> rowDataList = new ArrayList<>(2);
|
||||
TSDBResultSetRowData rowData = new TSDBResultSetRowData();
|
||||
List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
|
||||
TSDBResultSetRowData rowData = new TSDBResultSetRowData(1);
|
||||
rowData.setString(0, "TABLE");
|
||||
rowDataList.add(rowData);
|
||||
rowData = new TSDBResultSetRowData();
|
||||
rowData = new TSDBResultSetRowData(1);
|
||||
rowData.setString(0, "STABLE");
|
||||
rowDataList.add(rowData);
|
||||
|
||||
resultSet.setColumnMetaDataList(columnMetaDataList);
|
||||
resultSet.setRowDataList(rowDataList);
|
||||
|
||||
return resultSet;
|
||||
}
|
||||
|
||||
public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern)
|
||||
throws SQLException {
|
||||
public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException {
|
||||
if (conn == null || conn.isClosed())
|
||||
throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL));
|
||||
|
||||
/** add by zyyang **********/
|
||||
Statement stmt = null;
|
||||
if (null != conn && !conn.isClosed()) {
|
||||
stmt = conn.createStatement();
|
||||
try (Statement stmt = conn.createStatement()) {
|
||||
if (catalog == null || catalog.isEmpty())
|
||||
return null;
|
||||
|
||||
stmt.executeUpdate("use " + catalog);
|
||||
ResultSet databases = stmt.executeQuery("show databases");
|
||||
String dbname = null;
|
||||
while (databases.next()) {
|
||||
dbname = databases.getString("name");
|
||||
if (dbname.equalsIgnoreCase(catalog))
|
||||
break;
|
||||
}
|
||||
databases.close();
|
||||
if (dbname == null)
|
||||
return null;
|
||||
|
||||
stmt.execute("use " + dbname);
|
||||
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
|
||||
// set up ColumnMetaDataList
|
||||
List<ColumnMetaData> columnMetaDataList = new ArrayList<>(24);
|
||||
// TABLE_CAT
|
||||
ColumnMetaData col1 = new ColumnMetaData();
|
||||
col1.setColIndex(1);
|
||||
col1.setColName("TABLE_CAT");
|
||||
col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col1);
|
||||
// TABLE_SCHEM
|
||||
ColumnMetaData col2 = new ColumnMetaData();
|
||||
col2.setColIndex(2);
|
||||
col2.setColName("TABLE_SCHEM");
|
||||
col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col2);
|
||||
// TABLE_NAME
|
||||
ColumnMetaData col3 = new ColumnMetaData();
|
||||
col3.setColIndex(3);
|
||||
col3.setColName("TABLE_NAME");
|
||||
col3.setColSize(193);
|
||||
col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col3);
|
||||
// COLUMN_NAME
|
||||
ColumnMetaData col4 = new ColumnMetaData();
|
||||
col4.setColIndex(4);
|
||||
col4.setColName("COLUMN_NAME");
|
||||
col4.setColSize(65);
|
||||
col4.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col4);
|
||||
// DATA_TYPE
|
||||
ColumnMetaData col5 = new ColumnMetaData();
|
||||
col5.setColIndex(5);
|
||||
col5.setColName("DATA_TYPE");
|
||||
col5.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
|
||||
columnMetaDataList.add(col5);
|
||||
// TYPE_NAME
|
||||
ColumnMetaData col6 = new ColumnMetaData();
|
||||
col6.setColIndex(6);
|
||||
col6.setColName("TYPE_NAME");
|
||||
col6.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col6);
|
||||
// COLUMN_SIZE
|
||||
ColumnMetaData col7 = new ColumnMetaData();
|
||||
col7.setColIndex(7);
|
||||
col7.setColName("COLUMN_SIZE");
|
||||
col7.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
|
||||
columnMetaDataList.add(col7);
|
||||
// BUFFER_LENGTH ,not used
|
||||
columnMetaDataList.add(null);
|
||||
columnMetaDataList.add(null);
|
||||
// add TABLE_NAME
|
||||
ColumnMetaData colMetaData = new ColumnMetaData();
|
||||
colMetaData.setColIndex(3);
|
||||
colMetaData.setColName("TABLE_NAME");
|
||||
colMetaData.setColSize(193);
|
||||
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_BINARY);
|
||||
columnMetaDataList.add(colMetaData);
|
||||
// add COLUMN_NAME
|
||||
colMetaData = new ColumnMetaData();
|
||||
colMetaData.setColIndex(4);
|
||||
colMetaData.setColName("COLUMN_NAME");
|
||||
colMetaData.setColSize(65);
|
||||
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_BINARY);
|
||||
columnMetaDataList.add(colMetaData);
|
||||
// add DATA_TYPE
|
||||
colMetaData = new ColumnMetaData();
|
||||
colMetaData.setColIndex(5);
|
||||
colMetaData.setColName("DATA_TYPE");
|
||||
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
|
||||
columnMetaDataList.add(colMetaData);
|
||||
// add TYPE_NAME
|
||||
colMetaData = new ColumnMetaData();
|
||||
colMetaData.setColIndex(6);
|
||||
colMetaData.setColName("TYPE_NAME");
|
||||
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_BINARY);
|
||||
columnMetaDataList.add(colMetaData);
|
||||
// add COLUMN_SIZE
|
||||
colMetaData = new ColumnMetaData();
|
||||
colMetaData.setColIndex(7);
|
||||
colMetaData.setColName("COLUMN_SIZE");
|
||||
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
|
||||
columnMetaDataList.add(colMetaData);
|
||||
// add BUFFER_LENGTH ,not used
|
||||
columnMetaDataList.add(null);
|
||||
// add DECIMAL_DIGITS
|
||||
colMetaData = new ColumnMetaData();
|
||||
colMetaData.setColIndex(9);
|
||||
colMetaData.setColName("DECIMAL_DIGITS");
|
||||
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
|
||||
columnMetaDataList.add(colMetaData);
|
||||
// DECIMAL_DIGITS
|
||||
ColumnMetaData col9 = new ColumnMetaData();
|
||||
col9.setColIndex(9);
|
||||
col9.setColName("DECIMAL_DIGITS");
|
||||
col9.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
|
||||
columnMetaDataList.add(col9);
|
||||
// add NUM_PREC_RADIX
|
||||
colMetaData = new ColumnMetaData();
|
||||
colMetaData.setColIndex(10);
|
||||
colMetaData.setColName("NUM_PREC_RADIX");
|
||||
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
|
||||
columnMetaDataList.add(colMetaData);
|
||||
// add NULLABLE
|
||||
colMetaData = new ColumnMetaData();
|
||||
colMetaData.setColIndex(11);
|
||||
colMetaData.setColName("NULLABLE");
|
||||
colMetaData.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
|
||||
columnMetaDataList.add(colMetaData);
|
||||
|
||||
ColumnMetaData col10 = new ColumnMetaData();
|
||||
col10.setColIndex(10);
|
||||
col10.setColName("NUM_PREC_RADIX");
|
||||
col10.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
|
||||
columnMetaDataList.add(col10);
|
||||
// NULLABLE
|
||||
ColumnMetaData col11 = new ColumnMetaData();
|
||||
col11.setColIndex(11);
|
||||
col11.setColName("NULLABLE");
|
||||
col11.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
|
||||
columnMetaDataList.add(col11);
|
||||
// REMARKS
|
||||
ColumnMetaData col12 = new ColumnMetaData();
|
||||
col12.setColIndex(12);
|
||||
col12.setColName("REMARKS");
|
||||
col12.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col12);
|
||||
resultSet.setColumnMetaDataList(columnMetaDataList);
|
||||
|
||||
|
||||
// set up rowDataList
|
||||
ResultSet resultSet0 = stmt.executeQuery("describe " + tableNamePattern);
|
||||
ResultSet rs = stmt.executeQuery("describe " + dbname + "." + tableNamePattern);
|
||||
List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
|
||||
int index = 0;
|
||||
while (resultSet0.next()) {
|
||||
while (rs.next()) {
|
||||
TSDBResultSetRowData rowData = new TSDBResultSetRowData(24);
|
||||
// set TABLE_CAT
|
||||
rowData.setString(0, dbname);
|
||||
// set TABLE_NAME
|
||||
rowData.setString(2, tableNamePattern);
|
||||
// set COLUMN_NAME
|
||||
rowData.setString(3, resultSet0.getString(1));
|
||||
rowData.setString(3, rs.getString("Field"));
|
||||
// set DATA_TYPE
|
||||
String typeName = resultSet0.getString(2);
|
||||
String typeName = rs.getString("Type");
|
||||
rowData.setInt(4, getDataType(typeName));
|
||||
// set TYPE_NAME
|
||||
rowData.setString(5, typeName);
|
||||
// set COLUMN_SIZE
|
||||
int length = resultSet0.getInt(3);
|
||||
int length = rs.getInt("Length");
|
||||
rowData.setInt(6, getColumnSize(typeName, length));
|
||||
// set DECIMAL_DIGITS
|
||||
rowData.setInt(8, getDecimalDigits(typeName));
|
||||
|
@ -686,22 +821,18 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData {
|
|||
rowData.setInt(9, 10);
|
||||
// set NULLABLE
|
||||
rowData.setInt(10, getNullable(index, typeName));
|
||||
// set REMARKS
|
||||
rowData.setString(11, rs.getString("Note"));
|
||||
rowDataList.add(rowData);
|
||||
index++;
|
||||
}
|
||||
resultSet.setRowDataList(rowDataList);
|
||||
|
||||
// GetColumnsResultSet getColumnsResultSet = new GetColumnsResultSet(resultSet0, catalog, schemaPattern, tableNamePattern, columnNamePattern);
|
||||
// return getColumnsResultSet;
|
||||
// DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
|
||||
return resultSet;
|
||||
} else {
|
||||
throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL));
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
/*************************/
|
||||
|
||||
// return getEmptyResultSet();
|
||||
return null;
|
||||
}
|
||||
|
||||
private int getNullable(int index, String typeName) {
|
||||
|
@ -778,7 +909,82 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData {
|
|||
}
|
||||
|
||||
public ResultSet getPrimaryKeys(String catalog, String schema, String table) throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
if (conn == null || conn.isClosed())
|
||||
throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL));
|
||||
|
||||
try (Statement stmt = conn.createStatement()) {
|
||||
if (catalog == null || catalog.isEmpty())
|
||||
return null;
|
||||
|
||||
ResultSet databases = stmt.executeQuery("show databases");
|
||||
String dbname = null;
|
||||
while (databases.next()) {
|
||||
dbname = databases.getString("name");
|
||||
if (dbname.equalsIgnoreCase(catalog))
|
||||
break;
|
||||
}
|
||||
databases.close();
|
||||
if (dbname == null)
|
||||
return null;
|
||||
|
||||
stmt.execute("use " + dbname);
|
||||
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
|
||||
// set up ColumnMetaDataList
|
||||
List<ColumnMetaData> columnMetaDataList = new ArrayList<>();
|
||||
// TABLE_CAT
|
||||
ColumnMetaData col1 = new ColumnMetaData();
|
||||
col1.setColIndex(0);
|
||||
col1.setColName("TABLE_CAT");
|
||||
col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col1);
|
||||
// TABLE_SCHEM
|
||||
ColumnMetaData col2 = new ColumnMetaData();
|
||||
col2.setColIndex(1);
|
||||
col2.setColName("TABLE_SCHEM");
|
||||
col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col2);
|
||||
// TABLE_NAME
|
||||
ColumnMetaData col3 = new ColumnMetaData();
|
||||
col3.setColIndex(2);
|
||||
col3.setColName("TABLE_NAME");
|
||||
col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col3);
|
||||
// COLUMN_NAME
|
||||
ColumnMetaData col4 = new ColumnMetaData();
|
||||
col4.setColIndex(3);
|
||||
col4.setColName("COLUMN_NAME");
|
||||
col4.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col4);
|
||||
// KEY_SEQ
|
||||
ColumnMetaData col5 = new ColumnMetaData();
|
||||
col5.setColIndex(4);
|
||||
col5.setColName("KEY_SEQ");
|
||||
col5.setColType(TSDBConstants.TSDB_DATA_TYPE_INT);
|
||||
columnMetaDataList.add(col5);
|
||||
// PK_NAME
|
||||
ColumnMetaData col6 = new ColumnMetaData();
|
||||
col6.setColIndex(5);
|
||||
col6.setColName("PK_NAME");
|
||||
col6.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col6);
|
||||
resultSet.setColumnMetaDataList(columnMetaDataList);
|
||||
|
||||
// set rowData
|
||||
List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
|
||||
ResultSet rs = stmt.executeQuery("describe " + dbname + "." + table);
|
||||
rs.next();
|
||||
TSDBResultSetRowData rowData = new TSDBResultSetRowData(6);
|
||||
rowData.setString(0, null);
|
||||
rowData.setString(1, null);
|
||||
rowData.setString(2, table);
|
||||
String pkName = rs.getString(1);
|
||||
rowData.setString(3, pkName);
|
||||
rowData.setInt(4, 1);
|
||||
rowData.setString(5, pkName);
|
||||
rowDataList.add(rowData);
|
||||
resultSet.setRowDataList(rowDataList);
|
||||
return resultSet;
|
||||
}
|
||||
}
|
||||
|
||||
public ResultSet getImportedKeys(String catalog, String schema, String table) throws SQLException {
|
||||
|
@ -876,12 +1082,72 @@ public class TSDBDatabaseMetaData implements java.sql.DatabaseMetaData {
|
|||
return false;
|
||||
}
|
||||
|
||||
public ResultSet getSuperTypes(String catalog, String schemaPattern, String typeNamePattern) throws SQLException {
|
||||
public ResultSet getSuperTypes(String catalog, String schemaPattern, String typeNamePattern) throws
|
||||
SQLException {
|
||||
return getEmptyResultSet();
|
||||
}
|
||||
|
||||
public ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) throws SQLException {
|
||||
return getEmptyResultSet();
|
||||
public ResultSet getSuperTables(String catalog, String schemaPattern, String tableNamePattern) throws
|
||||
SQLException {
|
||||
if (conn == null || conn.isClosed())
|
||||
throw new SQLException(TSDBConstants.FixErrMsg(TSDBConstants.JNI_CONNECTION_NULL));
|
||||
|
||||
try (Statement stmt = conn.createStatement()) {
|
||||
if (catalog == null || catalog.isEmpty())
|
||||
return null;
|
||||
|
||||
ResultSet databases = stmt.executeQuery("show databases");
|
||||
String dbname = null;
|
||||
while (databases.next()) {
|
||||
dbname = databases.getString("name");
|
||||
if (dbname.equalsIgnoreCase(catalog))
|
||||
break;
|
||||
}
|
||||
databases.close();
|
||||
if (dbname == null)
|
||||
return null;
|
||||
|
||||
stmt.execute("use " + dbname);
|
||||
DatabaseMetaDataResultSet resultSet = new DatabaseMetaDataResultSet();
|
||||
// set up ColumnMetaDataList
|
||||
List<ColumnMetaData> columnMetaDataList = new ArrayList<>();
|
||||
// TABLE_CAT
|
||||
ColumnMetaData col1 = new ColumnMetaData();
|
||||
col1.setColIndex(0);
|
||||
col1.setColName("TABLE_CAT");
|
||||
col1.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col1);
|
||||
// TABLE_SCHEM
|
||||
ColumnMetaData col2 = new ColumnMetaData();
|
||||
col2.setColIndex(1);
|
||||
col2.setColName("TABLE_SCHEM");
|
||||
col2.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col2);
|
||||
// TABLE_NAME
|
||||
ColumnMetaData col3 = new ColumnMetaData();
|
||||
col3.setColIndex(2);
|
||||
col3.setColName("TABLE_NAME");
|
||||
col3.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col3);
|
||||
// SUPERTABLE_NAME
|
||||
ColumnMetaData col4 = new ColumnMetaData();
|
||||
col4.setColIndex(3);
|
||||
col4.setColName("SUPERTABLE_NAME");
|
||||
col4.setColType(TSDBConstants.TSDB_DATA_TYPE_NCHAR);
|
||||
columnMetaDataList.add(col4);
|
||||
resultSet.setColumnMetaDataList(columnMetaDataList);
|
||||
|
||||
ResultSet rs = stmt.executeQuery("show tables like '" + tableNamePattern + "'");
|
||||
List<TSDBResultSetRowData> rowDataList = new ArrayList<>();
|
||||
while (rs.next()) {
|
||||
TSDBResultSetRowData rowData = new TSDBResultSetRowData(4);
|
||||
rowData.setString(2, rs.getString(1));
|
||||
rowData.setString(3, rs.getString(4));
|
||||
rowDataList.add(rowData);
|
||||
}
|
||||
resultSet.setRowDataList(rowDataList);
|
||||
return resultSet;
|
||||
}
|
||||
}
|
||||
|
||||
public ResultSet getAttributes(String catalog, String schemaPattern, String typeNamePattern,
|
||||
|
|
|
@ -26,9 +26,9 @@ public class TSDBResultSetRowData {
|
|||
public TSDBResultSetRowData(int colSize) {
|
||||
this.setColSize(colSize);
|
||||
}
|
||||
|
||||
|
||||
public TSDBResultSetRowData() {
|
||||
this.data = new ArrayList<Object>();
|
||||
this.data = new ArrayList<>();
|
||||
this.setColSize(0);
|
||||
}
|
||||
|
||||
|
@ -39,7 +39,7 @@ public class TSDBResultSetRowData {
|
|||
if (this.colSize == 0) {
|
||||
return;
|
||||
}
|
||||
this.data = new ArrayList<Object>(colSize);
|
||||
this.data = new ArrayList<>(colSize);
|
||||
this.data.addAll(Collections.nCopies(this.colSize, null));
|
||||
}
|
||||
|
||||
|
@ -53,7 +53,7 @@ public class TSDBResultSetRowData {
|
|||
|
||||
public boolean getBoolean(int col, int srcType) throws SQLException {
|
||||
Object obj = data.get(col);
|
||||
|
||||
|
||||
switch(srcType) {
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BOOL: return (Boolean) obj;
|
||||
case TSDBConstants.TSDB_DATA_TYPE_FLOAT: return ((Float) obj) == 1.0? Boolean.TRUE:Boolean.FALSE;
|
||||
|
@ -64,10 +64,10 @@ public class TSDBResultSetRowData {
|
|||
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return ((Long) obj) == 1L? Boolean.TRUE:Boolean.FALSE;
|
||||
}
|
||||
|
||||
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
|
||||
|
||||
public void setByte(int col, byte value) {
|
||||
data.set(col, value);
|
||||
}
|
||||
|
@ -82,20 +82,20 @@ public class TSDBResultSetRowData {
|
|||
|
||||
public int getInt(int col, int srcType) throws SQLException {
|
||||
Object obj = data.get(col);
|
||||
|
||||
|
||||
switch(srcType) {
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BOOL: return Boolean.TRUE.equals(obj)? 1:0;
|
||||
case TSDBConstants.TSDB_DATA_TYPE_FLOAT: return ((Float) obj).intValue();
|
||||
case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: return ((Double)obj).intValue();
|
||||
case TSDBConstants.TSDB_DATA_TYPE_TINYINT: return (Byte) obj;
|
||||
case TSDBConstants.TSDB_DATA_TYPE_SMALLINT:return (Short) obj;
|
||||
case TSDBConstants.TSDB_DATA_TYPE_INT: return (Integer) obj;
|
||||
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return ((Long) obj).intValue();
|
||||
case TSDBConstants.TSDB_DATA_TYPE_NCHAR:
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BINARY: return Integer.parseInt((String) obj);
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BOOL: return Boolean.TRUE.equals(obj)? 1:0;
|
||||
case TSDBConstants.TSDB_DATA_TYPE_FLOAT: return ((Float) obj).intValue();
|
||||
case TSDBConstants.TSDB_DATA_TYPE_DOUBLE: return ((Double)obj).intValue();
|
||||
case TSDBConstants.TSDB_DATA_TYPE_TINYINT: return (Byte) obj;
|
||||
case TSDBConstants.TSDB_DATA_TYPE_SMALLINT:return (Short) obj;
|
||||
case TSDBConstants.TSDB_DATA_TYPE_INT: return (Integer) obj;
|
||||
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return ((Long) obj).intValue();
|
||||
case TSDBConstants.TSDB_DATA_TYPE_NCHAR:
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BINARY: return Integer.parseInt((String) obj);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -105,7 +105,7 @@ public class TSDBResultSetRowData {
|
|||
|
||||
public long getLong(int col, int srcType) throws SQLException {
|
||||
Object obj = data.get(col);
|
||||
|
||||
|
||||
switch(srcType) {
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BOOL: return Boolean.TRUE.equals(obj)? 1:0;
|
||||
case TSDBConstants.TSDB_DATA_TYPE_FLOAT: return ((Float) obj).longValue();
|
||||
|
@ -116,9 +116,9 @@ public class TSDBResultSetRowData {
|
|||
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return (Long) obj;
|
||||
case TSDBConstants.TSDB_DATA_TYPE_NCHAR:
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BINARY: return Long.parseLong((String) obj);
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BINARY: return Long.parseLong((String) obj);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -128,7 +128,7 @@ public class TSDBResultSetRowData {
|
|||
|
||||
public float getFloat(int col, int srcType) throws SQLException {
|
||||
Object obj = data.get(col);
|
||||
|
||||
|
||||
switch(srcType) {
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BOOL: return Boolean.TRUE.equals(obj)? 1:0;
|
||||
case TSDBConstants.TSDB_DATA_TYPE_FLOAT: return (Float) obj;
|
||||
|
@ -139,7 +139,7 @@ public class TSDBResultSetRowData {
|
|||
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return (Long) obj;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -149,7 +149,7 @@ public class TSDBResultSetRowData {
|
|||
|
||||
public double getDouble(int col, int srcType) throws SQLException {
|
||||
Object obj = data.get(col);
|
||||
|
||||
|
||||
switch(srcType) {
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BOOL: return Boolean.TRUE.equals(obj)? 1:0;
|
||||
case TSDBConstants.TSDB_DATA_TYPE_FLOAT: return (Float) obj;
|
||||
|
@ -160,7 +160,7 @@ public class TSDBResultSetRowData {
|
|||
case TSDBConstants.TSDB_DATA_TYPE_TIMESTAMP:
|
||||
case TSDBConstants.TSDB_DATA_TYPE_BIGINT: return (Long) obj;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -180,7 +180,7 @@ public class TSDBResultSetRowData {
|
|||
* The original type may not be a string type, but will be converted to by calling this method
|
||||
* @param col column index
|
||||
* @return
|
||||
* @throws SQLException
|
||||
* @throws SQLException
|
||||
*/
|
||||
public String getString(int col, int srcType) throws SQLException {
|
||||
if (srcType == TSDBConstants.TSDB_DATA_TYPE_BINARY || srcType == TSDBConstants.TSDB_DATA_TYPE_NCHAR) {
|
||||
|
|
|
@ -1,33 +0,0 @@
|
|||
import com.taosdata.jdbc.TSDBDriver;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Properties;
|
||||
|
||||
public class TestTSDBDatabaseMetaData {
|
||||
|
||||
public static void main(String[] args) throws SQLException {
|
||||
Connection connection = null;
|
||||
DatabaseMetaData dbMetaData = null;
|
||||
ResultSet resSet = null;
|
||||
try {
|
||||
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, "localhost");
|
||||
connection = DriverManager.getConnection("jdbc:TAOS://localhost:0/", properties);
|
||||
dbMetaData = connection.getMetaData();
|
||||
resSet = dbMetaData.getCatalogs();
|
||||
while(resSet.next()) {
|
||||
for (int i = 1; i <= resSet.getMetaData().getColumnCount(); i++) {
|
||||
System.out.printf("dbMetaData.getCatalogs(%d) = %s\n", i, resSet.getString(i));
|
||||
}
|
||||
}
|
||||
resSet.close();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
if (null != connection) {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@ import java.util.Properties;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class StatementTest extends BaseTest {
|
||||
public class StatementTest {
|
||||
static Connection connection = null;
|
||||
static Statement statement = null;
|
||||
static String dbName = "test";
|
||||
|
@ -26,14 +26,29 @@ public class StatementTest extends BaseTest {
|
|||
return;
|
||||
}
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host);
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
|
||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
|
||||
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties);
|
||||
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/?user=root&password=taosdata", properties);
|
||||
|
||||
statement = connection.createStatement();
|
||||
statement.executeUpdate("drop database if exists " + dbName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCase() {
|
||||
try {
|
||||
ResultSet rs = statement.executeQuery("show databases");
|
||||
ResultSetMetaData metaData = rs.getMetaData();
|
||||
while (rs.next()) {
|
||||
for (int i = 1; i <= metaData.getColumnCount(); i++) {
|
||||
System.out.print(metaData.getColumnLabel(i) + ":" + rs.getString(i) + "\t");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -54,9 +69,6 @@ public class StatementTest extends BaseTest {
|
|||
|
||||
@Test
|
||||
public void testUnsupport() {
|
||||
// if(null == resSet) {
|
||||
// return;
|
||||
// }
|
||||
TSDBStatement tsdbStatement = (TSDBStatement) statement;
|
||||
try {
|
||||
tsdbStatement.unwrap(null);
|
||||
|
@ -163,11 +175,10 @@ public class StatementTest extends BaseTest {
|
|||
@AfterClass
|
||||
public static void close() throws Exception {
|
||||
if (!statement.isClosed()) {
|
||||
statement.executeUpdate("drop database " + dbName);
|
||||
statement.executeUpdate("drop database if exists " + dbName);
|
||||
statement.close();
|
||||
connection.close();
|
||||
Thread.sleep(10);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,10 +4,7 @@ import org.junit.Assert;
|
|||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.*;
|
||||
import java.util.Properties;
|
||||
|
||||
public class TSDBDatabaseMetaDataTest {
|
||||
|
@ -642,7 +639,15 @@ public class TSDBDatabaseMetaDataTest {
|
|||
|
||||
@Test
|
||||
public void getTables() throws SQLException {
|
||||
Assert.assertNull(metaData.getTables("", "", "*", null));
|
||||
ResultSet tables = metaData.getTables("log", "", null, null);
|
||||
ResultSetMetaData metaData = tables.getMetaData();
|
||||
while (tables.next()) {
|
||||
System.out.print(metaData.getColumnLabel(3) + ":" + tables.getString(3) + "\t");
|
||||
System.out.print(metaData.getColumnLabel(4) + ":" + tables.getString(4) + "\t");
|
||||
System.out.print(metaData.getColumnLabel(5) + ":" + tables.getString(5) + "\n");
|
||||
}
|
||||
System.out.println();
|
||||
Assert.assertNotNull(tables);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -652,17 +657,41 @@ public class TSDBDatabaseMetaDataTest {
|
|||
|
||||
@Test
|
||||
public void getCatalogs() throws SQLException {
|
||||
Assert.assertNotNull(metaData.getCatalogs());
|
||||
ResultSet catalogs = metaData.getCatalogs();
|
||||
ResultSetMetaData meta = catalogs.getMetaData();
|
||||
while (catalogs.next()) {
|
||||
for (int i = 1; i <= meta.getColumnCount(); i++) {
|
||||
System.out.print(meta.getColumnLabel(i) + ": " + catalogs.getString(i));
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTableTypes() throws SQLException {
|
||||
ResultSet tableTypes = metaData.getTableTypes();
|
||||
while (tableTypes.next()) {
|
||||
System.out.println(tableTypes.getString("TABLE_TYPE"));
|
||||
}
|
||||
Assert.assertNotNull(metaData.getTableTypes());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getColumns() throws SQLException {
|
||||
Assert.assertNotNull(metaData.getColumns("", "", "", ""));
|
||||
ResultSet columns = metaData.getColumns("log", "", "dn", "");
|
||||
ResultSetMetaData meta = columns.getMetaData();
|
||||
while (columns.next()) {
|
||||
System.out.print(meta.getColumnLabel(1) + ": " + columns.getString(1) + "\t");
|
||||
System.out.print(meta.getColumnLabel(3) + ": " + columns.getString(3) + "\t");
|
||||
System.out.print(meta.getColumnLabel(4) + ": " + columns.getString(4) + "\t");
|
||||
System.out.print(meta.getColumnLabel(5) + ": " + columns.getString(5) + "\t");
|
||||
System.out.print(meta.getColumnLabel(6) + ": " + columns.getString(6) + "\t");
|
||||
System.out.print(meta.getColumnLabel(7) + ": " + columns.getString(7) + "\t");
|
||||
System.out.print(meta.getColumnLabel(9) + ": " + columns.getString(9) + "\t");
|
||||
System.out.print(meta.getColumnLabel(10) + ": " + columns.getString(10) + "\t");
|
||||
System.out.print(meta.getColumnLabel(11) + ": " + columns.getString(11) + "\n");
|
||||
System.out.print(meta.getColumnLabel(12) + ": " + columns.getString(12) + "\n");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -687,7 +716,15 @@ public class TSDBDatabaseMetaDataTest {
|
|||
|
||||
@Test
|
||||
public void getPrimaryKeys() throws SQLException {
|
||||
Assert.assertNotNull(metaData.getPrimaryKeys("", "", ""));
|
||||
ResultSet rs = metaData.getPrimaryKeys("log", "", "dn1");
|
||||
while (rs.next()) {
|
||||
System.out.println("TABLE_NAME: " + rs.getString("TABLE_NAME"));
|
||||
System.out.println("COLUMN_NAME: " + rs.getString("COLUMN_NAME"));
|
||||
System.out.println("KEY_SEQ: " + rs.getString("KEY_SEQ"));
|
||||
System.out.println("PK_NAME: " + rs.getString("PK_NAME"));
|
||||
}
|
||||
|
||||
Assert.assertNotNull(rs);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -812,7 +849,12 @@ public class TSDBDatabaseMetaDataTest {
|
|||
|
||||
@Test
|
||||
public void getSuperTables() throws SQLException {
|
||||
Assert.assertNotNull(metaData.getSuperTables("", "", ""));
|
||||
ResultSet rs = metaData.getSuperTables("log", "", "dn1");
|
||||
while (rs.next()) {
|
||||
System.out.println("TABLE_NAME: " + rs.getString("TABLE_NAME"));
|
||||
System.out.println("SUPERTABLE_NAME: " + rs.getString("SUPERTABLE_NAME"));
|
||||
}
|
||||
Assert.assertNotNull(rs);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -48,7 +48,6 @@ public class JDBCDemo {
|
|||
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
||||
}
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("host", host);
|
||||
properties.setProperty("charset", "UTF-8");
|
||||
properties.setProperty("locale", "en_US.UTF-8");
|
||||
properties.setProperty("timezone", "UTC-8");
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
package com.taosdata.example;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.Properties;
|
||||
|
||||
public class JdbcRestfulDemo {
|
||||
private static final String host = "master";
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// load JDBC-restful driver
|
||||
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
|
||||
// use port 6041 in url when use JDBC-restful
|
||||
String url = "jdbc:TAOS-RS://" + host + ":6041/?user=root&password=taosdata";
|
||||
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("charset", "UTF-8");
|
||||
properties.setProperty("locale", "en_US.UTF-8");
|
||||
properties.setProperty("timezone", "UTC-8");
|
||||
|
||||
Connection conn = DriverManager.getConnection(url, properties);
|
||||
Statement stmt = conn.createStatement();
|
||||
|
||||
stmt.execute("create database if not exists restful_test");
|
||||
stmt.execute("use restful_test");
|
||||
stmt.execute("create table restful_test.weather(ts timestamp, temperature float) tags(location nchar(64))");
|
||||
stmt.executeUpdate("insert into t1 using restful_test.weather tags('北京') values(now, 18.2)");
|
||||
ResultSet rs = stmt.executeQuery("select * from restful_test.weather");
|
||||
ResultSetMetaData meta = rs.getMetaData();
|
||||
while (rs.next()) {
|
||||
for (int i = 1; i <= meta.getColumnCount(); i++) {
|
||||
System.out.print(meta.getColumnLabel(i) + ": " + rs.getString(i) + "\t");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
stmt.close();
|
||||
conn.close();
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -67,9 +67,9 @@
|
|||
<dependency>
|
||||
<groupId>com.taosdata.jdbc</groupId>
|
||||
<artifactId>taos-jdbcdriver</artifactId>
|
||||
<version>2.0.15</version>
|
||||
<scope>system</scope>
|
||||
<systemPath>${project.basedir}/src/main/resources/lib/taos-jdbcdriver-2.0.15-dist.jar</systemPath>
|
||||
<version>2.0.16</version>
|
||||
<!-- <scope>system</scope>-->
|
||||
<!-- <systemPath>${project.basedir}/src/main/resources/lib/taos-jdbcdriver-2.0.15-dist.jar</systemPath>-->
|
||||
</dependency>
|
||||
<!-- fastjson -->
|
||||
<dependency>
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue