简体   繁体   English

java.lang.ClassCastException: class com.mysql.cj.jdbc.ConnectionImpl cannot be cast to

[英]java.lang.ClassCastException: class com.mysql.cj.jdbc.ConnectionImpl cannot be cast to

I am trying to set up micro service for my school project.我正在尝试为我的学校项目设置微服务。 I am using java spring boot framework.我正在使用 java spring 引导框架。 But when i compile and run the DB connnection it gave me the error: java.lang.ClassCastException: class com.mysql.cj.jdbc.ConnectionImpl cannot be cast to class com.hrms.employees.db.Connection (com.mysql.cj.jdbc.ConnectionImpl and com.hrms.employees.db.Connection are in unnamed module of loader 'app') But when i compile and run the DB connnection it gave me the error: java.lang.ClassCastException: class com.mysql.cj.jdbc.ConnectionImpl cannot be cast to 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;
    }

}

dependency in grade.build 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'
}

I expect the output string of "connected"我希望 output 字符串“已连接”

The problem is that your class is called Connection , while DriverManager.getConnection() returns a java.sql.Connection .问题是您的 class 称为Connection ,而DriverManager.getConnection()返回java.sql.Connection When using MySQL Connector/J the actual returned implementation is com.mysql.cj.jdbc.ConnectionImpl , and this is clearly not an instance of your class com.hrms.employees.db.Connection , so the cast fails. When using MySQL Connector/J the actual returned implementation is com.mysql.cj.jdbc.ConnectionImpl , and this is clearly not an instance of your class com.hrms.employees.db.Connection , so the cast fails.

You either need to rename your class, or you need to define your variable as private static java.sql.Connection conn and remove the cast (so use conn = DriverManager.getConnection ). You either need to rename your class, or you need to define your variable as private static java.sql.Connection conn and remove the cast (so use conn = DriverManager.getConnection ).

As an aside, using a static variable for a database connection is almost always the wrong solution, in long running applications it can cause problems when the connection is closed or lost, and in highly concurrent applications this leads in sharing a single connection between multiple threads which can lead to race conditions or other hard to diagnose bugs.顺便说一句,对数据库连接使用 static 变量几乎总是错误的解决方案,在长时间运行的应用程序中,当连接关闭或丢失时,它可能会导致问题,而在高度并发的应用程序中,这会导致在多个线程之间共享单个连接这可能导致竞争条件或其他难以诊断的错误。

The better solution would be to use a connection pooling data source like Apache DBCP, c3p0 or HikariCP, and get a connection only for a unit of work, and close it afterwards (which returns it to the pool for reuse).更好的解决方案是使用像 Apache DBCP、c3p0 或 HikariCP 这样的连接池数据源,并仅为一个工作单元获取连接,然后将其关闭(将其返回池以供重用)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:com.mysql.jdbc.JDBC4ResultSet无法转换为com.mysql.jdbc.ResultSet - java.lang.ClassCastException: com.mysql.jdbc.JDBC4ResultSet cannot be cast to com.mysql.jdbc.ResultSet java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap 无法转换为模型类 android - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to model class android java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to pojo.class - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to pojo.class java.lang.ClassCastException:无法将com.ibm.WsnOptimizedNaming._NamingContextStub类转换为接口 - java.lang.ClassCastException: cannot cast class com.ibm.WsnOptimizedNaming._NamingContextStub to interface java.lang.ClassCastException(无法将类转换为同一类) - java.lang.ClassCastException (Cannot cast class to same class) java.lang.ClassCastException:CLASS/Activity 无法转换为 MainActivity - java.lang.ClassCastException: CLASS/Activity cannot be cast to MainActivity java.lang.ClassCastException:oracle.jdbc.driver.T4CConnection无法转换为com.arjuna.ats.internal.arjuna.recovery.Connection - java.lang.ClassCastException: oracle.jdbc.driver.T4CConnection cannot be cast to com.arjuna.ats.internal.arjuna.recovery.Connection java.lang.NullPointerException at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:3005) - java.lang.NullPointerException at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:3005) java.lang.ClassCastException:MainActivity无法强制转换? - java.lang.ClassCastException: MainActivity cannot be cast? java.lang.ClassCastException,DeepNodeListImpl无法强制转换 - java.lang.ClassCastException, DeepNodeListImpl cannot be cast
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM