如何创建可滚动的ResultSet? [英] How to create a scrollable ResultSet?
问题描述
由于我设置了ResultSet.TYPE_SCROLL_INSENSITVE(与Javadocs中的示例相同),我得到了一个简单的代码来从MSSQL Server 2008中检索记录集,该记录集必须是可滚动的:
I got this simple code to retrieve a recordset from a MSSQL Server 2008 which has to be scrollable due to the fact that I set the ResultSet.TYPE_SCROLL_INSENSITVE, same as the example from the Javadocs:
String qry = "SELECT * from tblPeople";
SQLConnection sql = new SQLConnection();
Statement stmt = sql.getConnection().createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery(qry);
不幸的是,当我想获得像rs.last(); int rowCount = rs.getRow();
这样的行数时,我仍然得到了堆栈跟踪:
Unfortunately I still got this Stack Trace when I want to get the row count like rs.last(); int rowCount = rs.getRow();
:
java.sql.SQLException: ResultSet may only be accessed in a forward direction.
at net.sourceforge.jtds.jdbc.JtdsResultSet.checkScrollable(JtdsResultSet.java:304)
at net.sourceforge.jtds.jdbc.JtdsResultSet.last(JtdsResultSet.java:551)
at test.personen.Main.main(Main.java:44)
为什么会这样,如何解决(顺便说一句,当我检查ResultSet的类型时,得到1003 ..)?
Why is that and how can I fix it (by the way, when I check the Type of the ResultSet, I get 1003..)?
推荐答案
TYPE_SCROLL_INSENSITIVE
的组合很可能与CONCUR_UPDATABLE
不兼容.根据JDBC规范,如果驱动程序不能为请求的可滚动性和/或并发模式提供服务,则可以自由地将ResultSet
降级.另请参见: http://jtds.sourceforge.net/resultSets.html :
Most likely the combination of TYPE_SCROLL_INSENSITIVE
is not compatible with CONCUR_UPDATABLE
. According to the JDBC specification, a driver is free to downgrade a ResultSet
if it can't service the requested scrollability and/or concurrency mode. See also: http://jtds.sourceforge.net/resultSets.html :
TYPE_SCROLL_INSENSITIVE |静态游标|重型|仅适用于只读并发(可更新的版本已降级). SQL Server会生成一个临时表,因此其他人所做的更改是不可见的.可滚动.
TYPE_SCROLL_INSENSITIVE | Static cursor | Heavy | Only works with read-only concurrency (updatable is downgraded). SQL Server generates a temporary table, so changes made by others are not visible. Scrollable.
指定CONCUR_UPDATABLE
时确认驱动程序将降级.
Which confirms that the driver will downgrade when specifying CONCUR_UPDATABLE
.
您可能要考虑使用TYPE_SCROLL_SENSITIVE
,或者根本不将滚动性与可升级性结合使用.
You might want to consider using TYPE_SCROLL_SENSITIVE
or simply not combine scrollability with updatability.
这篇关于如何创建可滚动的ResultSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!