在Java中获取无效的游标状态异常 [英] Getting Invalid cursor state exception in java
问题描述
我运行了select命令,并使用以下代码将结果打印到system.out中.由于无效的光标错误而获得了预期的结果. 您能否告诉任何人,为什么在打印预期结果后出现此错误,以及如何解决? 代码:
i ran select command and printed the result in system.out using below code. was getting expected result with invalid cursor error. could you please any one tell, why this error was occurred after printing the expected result and how to fix it? code:
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "path";
conn = DriverManager.getConnection(url);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
String select="SELECT DISTINCT col1,col2 FROM Tablename";
ResultSet rs = stmt.executeQuery(select);
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
String columnValue;
while (rs.next())
{
for (int i = 1; i <= columnsNumber; i++) {
columnValue= rs.getString(i);
System.out.print(columnValue+" ");
}
}
}
catch(SQLException exc){
exc.printStackTrace();
}
output:
test1 result1
test2 result2
test3 result3
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6964)
at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7121)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(JdbcOdbc.java:3914)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(JdbcOdbcResultSet.java:5697)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(JdbcOdbcResultSet.java:353)
推荐答案
我能够重新创建您的问题.当使用SELECT DISTINCT ...
查询和ResultSet.TYPE_SCROLL_SENSITIVE
时,它似乎是JDBC-ODBC桥和Access ODBC驱动程序的不幸行为".
I was able to recreate your issue. It appears to be an "unfortunate behaviour" of the JDBC-ODBC Bridge and the Access ODBC driver when working with SELECT DISTINCT ...
queries and ResultSet.TYPE_SCROLL_SENSITIVE
.
以下kluge似乎可以解决我的问题:
The following kluge seems to work around the issue for me:
String select="SELECT * FROM (SELECT DISTINCT FirstName,LastName FROM Clients)";
从ResultSet.TYPE_SCROLL_SENSITIVE
切换到ResultSet.TYPE_FORWARD_ONLY
似乎也可以避免此问题.
Switching from ResultSet.TYPE_SCROLL_SENSITIVE
to ResultSet.TYPE_FORWARD_ONLY
also appears to avoid the issue.
但是,由于JDBC-ODBC Bridge已过时并且已从Java 8中删除,因此您可以考虑使用 UCanAccess JDBC驱动程序.有关更多详细信息,请参见
However, since the JDBC-ODBC Bridge is obsolete and has been removed from Java 8 you might consider using the UCanAccess JDBC driver instead. For more details see
这篇关于在Java中获取无效的游标状态异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!