简体   繁体   中英

java.lang.ClassCastException: oracle.jdbc.driver.T4CConnection cannot be cast to com.arjuna.ats.internal.arjuna.recovery.Connection

I am trying to develop a Java EE application that connect to an Oracle database. I am using ojdbc6 jar. I am reading the database info from property file.

package com.varun.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import com.arjuna.ats.internal.arjuna.recovery.Connection;

public class DbUtil {

    private static Connection connection = null;
    public static Connection getConnection(){
        if(connection!=null)
        {
            return connection;
        }
        else
        {
            try{

                Properties prop=new Properties();
                InputStream inputStream=DbUtil.class.getClassLoader().getResourceAsStream("/db.properties");
                prop.load(inputStream);
                String driver =  prop.getProperty("driver");
                String url = prop.getProperty("url");
                String user = prop.getProperty("user");
                String password = prop.getProperty("password");
                Class.forName(driver);
                connection = (Connection) DriverManager.getConnection(url, user, password);

            }catch(ClassNotFoundException e)
            {
                 e.printStackTrace();
            }
            catch (SQLException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return connection;
        }
    }

}

I am getting an exception saying:

java.lang.ClassCastException: oracle.jdbc.driver.T4CConnection cannot be cast to com.arjuna.ats.internal.arjuna.recovery.Connection

I am not getting the root cause. I have changed the jar file as well, but still it is throwing the same error.

try to change the import from

import com.arjuna.ats.internal.arjuna.recovery.Connection;

to

import java.sql.Connection;

Fix your import for Connection . It must not be com.arjuna.ats.internal.arjuna.recovery.Connection .

DriverManager.getConnection(url, user, password) returns an instance of 'oracle.jdbc.driver.T4CConnection', and you are attempting to cast it to 'com.arjuna.ats.internal.arjuna.recovery.Connection'. This is the cause for your error.

I think the problem is with your import, check if you have this import in your code:

import com.arjuna.ats.internal.arjuna.recovery.Connection;

It should be :

import java.sql.Connection;

Take a look at This tutorial .

Problem with your import.

use java.sql.Connection.

Problem get solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM