java获取未知结构的游标,在java中获取无效的游标状态异常

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)

解决方案

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.

The following kluge seems to work around the issue for me:

String select="SELECT * FROM (SELECT DISTINCT FirstName,LastName FROM Clients)";

Switching from ResultSet.TYPE_SCROLL_SENSITIVE to ResultSet.TYPE_FORWARD_ONLY also appears to avoid the issue.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值