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.
26 lines
949 B
26 lines
949 B
package com.gym.test;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.CommandLineRunner;
|
|
import org.springframework.stereotype.Component;
|
|
import javax.sql.DataSource;
|
|
import java.sql.Connection;
|
|
|
|
@Component
|
|
public class DatabaseTest implements CommandLineRunner {
|
|
|
|
@Autowired
|
|
private DataSource dataSource;
|
|
|
|
@Override
|
|
public void run(String... args) throws Exception {
|
|
try (Connection connection = dataSource.getConnection()) {
|
|
System.out.println("✅ 数据库连接成功!");
|
|
System.out.println("数据库URL: " + connection.getMetaData().getURL());
|
|
System.out.println("数据库用户: " + connection.getMetaData().getUserName());
|
|
} catch (Exception e) {
|
|
System.out.println("❌ 数据库连接失败!");
|
|
System.out.println("错误信息: " + e.getMessage());
|
|
}
|
|
}
|
|
} |