1

I'm a student and new to Java and stored procedure. I'm trying to write a stored procedure which is supposed to return a table row as java object.

I've this table: USER_TABLE(USERID, USERNAME, DOB)

& Procedure is :

 create or replace procedure "USER_OUT" (USERID in varchar2,UserObj out User)
 is 
 begin
 select * from user_table where 
 USERID = UserObj.USERID;

 end USER_OUT;

In Java I'm trying to call this procedure and retrieve the object as :-

CallableStatement callableStatement = dh.con.prepareCall("{call USER_OUT(?)}");
User usr = new User();
callableStatement .setInt (1,"123");
usr1 = (User)callableStatement.getResultSet();
callableStatement.registerOutParameter(1, OracleTypes.CURSOR);
callableStatement.executeUpdate();//getting exception as Object is invalid.

Is the procedure wrongly implemented or am I missing something ? Any help is really appreciated!! Thanks.

zy31
  • 47
  • 6

2 Answers2

0

I think you suppose to trying to get a value by passing user id.

CallableStatement callableStatement = dh.con.prepareCall("{call USER_OUT(?,?)}");
callableStatement.setString(1, "123");
callableStatement.setString(2, OracleTypes.CURSOR);//use OracleTypes.CURSOR
callableStatement.executeUpdate();//execute USER_OUT store procedure
//read the OUT parameter now
// get cursor and cast it to ResultSet
ResultSet rs = (ResultSet) callableStatement.getObject(2);
while (rs.next()) {
String userid = rs.getString(1);
...............................
}
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
0

You probably don't need that line:

callableStatement.registerOutParameter(1, OracleTypes.CURSOR);

I am not an Oracle guys, but I would think of something like:

create or replace procedure "USER_OUT" (pUSERID in varchar2)
is 
begin
select USERID, USERNAME, DOB from user_table
where USERID = pUSERID;
end

and

CallableStatement callableStatement = dh.con.prepareCall("{call USER_OUT(?)}");
callableStatement.setString("pUSERID","123");

ResultSet rs = callableStatement.executeQuery();
rs.next();
// Assuming you have a constructor that takes all parameter in
User usr = new User(rs.getInt("USERID"), rs.getString("USERNAME"), rs.getString("DOB"));
HLP
  • 2,142
  • 5
  • 19
  • 20