Skip to main content

Connect with Java

Add Dependency

pom.xml
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>3.2.9</version>
</dependency>

view source code

Config

Run this command in your terminal to save the JDBC URL as variable, or if it is a Spring application, you can use the Spring configuration:

export TDENGINE_JDBC_URL="<jdbcURL>"
IMPORTANT

Replace <jdbcURL> with real JDBC URL, it will seems like: jdbc:TAOS-RS://example.com?useSSL=true&token=xxxx.

To obtain the value of JDBC URL, please log in TDengine Cloud and click "Programming" on the left menu, then select "Java".

Connect

Code bellow get JDBC URL from environment variables first and then create a Connection object, which is a standard JDBC Connection object.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;


public class ConnectCloudExample {
public static void main(String[] args) throws SQLException {
String jdbcUrl = System.getenv("TDENGINE_JDBC_URL");
System.out.println(jdbcUrl);
try(Connection conn = DriverManager.getConnection(jdbcUrl)) {
try(Statement stmt = conn.createStatement()) {
stmt.executeQuery("select server_version()");
}
}
}
}

view source code

The client connection is then established. For how to write data and query data, please refer to Insert and Query.

For more details about how to write or query data via REST API, please check REST API.