CallableStatement cstmt = conn.prepareCall("{call get_employee_count(?, ?)}"); cstmt.setInt(1, 10); cstmt.registerOutParameter(2, Types.INTEGER); cstmt.execute(); int count = cstmt.getInt(2);
CallableStatement cstmt = conn.prepareCall("{call get_employee(?, ?)}"); cstmt.setInt(1, 10); cstmt.registerOutParameter(2, Types.REF_CURSOR); cstmt.execute(); ResultSet rs = (ResultSet) cstmt.getObject(2); while (rs.next()) { System.out.println(rs.getString("name")); }In this example, we prepare a CallableStatement to call a stored procedure called get_employee that takes an integer parameter and returns a cursor. We set the integer parameter using the setInt method and then register the second parameter as an output parameter of SQL type REF_CURSOR using the registerOutParameter method. We then execute the statement and retrieve the output parameter using the getObject method. Since the output parameter is a cursor, we cast it to a ResultSet and then iterate over its rows to print the names of the employees. The package library for java.sql is java.sql.*.