Use JDBC driver to access ADQM from JVM-based applications
To connect a JVM-based application to an ADQM database, you can use ClickHouse JDBC driver.
Below is an example of a Java application that is built with Maven and interacts with an ADQM server via JDBC driver. The source code of the application is stored in the <project_path>/src/main/java/io/arenadata/adqm/test/TestApp.java file, and the pom.xml file that contains information about the project structure and configuration is located in the project’s root folder — <project_path>/pom.xml.
-
In the pom.xml file, add the following dependency to download a JDBC driver from the Maven central repository:
<dependencies> <dependency> <groupId>com.clickhouse</groupId> <artifactId>clickhouse-jdbc</artifactId> <version>0.3.2</version> <classifier>all</classifier> <exclusions> <exclusion> <groupId>*</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
-
Create a Java class with the following code:
package io.arenadata.adqm.test; import com.clickhouse.jdbc.*; import java.sql.*; import java.util.*; public class TestApp { private static Connection getConnection(String url) throws SQLException { return DriverManager.getConnection(url); } public static void main(String[] args) { String url = "jdbc:clickhouse://<IP>:8123/default"; try ( Connection connection = getConnection(url); Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM <table_name> LIMIT 5")) { ResultSetMetaData rsMetaData = rs.getMetaData(); int columns = rsMetaData.getColumnCount(); while (rs.next()) { for (int c = 1; c <= columns; c++) { System.out.print(rsMetaData.getColumnName(c) + ":" + rs.getString(c) + (c < columns ? " | " : "\n")); } } } catch (SQLException e) { e.printStackTrace(); } } }
This code connects to the ADQM server using DriverManager (as the
default
user without a password) and selects the first five rows from the specified table of thedefault
database. Replace<IP>
with the IP-address of your ADQM server and<table_name>
in the query string with a name of a table existing in thedefault
database of your ADQM server. -
Specify networks from which connections to ADQM are allowed for the
default
user as described in the Connect to ADQM article. -
Compile and run the project. The result should be similar to:
a:1 | b:text1 | c:100 a:2 | b:text2 | c:200 a:3 | b:text3 | c:300