Connecting to a Standard HTAP Instance Through JDBC
You can connect to a standard HTAP instance through JDBC.
Precautions
Currently, HTAP instances only support the UTF-8 character set.
Prerequisites
- You are familiar with:
- Computer basics
- Java
- JDBC knowledge
- You have downloaded the official JDBC driver for MySQL or MirrorDB.
- You have created a standard HTAP instance.
- The following dependency has been added to the pom.xml file.
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency>
- You can use the following command to connect to an HTAP instance through JDBC:
jdbc:mysql://<instance_ip>:<instance_port>/<database_name>
Parameter
Description
<instance_ip>
IP address of the FE node in the HTAP instance. If a proxy is installed, use the IP address of the proxy.
<instance_port>
HTAP instance port. The default value is 3306.
<database_name>
Database name used for connecting to the instance.
Sample Code
Code example (Java code for connecting to an HTAP database):
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.sql.SQLException; public class JDBCTest { static final String IP = "*.*.*.*"; //IP address of the instance static final String USER = "***"; //Username static final String PASS = "***"; //Password public static void main(String[] args) { Connection conn = null; Statement stmt = null; String url = "jdbc:clickhouse://" + IP + ":8123"; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, USER, PASS); stmt = conn.createStatement(); String sql = "show databases;"; ResultSet rs = stmt.executeQuery(sql); int columns = rs.getMetaData().getColumnCount(); for (int i = 1; i <= columns; i++) { System.out.print(rs.getMetaData().getColumnName(i)); System.out.print("\t"); } while (rs.next()) { System.out.println(); for (int i = 1; i <= columns; i++) { System.out.print(rs.getObject(i)); System.out.print("\t"); } } rs.close(); stmt.close(); conn.close(); } catch (SQLException se) { se.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { // release resource .... } } }
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot