Oracle CURSOR data type handling in Java

Last week a colleague posed me a question about database access from Java and handling of output. Question went something like this –

we have to call an Oracle procedure from our Java code and handle the output of that procedure which is a CURSOR back in Java.

Here is the example which I shared –

 

   1:  CallableStatement callableStatement = connection.prepareCall("CALL MYPROC(?,?,?)");
   2:  callableStatement.setString(1,var1);
   3:  callableStatement.setString(2,var2);
   4:  callableStatement.registerOutParameter(3,OracleTypes.CURSOR);
   5:  callableStatement.execute();
   6:  resultSet = (ResultSet) callableStatement.getObject(3);
   7:   
   8:   
   9:  while(resultSet.next()){
  10:  var3 = resultSet.getString("abc");
  11:  var4 = resultSet.getString("xyz");
  12:  }

 

Might be helpful for someone else looking out for the same.