开发者

How to access this stored Procedure from JDBC Callablestatement?

How to access this stored Procedure from JDBC Callablestatement ??

public class TestOCIApp {

public static void main(String args[]) throws ClassNotFoundException,
SQLException {

try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcle", "scott", "tiger");

CallableStatement cs = conn.prepareCall("{call test(?,?)}");
cs.setInt(1, 10);

cs.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);

ResultSet rs = (ResultSet)cs.getObject(2);

while(rs.next())
{
System.out.println(rs.getString(2));
}

conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

I am getting Excepti开发者_Go百科on as

java.sql.SQLException: Invalid column index * at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:180)* * at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:222)* * at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:285)* * at oracle.jdbc.driver.OracleStatement.prepare_for_new_get(OracleStatement.java:2804)* * at oracle.jdbc.driver.OracleStatement.getObjectValue(OracleStatement.java:4983)* * at oracle.jdbc.driver.OracleStatement.getObjectValue(OracleStatement.java:4964)* * at oracle.jdbc.driver.OracleCallableStatement.getObject(OracleCallableStatement.java:586)* * at TestOCIApp.main(TestOCIApp.java:23)*

create or replace procedure test( p_deptno IN number
, p_cursor OUT SYS_REFCURSOR)
is
begin
open p_cursor FOR
select *
from emp
where deptno = p_deptno;
end test;
/


When dealign with oracle cursors,The CallableStatement object is cast to OracleCallableStatement to use the getCursor method, which is an Oracle extension to the standard JDBC application programming interface (API), and returns the REF CURSOR into a ResultSet object.

cstmt.registerOutParameter(1, OracleTypes.CURSOR);
cstmt.execute();
cursor = ((OracleCallableStatement)cstmt).getCursor(1);

while (cursor.next ()){
System.out.println (cursor.getString(1));
} 

But this will couple your code to oracle database [:(]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜