不幸的是,我似乎找不到任何文档,应该如何sp_help从JDBC调用。根据文档,sp_help返回几个游标/结果集。第一个包含有关表本身的信息,第二个包含有关列的信息,等等。当我这样做时:
sp_help
PreparedStatement stmt = getConnection().prepareStatement("sp_help 't_language'"); ResultSet rs = stmt.executeQuery(); while (rs.next()) { System.out.println(rs.getObject(1)); // ... }
我只从第一个光标得到结果。如何访问其他?
当您有多个结果集时,需要使用execute()方法而不是executeQuery()。 这是一个例子:
CallableStatement cstmt; ResultSet rs; int i; String s; ... cstmt.execute(); // Call the stored procedure 1 rs = cstmt.getResultSet(); // Get the first result set 2 while (rs.next()) { // Position the cursor 3 i = rs.getInt(1); // Retrieve current result set value System.out.println("Value from first result set = " + i); // Print the value } cstmt.getMoreResults(); // Point to the second result set 4a // and close the first result set rs = cstmt.getResultSet(); // Get the second result set 4b while (rs.next()) { // Position the cursor 4c s = rs.getString(1); // Retrieve current result set value System.out.println("Value from second result set = " + s); // Print the value } rs.close(); // Close the result set cstmt.close(); // Close the statement