全部产品

Java

更新时间:2019-07-02 21:37:38

本文介绍Java中如何通过MySQL JDBC连接AnalyticDB for MySQL。

MySQL JDBC驱动版本

AnalyticDB for MySQL支持以下版本的MySQL JDBC驱动。

  • 5.0版本系列:5.0.2,5.0.3,5.0.4,5.0.5,5.0.7,5.0.8。

  • 5.1版本系列:5.1.1,5.1.2,5.1.3,5.1.4,5.1.5,5.1.6,5.1.7,5.1.8,5.1.11,5.1.12,5.1.13,5.1.14,5.1.15,5.1.16,5.1.17,5.1.18,5.1.19,5.1.20,5.1.21,5.1.22,5.1.23,5.1.24,5.1.25,5.1.26,5.1.27,5.1.28,5.1.29,5.1.31, 5.1.32, 5.1.33, 5.1.34。

注意事项

Java中创建MySQL JDBC连接依赖于MySQL-JDBC驱动包,您需要手动将MySQL-JDBC驱动包(mysql-connector-java-x.x.x.jar)加入到CLASSPATH中,否则无法创建MySQL JDBC连接。

不带重试的JDBC连接示例

  1. Connection connection = null;
  2. Statement statement = null;
  3. ResultSet rs = null;
  4. try {
  5. Class.forName("com.mysql.jdbc.Driver");
  6. //adb_url是AnalyticDB for MySQL集群的连接地址URL,可以在控制台的集群信息页面获取连接URL,3306是端口号。
  7. //db_name是AnalyticDB for MySQL集群中的数据库名称。
  8. String url = "jdbc:mysql://adb_url:3306/db_name?useUnicode=true&characterEncoding=UTF-8";
  9. Properties connectionProps = new Properties();
  10. //account_name是AnalyticDB for MySQL集群中的用户账号:高权限账号或者普通账号。
  11. connectionProps.put("user", "account_name");
  12. //account_password是AnalyticDB for MySQL集群中用户账号对应的密码。
  13. connectionProps.put("password", "account_password");
  14. connection = DriverManager.getConnection(url, connectionProps);
  15. statement = connection.createStatement();
  16. String query = "select count(*) from information_schema.tables";
  17. rs = statement.executeQuery(query);
  18. while (rs.next()) {
  19. System.out.println(rs.getObject(1));
  20. }
  21. } catch (ClassNotFoundException e) {
  22. e.printStackTrace();
  23. } catch (SQLException e) {
  24. e.printStackTrace();
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. } finally {
  28. if (rs != null) {
  29. try {
  30. rs.close();
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. if (statement != null) {
  36. try {
  37. statement.close();
  38. } catch (SQLException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. if (connection != null) {
  43. try {
  44. connection.close();
  45. } catch (SQLException e) {
  46. e.printStackTrace();
  47. }
  48. }
  49. }

带重试的JDBC连接示例

在JDBC中通过配置参数可以实现连接重试机制。

  1. public static final int MAX_QUERY_RETRY_TIMES = 3;
  2. public static Connection conn = null;
  3. public static Statement statement = null;
  4. public static ResultSet rs = null;
  5. public static void main(String[] args) throws ClassNotFoundException {
  6. //AnalyticDB for MySQL集群中的数据库名称。
  7. String yourDB = "db_name";
  8. //AnalyticDB for MySQL集群中的用户账号:高权限账号或者普通账号。
  9. String username = "account_name";
  10. //AnalyticDB for MySQL集群中用户账号对应的密码。
  11. String password = "account_password";
  12. Class.forName("com.mysql.jdbc.Driver");
  13. //adb_url是AnalyticDB for MySQL集群的连接地址URL,可以在控制台的集群信息页面获取连接URL,3306是端口号。
  14. String url = "jdbc:mysql://adb_url:3306/" + yourDB + "?useUnicode=true&characterEncoding=UTF-8";
  15. Properties connectionProps = new Properties();
  16. connectionProps.put("user", username);
  17. connectionProps.put("password", password);
  18. String query = "select id from test4dmp.test limit 10";
  19. int retryTimes = 0;
  20. while (retryTimes < MAX_QUERY_RETRY_TIMES) {
  21. try {
  22. getConn(url, connectionProps);
  23. execQuery(query);
  24. break; // query执行成功后,结束整个循环。
  25. } catch (SQLException e) {
  26. System.out.println("Met SQL exception: " + e.getMessage() + ", then go to retry task ...");
  27. try {
  28. if (conn == null || conn.isClosed()) {
  29. retryTimes++;
  30. }
  31. } catch (SQLException e1) {
  32. if (conn != null) {
  33. try {
  34. conn.close();
  35. } catch (SQLException e2) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }
  40. }
  41. }
  42. // Clear connection resource.
  43. closeResource();
  44. }
  45. /**
  46. * Get connection.
  47. *
  48. * @param url
  49. * @param connectionProps
  50. * @throws SQLException
  51. */
  52. public static void getConn(String url, Properties connectionProps) throws SQLException {
  53. conn = DriverManager.getConnection(url, connectionProps);
  54. }
  55. /**
  56. * Query task execution logic.
  57. *
  58. * @param sql
  59. * @throws SQLException
  60. */
  61. public static void execQuery(String sql) throws SQLException {
  62. Statement statement = null;
  63. ResultSet rs = null;
  64. statement = conn.createStatement();
  65. for (int i = 0; i < 10; i++) {
  66. long startTs = System.currentTimeMillis();
  67. rs = statement.executeQuery(sql);
  68. int cnt = 0;
  69. while (rs.next()) {
  70. cnt++;
  71. System.out.println(rs.getObject(1) + " ");
  72. }
  73. long endTs = System.currentTimeMillis();
  74. System.out.println("Elapse Time: " + (endTs - startTs));
  75. System.out.println("Row count: " + cnt);
  76. try {
  77. Thread.sleep(160000);
  78. } catch (InterruptedException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82. }
  83. /**
  84. * Close connection resource.
  85. */
  86. public static void closeResource() {
  87. if (rs != null) {
  88. try {
  89. rs.close();
  90. } catch (SQLException e) {
  91. e.printStackTrace();
  92. }
  93. }
  94. if (statement != null) {
  95. try {
  96. statement.close();
  97. } catch (SQLException e) {
  98. e.printStackTrace();
  99. }
  100. }
  101. if (conn != null) {
  102. try {
  103. conn.close();
  104. } catch (SQLException e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. }