[英]java.sql.Statement or java.sql.PreparedStatement - scrollable resultset with parameters
In my java app, it seems to use parameters in my query to the database, I need to utilize the PreparedStatement. 在我的java应用程序中,它似乎在我的查询中使用参数到数据库,我需要利用PreparedStatement。 However at the same time, I would like to use the resultset from the statement in forward/backward mode (scrollable) PreparedStatement does not seem to offer setting the scrollable mode Statement does not seem to offer parameters. 但是同时,我想在前进/后退模式(可滚动)中使用语句的结果集,PreparedStatement似乎没有提供设置可滚动模式,Statement似乎没有提供参数。
Seems like a basic question..but nothing jumping out at me (other than using Statement and constructing the SQL without parameters). 看起来像一个基本问题..但没有什么东西跳出来(除了使用Statement和构造没有参数的SQL)。 Is there really no way to supply parameters to a Statement..or have a preparedstatement scrollable? 真的没有办法为Statement提供参数。或者有一个可滚动的预备语句吗? Am I missing something? 我错过了什么吗?
conn = Utility.getConnection();
tmpSQL = "SELECT * FROM " + baseTable + " WHERE " + filterCriteria
+ " ORDER BY " + sortCriteria;
//method 1
Statement stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rset = stmt.executeQuery(tmpSQL); //not using any parameters!
//method 2
PreparedStatement pStatement = conn.prepareStatement(tmpSQL); //not scrollable!
if (params != null)
for (int i = 0; i < params.size(); i++) {
pStatement.setString(i + 1,
((Parameter) params.get(i)).getStringValue());
}
rset = pStatement.executeQuery();
Use 使用
PreparedStatement pStatement = conn.prepareStatement(tmpSQL,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
Then to get the count of records in your ResultSet, use rset.last()
followed by rset.getRow()
. 然后,要获取 ResultSet中的记录计数 ,请使用rset.last()
然后使用rset.last()
rset.getRow()
。 Then use rset.beforeFirst()
to put the cursor back to where it was initially. 然后使用rset.beforeFirst()
将光标放回原来的位置。
Scrollability is mostly depending on the underlying database. 可滚动性主要取决于底层数据库。 Even though JDBC has a method to scroll back, it is not implemented eg in Oracle JDBC driver. 尽管JDBC有一种向后滚动的方法,但它并没有在Oracle JDBC驱动程序中实现。
I would suggest to avoid of scrolling the result set. 我建议避免滚动结果集。 In fact even if it works for some databases, it is quite inefficient to implement. 实际上,即使它适用于某些数据库,实现效率也很低。 Also inefficient to use on the GUI, since each scrolling would then trigger a database operation, which is slow. 在GUI上使用效率也很低,因为每次滚动都会触发数据库操作,这很慢。
The usual approach is to load all rows to a container (eg List<...> ) and process that, if you have a moderate number of rows (say up to 1000 rows). 通常的方法是将所有行加载到容器(例如List <...>)并处理该行,如果您有适度的行数(例如最多1000行)。 If you have a lot more rows, then: 如果你有更多的行,那么:
To get the count of records, execute a separate statement with select count(*). 要获取记录计数,请使用select count(*)执行单独的语句。 Then execute another select to actually read the records and fetch them (only forward). 然后执行另一个选择以实际读取记录并获取它们(仅向前)。
It is much faster than reading all records just to count them. 它比读取所有记录要快得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.