程序中的一段代码如下:
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try{
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from guestbook order by gst_time desc");
rs.last();
}catch(SQLException se){
se.printStackTrace();
}
int rowCount=rs.getRow();
程序运行后,编译器报错如下:
javax.servlet.ServletException: 对只转发结果集的无效操作: last
java.sql.SQLException: 对只转发结果集的无效操作: last
解决方法:
使用带参数的方法来构造statement:
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY )
允许结果集移动,可以使用last()方法。
本文介绍了一段Java代码在执行SQL查询时遇到的问题,即尝试对只向前移动的结果集使用last()方法导致的异常。通过调整Statement的创建方式,使用支持滚动的结果集类型,解决了这一问题。
566

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



