我从java调用存储过程,作为out参数,我给了struct
。下面的代码可以解决所有问题;
conn = DriverManager.getConnection(URL,"username","password")
StructDescriptor recDescriptor = StructDescriptor.createDescriptor("TCS_CHALLAN",conn);
CallableStatement stmt = conn.prepareCall("{ call xx_get_challan_info(?, ?) }");
Object[] return_record_array = new Object[8];
STRUCT output_oracle_record;
stmt.setObject(1, challan_no);
stmt.registerOutParameter(2, OracleTypes.STRUCT, "TCS_CHALLAN");
output_oracle_record = ((OracleCallableStatement)stmt).getSTRUCT(2);
return_record_array = output_oracle_record.getAttributes();
当我从datasource
weblogic server
获取我的连接时,会出现此问题。
javax.naming.Context initialContext = new javax.naming.InitialContext();
javax.sql.DataSource dataSource = (javax.sql.DataSource)initialContext.lookup("jdbc/testApps");
conn = dataSource.getConnection();
使用上面的conn
对象,当我尝试相同的代码时,我收到错误
weblogic.jdbc.wrapper.Struct_oracle_sql_STRUCT cannot be cast to oracle.sql.STRUCT
答案 0 :(得分:1)
这就是解决方案。在weblogic中运行代码时,您需要进行一些额外的转换。确保在您的类路径中包含jar文件。
/*Code to support Weblogic datatypes*/
Object returnObj = stmt.getObject(2);
if (returnObj instanceof Struct)
output_oracle_record = (oracle.sql.STRUCT)(((weblogic.jdbc.wrapper.Struct)returnObj).unwrap(Class.forName("oracle.sql.STRUCT")));
else
output_oracle_record = (oracle.sql.STRUCT)returnObj;
/*code for tomcat deployment*/
//output_oracle_record = ((OracleCallableStatement)stmt).getSTRUCT(2);
答案 1 :(得分:0)
2种可能的解决方案:
javax.naming.Context initialContext = new javax.naming.InitialContext();
javax.sql.DataSource dataSource =(javax.sql.DataSource)initialContext.lookup(" java:comp / env / jdbc / MyDS");
con = dataSource.getConnection();
vendorConnection =(oracle.jdbc.OracleConnection)((weblogic.jdbc.extensions.WLConnection)con).getVendorConnection();