2016-10-02 44 views
0

連接文件

InitialContext initialContext = new InitialContext(); 
Context context = (Context) initialContext.lookup("java:comp/env"); 

//The JDBC Data source that we just created 
DataSource ds = (DataSource) context.lookup("connpool"); 
this.con = ds.getConnection(); 
this.des=ArrayDescriptor.createDescriptor("ARRAY_INT",this.con); 

System.out.println("in set array1"); 
this.arr_to_pass=new ARRAY(this.des,this.con,arr); 
this.csmt.setArray(index, this.arr_to_pass); 

,但我得到了以下異常

的java.lang。 ClassCastException異常:org.apache.tomcat.dbcp.dbcp2.PoolingDataSource $ PoolGuardConnectionWrapper不能轉換到oracle.jdbc.OracleConnection

+0

我遇到類似的問題,你可以把你的這.con類以及如何定義您的jndi數據源(java:comp/env)? – elcadro

回答

0

我猜ClassCastException在這條線

this.con = ds.getConnection(); 

您的代碼不顯示的this.con類型拋出,但它必須是一個OracleConnection。你不能那樣做,因爲連接是一個包裝的dbcp連接。

如果您想使用Oracle Connection,則必須先使用BasicDataSource.unwrap(Class<T> iface)解包。您也可以在使用前BasicDataSource.isWrapperFor(Class<?> iface),要檢查包裝的連接是Oracle連接類型,以避免鑄造例外:)

例如:

if (ds.getConnection().isWrapperFor(OracleConnection.class)) { 
    this.con = ds.getConnection().unwrap(OracleConnection.class); 
} 
+0

this.con類型是一個連接,它不是一個Oracle連接,我試過你的解決方案,但它提供了異常..javax.naming.NoInitialContextException:需要在環境或系統屬性中指定類名稱,或者作爲小程序參數,或者在應用程序資源文件中:java.naming.factory.initial,但它不會給我的原始代碼異常 – ParthKansara

相關問題