Java调用Oracle存储过程的问题

05-05-13 rabbitbug

存储过程有一个数组参数

Table of Varchar2(20)

在Java调用该存储过程时

把用什么数据类型传进来呢?

我写了下面的程序

String sql =

"{?=call PckgStudSltCourse.addStudPreSltCourse(?,?,?,?)}";

Connection con = null;

CallableStatement cs = null;

ResultSet rs = null;

int rets[] ;

int count = 0;

try

{

con = getConnection(dataSource);

cs = con.prepareCall(sql);

cs.registerOutParameter(1,java.sql.Types.INTEGER);

cs.setString(2,years);

cs.setString(3,termID);

cs.setString(4,studentID);

//cs.setObject(5,courseIDs,java.sql.Types.ARRAY); //用这句也抛出ClassCastException

oracle.sql.ArrayDescriptor descriptor = oracle.sql.ArrayDescriptor.createDescriptor("TYPEVARCHARARRAY",(oracle.jdbc.OracleConnection)con); //就是这句抛出了异常

oracle.sql.ARRAY array = new oracle.sql.ARRAY(descriptor,con,courseIDs);

((oracle.jdbc.OracleCallableStatement)cs).setArray(5,array);

cs.execute();

count = cs.getInt(1);

}

catch (Exception ex)

{

String errorMessage = "增加数据时出错";

System.out.println(errorMessage);

ex.printStackTrace();

return -1;

}

下面是抛出的异常

INFO [STDOUT] java.lang.ClassCastException

INFO [STDOUT] at com.saintbo.javabean.db.SltCourse.addStudPreCourseCourse(SltCourse.java:2105)

INFO [STDOUT] at com.saintbo.servlet.sltcourse.AddStudPreCourse.handle(AddStudPreCourse.java:75)

INFO [STDOUT] at com.saintbo.servlet.MyExtendedServlet.service(MyExtendedServlet.java:75)

INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)

INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)

INFO [STDOUT] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)

INFO [STDOUT] at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)

INFO [STDOUT] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)

INFO [STDOUT] at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)

INFO [STDOUT] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)

INFO [STDOUT] at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)

INFO [STDOUT] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:72)

INFO [STDOUT] at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)

INFO [STDOUT] at org.jboss.web.tomcat.security.JBossSecurityMgrRealm.invoke(JBossSecurityMgrRealm.java:275)

INFO [STDOUT] at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)

INFO [STDOUT] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)

INFO [STDOUT] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)

INFO [STDOUT] at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)

INFO [STDOUT] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)

INFO [STDOUT] at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)

INFO [STDOUT] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)

INFO [STDOUT] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

INFO [STDOUT] at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)

INFO [STDOUT] at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)

INFO [STDOUT] at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)

INFO [STDOUT] at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)

INFO [STDOUT] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)

INFO [STDOUT] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)

INFO [STDOUT] at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)

INFO [STDOUT] at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)

INFO [STDOUT] at java.lang.Thread.run(Thread.java:536)

cswei1021
2005-05-13 16:27
//cs.setObject(5,courseIDs,java.sql.Types.ARRAY);

这句话中你使用了java.sql.Types,但是对于Oracle对象而言,你应该使用Oracle特定的Types:oracle.jdbc.OracleTypes。

里边有oracle.jdbc.OracleTypes.ARRAY

rabbitbug
2005-05-17 16:55
其实我也用过oracle.jdbc.OracleTypes.ARRAY的

但也抛出异常

现在关键是在这句之前的

oracle.sql.ArrayDescriptor descriptor = oracle.sql.ArrayDescriptor.createDescriptor("TYPEVARCHARARRAY",(oracle.jdbc.OracleConnection)con);

这句就抛出了异常了

很是纳闷啊

不转成oracle.jdbc.OracleConnection也试过,同样的异常

> //cs.setObject(5,courseIDs,java.sql.Types.ARRAY);

> 这句话中你使用了java.sql.Types,但是对于Oracle对象而言

> 阌Ω檬褂Oracle特定的Types:oracle.jdbc.OracleTypes?>

> 里边有oracle.jdbc.OracleTypes.ARRAY

猜你喜欢