【问题标题】:Calling Sybase Adaptive Server Enterprise's "sp_help" from JDBC从 JDBC 调用 Sybase Adaptive Server Enterprise 的“sp_help”
【发布时间】:2011-09-04 10:58:02
【问题描述】:

为了在 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));
    // ...
}

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

【问题讨论】:

    标签: java sql jdbc sap-ase


    【解决方案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 
    

    【讨论】:

    • 很好,我不知道那些标准的 JDBC 方法。注意:对于这种情况(使用 Sybase ASE 的 sp_help),在第 2 步之前,我似乎也必须调用 cstmt.getMoreResults()。否则,rs 将是 null。所以我不知道 DB2 的例子是错的还是 Sybase ASE 的特殊情况
    • 你也知道这个问题的答案吗? stackoverflow.com/questions/7299550/…
    【解决方案2】:

    您还需要调用 getUpdateCount() 和 getMoreResults() 来读取整个结果集。这是我用来调用 sp_helpartition 以从 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();
    }
    

    【讨论】:

    • 嗯,我为什么需要getUpdateCount()?当前接受的答案(stackoverflow.com/a/7298790/521799)似乎工作得很好......
    • 我发现如果结果由结果集和更新计数混合组成,您需要读取更新计数,否则您将无法获得完整数量的结果集。
    • 有趣!我猜我得把它改成jOOQ。非常感谢您的提示!
    【解决方案3】:

    感谢Martin Clayton's answer here,我可以弄清楚如何在一般情况下查询 Sybase ASE 的sp_help 函数。我在blog here 中记录了有关如何完成此操作的更多详细信息。我在jOOQ 中工作了对多个 JDBC 结果集的支持。对于 sp_help,使用 jOOQ API 调用该函数可能如下所示:

    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'");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-06
      • 2015-10-30
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-26
      • 1970-01-01
      相关资源
      最近更新 更多