Search code examples
javajdbccursor

Read a reference cursor which returns a column of string values


I am trying to read ref cursors, which returns only one column, a list of string data.

How can I do it java? Do I have to iterate through each resultset or is there any way so that I could get the entire column in one go. I have 5 ref cursor from the procedure.

rs1= (ResultSet) callableStatement.getObject(1);
rs2= (ResultSet) callableStatement.getObject(2);
rs3= (ResultSet) callableStatement.getObject(3);


while(rs1.next()){
      list1.add(rs1.getString(1));
  }

while(rs2.next()){
      list2.add(rs2.getString(1));
  }

while(rs3.next()){
      list3.add(rs3.getString(1));
  }

Solution

  • May this can help :

            ResultSet result=null ;
            PreparedStatement pstmt = null;
            ArrayList yourlist = new ArrayList();
            try
            {
                String queryString = "select yourfield from ....";
                 result = pstmt.executeQuery();
    
                while(result.next())
                {
                    yourlist.add(result.getInt("yourfield")); //if you are returning varchar so use getString
    
                }
    
            }    
            catch (SQLException e){
                System.out.println("Exception: " + e.toString() );
                        }
    
            finally
            {
                if(result!=null)
                    result.close();
                if(pstmt!=null)
                    pstmt.close();
            }