一、原生 MySQL JDBC
此方法适用于非腾讯云 CAM 用户进行连接。
二、依赖配置 (Maven)
使用 Maven 项目时,请添加以下依赖:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version></dependency>
三、编写代码
import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class MySQLJDBCExample { // Database connection parameters private static final String URL = "jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC"; private static final String USERNAME = "your_username"; private static final String PASSWORD = "your_password"; public static void main(String[] args) { Connection connection = null; try { // Load the JDBC driver (This step can be omitted for MySQL 8.0+). Class.forName("com.mysql.cj.jdbc.Driver"); // Establish database connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); System.out.println("Successfully established database connection!"); // Performing various database operations createTable(connection); insertData(connection); queryData(connection); updateData(connection); deleteData(connection); } catch (ClassNotFoundException e) { // Handling JDBC driver not found issue } catch (SQLException e) { // Handling database operation errors } finally { // Closing database connection } } // Create table private static void createTable(Connection connection) throws SQLException { String sql = "CREATE TABLE IF NOT EXISTS users (" + "id INT, " + "name STRING, " + "age INT" + ")"; try (Statement statement = connection.createStatement()) { statement.executeUpdate(sql); System.out.println("Data Created successfully"); } } // Insert data private static void insertData(Connection connection) throws SQLException { String sql = "INSERT INTO users (id, name, age) VALUES (?, ?, ?)"; try (PreparedStatement pstmt = connection.prepareStatement(sql)) { // Insert the first record pstmt.setInt(1, 1); pstmt.setString(2, "bob"); pstmt.setInt(3, 25); pstmt.executeUpdate(); // Insert the second record pstmt.setInt(1, 2); pstmt.setString(2, "jimmy"); pstmt.setInt(3, 30); pstmt.executeUpdate(); System.out.println("Data Inserted successfully"); } } // Query data private static void queryData(Connection connection) throws SQLException { String sql = "SELECT id, name, age FROM users"; try (Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql)) { System.out.println("ID\\tname\\tage"); System.out.println("—————————————-"); while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); int age = resultSet.getInt("age"); System.out.printf("%d\\t%s\\t%d%n", id, name, age); } } } // update data private static void updateData(Connection connection) throws SQLException { String sql = "UPDATE users SET age = ? WHERE name = ?"; try (PreparedStatement pstmt = connection.prepareStatement(sql)) { pstmt.setInt(1, 26); pstmt.setString(2, "alex"); int rowsAffected = pstmt.executeUpdate(); System.out.println("Successfully updated " + rowsAffected + " records"); } } // delete data private static void deleteData(Connection connection) throws SQLException { String sql = "DELETE FROM users WHERE age > ?"; try (PreparedStatement pstmt = connection.prepareStatement(sql)) { pstmt.setInt(1, 28); int rowsAffected = pstmt.executeUpdate(); System.out.println("Successfully deleted " + rowsAffected + " records"); } } }
注意:当你的登录用户属于第三方 LDAP 认证体系时,需要根据认证方式在 URL 中配置 defaultAuthenticationPlugin 属性。
注意:要启用 SSL,请在 URL 中增加 useSSL=true&allowPublicKeyRetrieval=true。
四、TCHouse-X JDBC
TCHouse-X 专属连接器在原生功能基础上,增加了对腾讯云 CAM 密钥的认证支持。
五、驱动下载与依赖配置
| 项目 | 说明 |
| 下载地址 | tchousex-connector-j-8.4.0.jar |
| Maven 依赖 | <dependency> <groupId>com.tchousex</groupId> <artifactId>tchousex-connector-j</artifactId> <version>8.4.0</version></dependency> |
六、连接方式 (URL 模板)
| 认证类型 | URL 格式 | 必须参数 |
| 腾讯云用户登录 (推荐) | jdbc:tchousex://{host}:{port}/{dbname}?camSecretId=%s&camSecretKey=%s&camToken=%s | camSecretIdcamSecretKeycamToken (临时密钥) 或留空 (持久密钥) |
| 独立用户登录 | jdbc:tchousex://{host}:{port}/{dbname}?user=%s&password=%s | userpassword |
注意:要启用 SSL,请在 URL 中增加 useSSL=true&allowPublicKeyRetrieval=true。