[英]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. 我正在尝试开发连接到Oracle数据库的Java EE应用程序。 I am using ojdbc6 jar. 我正在使用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 java.lang.ClassCastException:oracle.jdbc.driver.T4CConnection无法转换为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. 我也更改了jar文件,但是仍然抛出相同的错误。
try to change the import from 尝试从更改进口
import com.arjuna.ats.internal.arjuna.recovery.Connection;
to 至
import java.sql.Connection;
Fix your import for Connection
. 修复Connection
的导入。 It must not be com.arjuna.ats.internal.arjuna.recovery.Connection
. 它不能是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'. DriverManager.getConnection(url, user, password)
返回“ oracle.jdbc.driver.T4CConnection”的实例,而您尝试将其强制转换为“ 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. 使用java.sql.Connection。
Problem get solved. 问题得到解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.