前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >cmd查看mysql版本号_mysql当前版本

cmd查看mysql版本号_mysql当前版本

作者头像
全栈程序员站长
发布2022-10-05 09:48:57
4.5K0
发布2022-10-05 09:48:57
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

本文介绍如何在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。

MySQL 8.0。

注意事项

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

不带重试的JDBC连接示例

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

Connection connection = null;

Statement statement = null;

ResultSet rs = null;

try {

Class.forName(“com.mysql.jdbc.Driver”);

//adb_url是AnalyticDB for MySQL集群的连接地址URL,可以在控制台的集群信息页面获取连接URL,3306是端口号。

//db_name是AnalyticDB for MySQL集群中的数据库名称。

String url = “jdbc:mysql://adb_url:3306/db_name?useUnicode=true&characterEncoding=UTF-8”;

Properties connectionProps = new Properties();

//account_name是AnalyticDB for MySQL集群中的用户账号:高权限账号或者普通账号。

connectionProps.put(“user”, “account_name”);

//account_password是AnalyticDB for MySQL集群中用户账号对应的密码。

connectionProps.put(“password”, “account_password”);

connection = DriverManager.getConnection(url, connectionProps);

statement = connection.createStatement();

String query = “select count(*) from information_schema.tables”;

rs = statement.executeQuery(query);

while (rs.next()) {

System.out.println(rs.getObject(1));

}

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

} finally {

if (rs != null) {

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (statement != null) {

try {

statement.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (connection != null) {

try {

connection.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

带重试的JDBC连接示例

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

public static final int MAX_QUERY_RETRY_TIMES = 3;

public static Connection conn = null;

public static Statement statement = null;

public static ResultSet rs = null;

public static void main(String[] args) throws ClassNotFoundException {

//AnalyticDB for MySQL集群中的数据库名称。

String yourDB = “db_name”;

//AnalyticDB for MySQL集群中的用户账号:高权限账号或者普通账号。

String username = “account_name”;

//AnalyticDB for MySQL集群中用户账号对应的密码。

String password = “account_password”;

Class.forName(“com.mysql.jdbc.Driver”);

//adb_url是AnalyticDB for MySQL集群的连接地址URL,可以在控制台的集群信息页面获取连接URL,3306是端口号。

String url = “jdbc:mysql://adb_url:3306/” + yourDB + “?useUnicode=true&characterEncoding=UTF-8”;

Properties connectionProps = new Properties();

connectionProps.put(“user”, username);

connectionProps.put(“password”, password);

String query = “select id from test4dmp.test limit 10”;

int retryTimes = 0;

// 通过循环自动重试。

while (retryTimes < MAX_QUERY_RETRY_TIMES) {

try {

getConn(url, connectionProps);

execQuery(query);//执行query。

break; // query执行成功后,结束整个循环。

} catch (SQLException e) {

System.out.println(“Met SQL exception: ” + e.getMessage() + “, then go to retry task …”);

try {

if (conn == null || conn.isClosed()) {

retryTimes++;

}

} catch (SQLException e1) {

if (conn != null) {

try {

conn.close();

} catch (SQLException e2) {

e.printStackTrace();

}

}

}

}

}

// Clear connection resource.

closeResource();

}

/**

* Get connection.

*

* @param url

* @param connectionProps

* @throws SQLException

*/

public static void getConn(String url, Properties connectionProps) throws SQLException {

conn = DriverManager.getConnection(url, connectionProps);

}

/**

* Query task execution logic.

*

* @param sql

* @throws SQLException

*/

public static void execQuery(String sql) throws SQLException {

Statement statement = null;

ResultSet rs = null;

statement = conn.createStatement();

for (int i = 0; i < 10; i++) {

long startTs = System.currentTimeMillis();

rs = statement.executeQuery(sql);

int cnt = 0;

while (rs.next()) {

cnt++;

System.out.println(rs.getObject(1) + ” “);

}

long endTs = System.currentTimeMillis();

System.out.println(“Elapse Time: ” + (endTs – startTs));

System.out.println(“Row count: ” + cnt);

try {

Thread.sleep(160000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

/**

* Close connection resource.

*/

public static void closeResource() {

if (rs != null) {

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (statement != null) {

try {

statement.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (conn != null) {

try {

conn.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月14日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档