SQLException caught Invalid column index

1.5k views Asked by At

Can anyone help me to solve this error: I tried different way to solve this error but could not get success.

Here is a package and procedure and a java file from which i am calling the procedure. I am trying to call procedure for the first time. I have never use procedures before so If someone can give me an idea what's wrong would be appreciated.

  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  
1

There are 1 answers

4
OldProgrammer On

You are calling a stored procedure that does not return any cursor, but your java code is assuming that the procedure call is opening a cursor to iterate through. Also note that DBMS_OUTPUT is buffered on the server, and is not normally seen through a JDBC call. Here is an example for what you are trying to do http://www.mkyong.com/jdbc/jdbc-callablestatement-stored-procedure-cursor-example/