繁体   English   中英

使用JDBC从MSSQL服务器检索查询执行计划文本

[英]Retrieve query execution plan text from a MSSQL server using JDBC

我有以下仅返回语句本身的Java方法。 如何使用JDBC检索MSSQL查询执行计划文本?

 public String explainStatementMssql(String sqlStatement) {
    Connection connection = null;
    Statement statement = null;
    ResultSet rs = null;
    StringBuilder output = new StringBuilder();
    try {
      connection = Utilities.getConnection();
      connection.setAutoCommit(false);
      statement = connection.createStatement();
      statement.execute("SET SHOWPLAN_TEXT ON");
      statement.executeQuery(sqlStatement);
      rs = statement.getResultSet();
      while (rs.next()) {
        output.append(rs.getString(1)).append("\n");
      }
      statement.execute("SET SHOWPLAN_TEXT OFF");
      connection.commit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      Utilities.close(rs, statement, connection);
    }
    return output.toString();
  }

环境: java -version

java version "1.7.0_101"
OpenJDK Runtime Environment (IcedTea 2.6.6) (7u101-2.6.6-0ubuntu0.14.04.1)
OpenJDK 64-Bit Server VM (build 24.95-b01, mixed mode)
jtds-1.2.6.jar
Microsoft SQL Server 2012

您是否看过以下文章中的第一个示例: 在SQL Server上使用jdbc PreparedStatement获取查询计划

它没有使用jtds,但是我敢肯定,较小的调整应该可以为您提供所需的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM