The editor continues with this article . If you forget to write the sqlserver stored procedure and function, please see
the call of this stored procedure.
Before calling the SQL process, you need to pass the value to the IN and IN OUT parameters using the appropriate set method registeroutParameter to register OUT and IN OUT parameters. For example, before calling the sampleProcedure process, the following statement passes the value to the parameters p1 (IN), p2 (IN), and p3 (IN OUT), and registers the parameter p3 (IN OUT), you can use execute () or executeUpdate () Execute the process according to the type of SQL statement, and then use the get method to get the value from the OUT parameter. For example, the next statement gets the value from the parameter p3.
在这里插入代码片
CallableStatement s=dbConn.prepareCall("{call ad(?,?,?)}");//“?”占位符,作为参数
s.setInt(1, 1);
s.setInt(2, 2);
s.setInt(3, 2);
s.registerOutParameter(3,Types.INTEGER);
s.execute();
System.out.println(s.getInt(3));
dbConn.close();
call function
在这里插入代码片
CallableStatement s=dbConn.prepareCall("{?=call sum(?,?)}");
s.setInt(2, 1);
s.setInt(3, 2);
s.registerOutParameter(1,Types.INTEGER);
s.execute();
System.out.println(s.getInt(1));
dbConn.close();
In fact, calling a stored procedure is similar to calling a function, but sometimes there are returns and sometimes there are no such differences.