You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
package com.gym.test ;
import java.sql.Connection ;
import java.sql.DriverManager ;
public class TestMySQL {
public static void main ( String [ ] args ) {
String url = "jdbc:mysql://localhost:3306/gym_db" ;
String user = "root" ;
String password = "123456" ; // 您的密码
System . out . println ( "测试MySQL连接..." ) ;
try {
// 加载驱动
Class . forName ( "com.mysql.cj.jdbc.Driver" ) ;
System . out . println ( "✅ MySQL驱动加载成功" ) ;
// 测试连接
Connection conn = DriverManager . getConnection ( url , user , password ) ;
System . out . println ( "✅ MySQL连接成功! " ) ;
System . out . println ( " 数据库: " + conn . getMetaData ( ) . getDatabaseProductName ( ) ) ;
System . out . println ( " 版本: " + conn . getMetaData ( ) . getDatabaseProductVersion ( ) ) ;
conn . close ( ) ;
} catch ( ClassNotFoundException e ) {
System . err . println ( "❌ MySQL驱动未找到" ) ;
System . err . println ( " 请检查pom.xml中的mysql-connector-j依赖" ) ;
} catch ( Exception e ) {
System . err . println ( "❌ MySQL连接失败: " + e . getMessage ( ) ) ;
System . err . println ( " 请检查:" ) ;
System . err . println ( " 1. MySQL服务是否启动" ) ;
System . err . println ( " 2. 用户名密码是否正确" ) ;
System . err . println ( " 3. gym_db数据库是否存在" ) ;
}
}
}