为了在Sybase ASE中查询数据库元数据,我发现这个相关的答案(不是公认的答案)比较理想:

From a Sybase Database, how I can get table description ( field names and types)?

不幸的是,我似乎找不到任何文档,应该如何从JDBC调用sp_help。根据the documentationsp_help返回几个游标/结果集。第一个包含有关表本身的信息,第二个包含有关列的信息,等等。当我这样做时:

PreparedStatement stmt = getConnection().prepareStatement("sp_help 't_language'");
ResultSet rs = stmt.executeQuery();

while (rs.next()) {
    System.out.println(rs.getObject(1));
    // ...
}

我只从第一个光标得到结果。如何访问其他?

最佳答案

如果有多个结果集,则需要使用execute()方法而不是executeQuery()。
Here's an example:

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

10-07 19:21
查看更多