我是一名学生,也是Java和存储过程的新手。 我正在尝试编写一个存储过程,它应该将表行作为java对象返回。
我有这张桌子: USER_TABLE(USERID,USERNAME,DOB)
&安培;程序是:
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;
在Java中我试图调用此过程并将对象检索为: -
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.
程序是否错误执行或我遗失了什么? 任何帮助真的很感激!! 感谢。
答案 0 :(得分:0)
我认为您希望通过传递用户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);
...............................
}
答案 1 :(得分:0)
你可能不需要那条线:
callableStatement.registerOutParameter(1, OracleTypes.CURSOR);
我不是甲骨文家伙,但我会想到:
create or replace procedure "USER_OUT" (pUSERID in varchar2)
is
begin
select USERID, USERNAME, DOB from user_table
where USERID = pUSERID;
end
和
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"));