I have a stored procedure in SQL which can run in SQL without problems. But when I use Java to call it, the code can run but stored procedure cannot be called. Here's my code:
public int countPV(int value){
Connection conn = null;
ResultSet rs = null;
try {
conn = MyConection.getConnection();
CallableStatement cstmt = conn.prepareCall("{call countPV(?)}");
cstmt.setInt(1, value);
} catch (Exception e) {
e.printStackTrace();
return 0;
}finally{
try {
MyConection.closeConnection(conn, null, rs);
} catch (Exception e) {
}
}
return 1;
}
And here is my stored procedure in SQL:
CREATE procedure countPV (@pID int)
as
begin
WHILE (2>1)
BEGIN
update tblUser
set countx = countx + 1
where ID = @pID
SET @pID = (select parentID from tblUser where ID = @pID)
if(@pID = (select parentID from tblUser where ID = @pID))
break;
END
end
You need to call CallableStatement#execute()
in your method.
CallableStatement cstmt = conn.prepareCall("{call countPV(?)}");
cstmt.setInt(1, value);
cstmt.execute(); // MISSING!
https://docs.oracle.com/javase/7/docs/api/java/sql/CallableStatement.html
Since CallableStatemnt is extending PreparedStatement,it inherited it's executing methods (execute, executeQuery, executeUpdate) to be executed like normal quires.
I Would like to recommend using setQueryTimeout
for any stored procedure calls to avoid timeout issues.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.