我的问题:我想知道为什么每次运行来自预准备语句的查询结果应该是不一致的。
警告未来奇怪的事情。
我在Oracle数据库上。我正在使用ojdbc6.jar
驱动程序。我有一个返回大约1300行的查询(它随着时间的推移而变化,因为它有一个where
子句,它取决于sysdate
)。当我在Toad(SQL Developer工具)和Netbeans Services工具(与数据库的另一个接口)中运行它时,它运行良好。我的结果是一致的。但是当我在java代码中运行查询时,我的结果非常不一致。为了尝试查看每个结果计数返回的频率,我将其放入for循环并迭代10次。每次运行中都有一个相对一致的行数,但不是在运行中。我的输出和代码如下。让我知道是否有任何更多信息是有用的。
尝试有效时(sorta ...):
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;
}
答案 0 :(得分:0)
日期格式'yyyymmddHH24mmss'中有小错误,应该是'yyyymmddHH24miss'(“mi”而不是“mm”) mm表示月份,分钟表示“mi”。