Java调用MySQL存储过程并获得返回值的方法

时间:2022-02-09 02:37:52

本文实例讲述了Java调用MySQL存储过程并获得返回值的方法。分享给大家供大家参考。具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void empsInDept(Connection myConnect, int deptId) throws SQLException {
 CallableStatement cStmt = myConnect.prepareCall("{CALL sp_emps_in_dept(?)}");
 cStmt.setInt(1, deptId);
 cStmt.execute();
 ResultSet rs1 = cStmt.getResultSet();
 while (rs1.next()) {
    System.out.println(rs1.getString("department_name") + " " + rs1.getString("location"));
 }
 rs1.close();
 /* process second result set */
 if (cStmt.getMoreResults()) {
    ResultSet rs2 = cStmt.getResultSet();
    while (rs2.next()) {
    System.out.println(rs2.getInt(1) + " " + rs2.getString(2) + " " + rs2.getString(3));
    }
    rs2.close();
 }
 cStmt.close();
}

希望本文所述对大家的java程序设计有所帮助。