Calling Sybase Adaptive Server Enterprise's “sp_help” from JDBC

爷,独闯天下 提交于 2019-12-05 23:06:34

When you have multiple result sets you need to use the execute() method rather than 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 

You also need to call getUpdateCount() as well as getMoreResults() to read the entire result set. Here is some code I used to call sp_helpartition to retrieve partition information from a SYBASE DB.

try {
    connection = getPooledConnection(poolName);
    statement = connection.createStatement();
    CallableStatement callable = connection.prepareCall(
        "{ call sp_helpartition(?) }");
    callable.setString(1,tableName);
    callable.execute();

    int partitions = 0;

    /*
     * Loop through results until there are no more result sets or
     * or update counts to read. The number of partitions is recorded
     * in the number of rows in the second result set.
     */
    for (int index = 0 ; ; index ++){
        if (callable.getMoreResults()){
            ResultSet results = callable.getResultSet();
            int count = 0 ;
            while (results.next()){
                count++;
            }
            if (index == 1){
                partitions = count;
            }
        } else if (callable.getUpdateCount() == -1){
            break ;
        }
    }
    return partitions ;
} catch (Exception e) {
    return 0 ;
} finally {
    statement.close();
    connection.close();
}
Lukas Eder

Thanks to Martin Clayton's answer here, I could figure out how to query Sybase ASE's sp_help function generically. I documented some more details about how this can be done in my blog here. I worked support for multiple JDBC result sets into jOOQ. In the case of sp_help, calling that function using the jOOQ API might look like this:

Factory create = new ASEFactory(connection);

// Get a list of tables, a list of user types, etc
List<Result<Record>> tables = create.fetchMany("sp_help");

// Get some information about the my_table table, its
// columns, keys, indexes, etc
List<Result<Record>> results = create.fetchMany("sp_help 'my_table'");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!