今天这个小小的问题难住我半天,(毕竟实习生水平)~~浪费光阴啊。不过总算解决了
ResultSet may only be accessed in a forward direction 这个错误的引起是jTDS驱动中的TYPE_SCROLL_INSENSITIVE只支持只读操作,TYPE_SCROLL_SENSITIVE支持Update操作,但不支持另外的Insert
所以我们prepareStatement()函数的第二个参数用 TYPE_SCROLL_SENSITIVE比较好
//某表的记录的行数
public int CountRowNumber(String tableName){
//参数为表名
try {
ResultSet rs = null;
String sql = "select * from "+tableName;
//Statement stmt = conn.createStatement();
PreparedStatement pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = pstmt.executeQuery();
rs.last();
return rs.getRow();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
}
ResultSet may only be accessed in a forward direction 这个错误的引起是jTDS驱动中的TYPE_SCROLL_INSENSITIVE只支持只读操作,TYPE_SCROLL_SENSITIVE支持Update操作,但不支持另外的Insert
所以我们prepareStatement()函数的第二个参数用 TYPE_SCROLL_SENSITIVE比较好
//某表的记录的行数
public int CountRowNumber(String tableName){
//参数为表名
try {
ResultSet rs = null;
String sql = "select * from "+tableName;
//Statement stmt = conn.createStatement();
PreparedStatement pstmt = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = pstmt.executeQuery();
rs.last();
return rs.getRow();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return -1;
}
}

本文深入探讨了在使用JDBC时遇到的ResultSet相关错误,特别是与jTDS驱动中TYPE_SCROLL_INSENSITIVE和TYPE_SCROLL_SENSITIVE类型的选择。通过分析一个具体的实例,解释了如何正确设置prepareStatement函数的第二个参数来避免错误,并实现对数据库表记录行数的准确计数。
2381

被折叠的 条评论
为什么被折叠?



