0

我的问题:我想知道为什么来自准备好的语句的查询结果每次运行都应该不一致。

警告前方奇怪的东西。

我在 Oracle 数据库上。我正在使用ojdbc6.jar驱动程序。我有一个查询,它返回大约 1300 行(它随着时间的推移而变化,因为有一个where子句取决于sysdate)。当我在 Toad(SQL Developer 工具)和 Netbeans 服务工具(与数据库的另一个接口)中运行它时,它运行良好。我的结果是一致的。但是当我在 java 代码中运行查询时,我的结果非常不一致。为了尝试查看每个结果计数返回的频率,我将其放入 for 循环并迭代 10 次。每次运行中都有相对一致的行数,但跨运行时则不然。我的输出和代码如下。让我知道是否有更多信息有帮助。

当尝试有效时(排序...):

Got real queries (1308)
Got real queries (1308)
Got real queries (1308)
Got real queries (1307)
Got real queries (1307)
Got real queries (1317)
Got real queries (1317)
Got real queries (1317)
Got real queries (1317)
Got real queries (1315)

当尝试不起作用时:

Got real queries (212)
Got real queries (212)
Got real queries (212)
Got real queries (212)
Got real queries (212)
Got real queries (212)
Got real queries (212)
Got real queries (212)
Got real queries (212)
Got real queries (212)

我的代码:

public static void main(String[] args) throws Exception {
  for (int i = 0; i < 10; i++) {
    List<String> realQueries = new ArrayList<>();
    String sqlAllRealQueries = "select V.SQL_FULLTEXT" + StringHelper.newline
            + "from v$sql v" + StringHelper.newline
            + "where V.PARSING_SCHEMA_NAME = 'DSS1_PTC'" + StringHelper.newline
            + "and to_number(replace(replace(replace(v.last_load_time,'-',''), '/',''), ':','')) > to_number(to_char(sysdate - (24/24),'yyyymmddHH24mmss'))" + StringHelper.newline
            + "order by V.LAST_LOAD_TIME desc";
    try (ResultSet rs = getInstance().executeQuery(sqlAllRealQueries)) {
      while (rs.next()) {
        realQueries.add(rs.getString(1));
      }
    }

    System.out.println("Got real queries (" + realQueries.size() + ")");
  }
  System.exit(0);
}

public ResultSet executeQuery(String connection, String query, QueryParameter... params) throws DataException, SQLException {
  Connection conn = ConnectionPool.getInstance().get(connection);
  PreparedStatement pstmt = conn.prepareStatement(query);
  for (int i = 1; i <= params.length; i++) {
    //... stuff to fill in the '?'s of the query (in this case, there are none).
    }
  }

  ResultSet rs = pstmt.executeQuery();
  ConnectionPool.getInstance().release(conn);
  return rs;
}
4

1 回答 1

0

您在日期格式“yyyymmddHH24mmss”中有小错误,应该是“yyyymmddHH24miss”(“mi”而不是“mm”)mm 表示月份,分钟是“mi”。

于 2012-05-24T22:45:42.783 回答