我已经看过SO上的每一个例子(也看过其他地方),我对这个问题感到难过。
tblNodes是用于存储JTree的MS Access dbase表...在读入时我尝试查找存在多个具有相同父ID和相同父记录的异常情况的所有情况兄弟姐妹指数。
这是我的SSCCE,虽然没有dbase本身显然没那么有用。但是我认为任何有这方面知识的人都会发现输出很令人头疼。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class CursorProb {
public static void main(String[] args) throws SQLException {
// normalise where we find any duplicate sib_index+parent ID pairings...
// this more complex SQL statement lists the parent nodes involved
// String dup_sib_indices_sql = "SELECT Parent FROM ( SELECT Parent, SiblingIndex, Count(*) " +
// "FROM tblNodes GROUP BY Parent, SiblingIndex HAVING Count(*)>1 ) AS ParentsToNormalise GROUP BY Parent";
// but, bafflingly, even this simpler one produces the "Invalid cursor state" error
String dup_sib_indices_sql =
"SELECT tblNodes.Parent, tblNodes.SiblingIndex FROM tblNodes GROUP BY tblNodes.Parent, tblNodes.SiblingIndex";
String data_source = "jdbc:odbc:Copy of kernel3_1 DATA $USER";
Connection dbase_conn = DriverManager.getConnection( data_source, "", "");
PreparedStatement ps_dup_sib_indices = dbase_conn.prepareStatement( dup_sib_indices_sql,
java.sql.ResultSet.TYPE_SCROLL_SENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY );
// NB various combos tried here:
// java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_UPDATABLE ); ... etc.
ResultSet rs = ps_dup_sib_indices.executeQuery();
// rs.beforeFirst(); <-- makes no difference
int i = 0;
while ( rs.next() ){
int parent_id = rs.getInt( "Parent" );
System.out.println( "=== parent_id " + parent_id + ", i " + i );
i++;
}
System.out.println( "=== Never get to here..." );
}
}
这是输出:
=== parent_id 0, i 0
=== parent_id 1, i 1
...
=== parent_id 2765, i 491
=== parent_id 2768, i 492
Exception in thread "main" 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.SQLGetDataInteger(JdbcOdbc.java:3818)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataInteger(JdbcOdbcResultSet.java:5637)
at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(JdbcOdbcResultSet.java:582)
at sun.jdbc.odbc.JdbcOdbcResultSet.getInt(JdbcOdbcResultSet.java:600)
at root.CursorProb.main(CursorProb.java:34)
在MS Access dbase中运行SQL作为查询本身不会产生任何问题(并在错误之前生成相同的输出:493行)。
即。 &#34; SELECT tblNodes.Parent,tblNodes.SiblingIndex FROM tblNodes GROUP BY tblNodes.Parent,tblNodes.SiblingIndex;&#34;