推特 阿里云技术文档正文

Java访问_业务系统连接分析型数据库MySQL版_访问分析型数据库MySQL版_分析型数据库MySQL版2.0_分析型数据库MySQL版

admin 阿里云技术文档 2020-02-11 202 0
阿里云服务器优惠

Java访问

本文介绍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连接示例

您可以在业务系统的Java代码中添加以下代码,通过MySQL JDBC连接AnalyticDB for MySQL数据库。

  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. //my_access_key_id为阿里云账号或者RAM子账号的AccessKeyId。
  11. connectionProps.put("user", "my_access_key_id");
  12. //my_access_key_secret为阿里云账号或者RAM子账号的AccessKeySecret。
  13. connectionProps.put("password", "my_access_key_secret");
  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 = "user_db";
  8. //my_access_key_id为阿里云账号或者RAM子账号的AccessKeyId。
  9. String username = "my_access_key_id";
  10. //my_access_key_secret为阿里云账号或者RAM子账号的AccessKeySecret。
  11. String password = "my_access_key_secret";
  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. //通过循环自动重试。
  21. while (retryTimes < MAX_QUERY_RETRY_TIMES) {
  22. try {
  23. getConn(url, connectionProps);
  24. execQuery(query);//执行query。
  25. break; //query执行成功后,结束整个循环。
  26. } catch (SQLException e) {
  27. System.out.println("Met SQL exception: " + e.getMessage() + ", then go to retry task ...");
  28. try {
  29. if (conn == null || conn.isClosed()) {
  30. retryTimes++;
  31. }
  32. } catch (SQLException e1) {
  33. if (conn != null) {
  34. try {
  35. conn.close();
  36. } catch (SQLException e2) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }
  41. }
  42. }
  43. // Clear connection resource.
  44. closeResource();
  45. }
  46. /**
  47. * Get connection.
  48. *
  49. * @param url
  50. * @param connectionProps
  51. * @throws SQLException
  52. */
  53. public static void getConn(String url, Properties connectionProps) throws SQLException {
  54. conn = DriverManager.getConnection(url, connectionProps);
  55. }
  56. /**
  57. * Query task execution logic.
  58. *
  59. * @param sql
  60. * @throws SQLException
  61. */
  62. public static void execQuery(String sql) throws SQLException {
  63. Statement statement = null;
  64. ResultSet rs = null;
  65. statement = conn.createStatement();
  66. for (int i = 0; i < 10; i++) {
  67. long startTs = System.currentTimeMillis();
  68. rs = statement.executeQuery(sql);
  69. int cnt = 0;
  70. while (rs.next()) {
  71. cnt++;
  72. System.out.println(rs.getObject(1) + " ");
  73. }
  74. long endTs = System.currentTimeMillis();
  75. System.out.println("Elapse Time: " + (endTs - startTs));
  76. System.out.println("Row count: " + cnt);
  77. try {
  78. Thread.sleep(160000);
  79. } catch (InterruptedException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. }
  84. /**
  85. * Close connection resource.
  86. */
  87. public static void closeResource() {
  88. if (rs != null) {
  89. try {
  90. rs.close();
  91. } catch (SQLException e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. if (statement != null) {
  96. try {
  97. statement.close();
  98. } catch (SQLException e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. if (conn != null) {
  103. try {
  104. conn.close();
  105. } catch (SQLException e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. }
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论

-----