任何人都可以帮我解决这个错误:我尝试了不同的方法来解决这个错误,但无法取得成功。
这是一个包和过程以及一个java文件,我从中调用该过程。 我试图第一次调用程序。我以前从未使用过程 所以,如果有人能够让我知道什么是错的将被赞赏。
package.sql
create or replace package proj2 as
procedure show_students;
procedure show_courses;
end;
/
procedures.sql
set serveroutput on;
create or replace package body proj2 as
procedure show_courses is
cursor co1 is
select * from courses;
co1_rec co1%rowtype;
begin
if(not co1%isopen)then
open co1;
end if;
fetch co1 into co1_rec;
while co1%found loop
dbms_output.put_line(co1_rec.dept_code||','||co1_rec.course#||','||co1_rec.title);
fetch co1 into co1_rec;
end loop;
close co1;
end show_courses;
end;
mydemo2.java
import java.sql.*;
import oracle.jdbc.*;
import java.math.*;
import java.io.*;
import java.awt.*;
import oracle.jdbc.pool.OracleDataSource;
public class mydemo2 {
public static void main (String args []) throws SQLException {
try
{
//Connection to Oracle server
OracleDataSource ds = new oracle.jdbc.pool.OracleDataSource();
ds.setURL("jdbc:oracle:thin:@grouchoIII.cc.binghamton.edu:1521:ACAD111");
Connection conn = ds.getConnection("uname", "password");
OracleResultSet rs = null;
// CallableStatement cs = conn.prepareCall("{call proj2.show_students}");
OracleCallableStatement cs = (OracleCallableStatement)conn.prepareCall("{call proj2.show_courses}");
cs.registerOutParameter(1, OracleTypes.CURSOR);
cs.executeQuery();
rs = (OracleResultSet)cs.getObject(1);
// System.out.println("hi");
while(rs.next())
{
// String dept_code = rs.getString("dept_code");
// int course_no = rs.getInt("course#");
// String title = rs.getString("title");
// System.out.format(dept_code+","+course_no+","+title);
// System.out.println("success");
System.out.println(rs.getString(1)); }
//close the result set, statement, and the connection
rs.close();
cs.close();
conn.close();
}
catch (SQLException ex) { System.out.println ("\n*** SQLException caught ***\n" + ex.getMessage());}
catch (Exception e) {System.out.println ("\n*** other Exception caught ***\n");}
}
}
javac mydemo2.java
java mydemo2
*** SQLException caught ***
Invalid column index
答案 0 :(得分:0)
您正在调用一个不返回任何游标的存储过程,但您的java代码假定过程调用正在打开游标以进行迭代。另请注意,DBMS_OUTPUT在服务器上缓冲,通常不会通过JDBC调用看到。以下是您要执行的操作示例http://www.mkyong.com/jdbc/jdbc-callablestatement-stored-procedure-cursor-example/