日期:2014-05-20  浏览次数:21156 次

java调用存储过程报错
存储过程建成功了,我在用java调用的时候又报错了。
首先我的存储过程如下:
SQL code

create or replace procedure query_student   
is
v_name varchar2(20);
begin
 select name into v_name from st_t_student where id=3012;
 dbms_output.put_line(v_name);
end query_student;


我的java代码如下:
Java code

public void test(){
        conn = JDBCUtils.getConnection("miniWeb", "miniWeb");
        try {
            conn.setAutoCommit(false);
            //调用存储过程
            cstmt = conn.prepareCall("{call query_student}");
            rs = cstmt.executeQuery();
            while(rs.next()){
                System.out.println(rs.getString("v_name"));
            }
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }finally{
            JDBCUtils.closeConnection(rs, pstmt, conn);
        }
    }


我的错误信息如下:
Java code

java.sql.SQLException: ORA-00900: invalid SQL statement

    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:305)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:272)
    at oracle.jdbc.driver.T4C8Odscrarr.receive(T4C8Odscrarr.java:203)
    at oracle.jdbc.driver.T4CCallableStatement.do_describe(T4CCallableStatement.java:604)
    at oracle.jdbc.driver.OracleStatement.get_column_index(OracleStatement.java:3037)
    at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:1861)
    at oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:1559)
    at com.sinosoft.javase.ProcedureTest.test(ProcedureTest.java:36)
    at com.sinosoft.javase.ProcedureTest.main(ProcedureTest.java:53)




------解决方案--------------------
如果要返回值得

cstmt=conn.preparCall("call query_student(?,?)");
cstmt.setString(1,"aa");//第一个必须是输入的,不能是输出
cstmt=registerOutParameter(2,java.sql.Types.VARCHAR);//输出
stmt.executeUpdate();
String val=cstmt.getString(2);
------解决方案--------------------
cstmt = conn.prepareCall("{call query_student()}");
cstmt.execute();

------解决方案--------------------
11楼是对的

SQL code

create or replace procedure p_query(v_i in int,v_name out varchar2)
is
begin
 select ename into v_name from emp where empno=7900 and 1=v_i;
end p_query;

------解决方案--------------------

返回结果集

SQL code

create or replace package pa_query as
 type p_cursor is ref cursor;
end pa_query;
/
create or replace procedure p_query(v_cursor out pa_query.p_cursor)
is
begin
open v_cursor for select * from emp;
end p_query;