CallableStatement cst = conn.prepareCall("{CALL getStudents(?)}"); cst.setInt(1, 100); // set input parameter ResultSet rs = cst.executeQuery(); while (rs.next()) { System.out.println(rs.getInt("id") + ", " + rs.getString("name")); }
CallableStatement cst = conn.prepareCall("{? = CALL getAverage(?, ?)}"); cst.registerOutParameter(1, Types.DOUBLE); // set output parameter cst.setInt(2, 10); // set input parameter cst.setInt(3, 20); // set input parameter cst.executeQuery(); double avg = cst.getDouble(1); // retrieve output parameter System.out.println("Average: " + avg);In this example, we prepare a CallableStatement to call the stored function "getAverage", which takes two input parameters of type INT and returns a single value of type DOUBLE. We set the input parameters to 10 and 20 and also register the output parameter as a DOUBLE. We then execute the query and retrieve the output parameter using cst.getDouble(1). These examples use the java.sql package library, which is part of the Java Standard Edition (SE) platform.