我正在尝试为我的学校项目设置微服务。我正在使用java spring boot框架。但是当我编译并运行数据库连接时,它给了我错误: java.lang.ClassCastException: class com.mysql.cj.jdbc.ConnectionImpl 无法转换为 class com.hrms.employees.db.Connection (com.mysql. cj.jdbc.ConnectionImpl 和 com.hrms.employees.db.Connection 位于加载程序“app”的未命名模块中)
package com.hrms.employees.db;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connection {
private static Connection conn;
static{
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection("jdbc:mysql://itsatest2.c45g5gg8tx2m.ap-southeast-1.rds.amazonaws.com","admin","zx55774463");
System.out.println("connected");
}
catch(ClassNotFoundException | SQLException e){
System.out.println(e.getMessage());
}
}
public static Connection getConnection(){
return conn;
}
}
Grade.build 中的依赖项
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.15'
}
我期望输出字符串为“connected”
有只小跳蛙
相关分类