JDBC Calls stored procedures: CallableStatement calls stored procedures in Java, which is quite fixed: Class.forName (...). Connection conn = Drivermanager.getconnection (..../***p is the name of the stored procedure to invoke, 4 parameters of the stored procedure, with 4? */callablestatement cstmt = Conn.preparecall ("{call P (?,?,?,?)}") in place of the rest of the code Tell JDBC, these parameters, which are the output parameters, the type of the output parameter is specified with Java.sql.Types * The following means, the 3rd one? And the 4th one? is the output parameter, the type is the *types of the INTEGER after the specific write what type, depends on how your stored procedure parameters defined */cstmt.registeroutparameter (3, Types.integer); Cstmt.registeroutparameter (4, Types.integer);/*** in my 1th? And the 2nd one? is the input parameter, the 3rd is the output parameter, 4th both input and output * The following is set their value, the first set to 3, the second set to 4, 4th set to 5* no 3rd, because it is the output parameter */cstmt.setint (1, 2); Cstmt.setint (4); Cstmt.setint (4, 5);//execute Cstmt.execute ();//Take the value of the 3rd parameter as an int type three = Cstmt.getint (3); System.out.println (three);//Take the value of the 4th parameter as an int. int four = Cstmt.getint (4); System.out.println (four);//Use up don't forget to shut up, after opening first off Cstmt.close (); Conn.close (); JDBC calls the stored procedure, which is enough to master this one. The following is the code for the stored procedure used by the above program, I use the Oracle database, but no matter what database, for your program, the JDBC end of the writing is the same. Create or replace Procedure P (v_a in Number,v_b Number,v_ret out number,v_temp on out number) isbeginif (V_a > V_b) Thenv_ret: = V_a;elsev_ret: = v_b;end if;v_temp: = v_temp + 1;end;
Calling stored procedures with parameters in Java