jsp中rs.last()的使用,结果集不可操作!
如今小弟遇到如下问题,利用last()方法,得到移到最后一行。然而,同样代码都在MyEclipse中,在不同的项目中,一个能执行,另一个不能执行,为什么?(可以连接到数据库,并查询到数据)
public ResultSet ExecuteSELECT(String sql) throws SQLException
{
try
{
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}
catch(SQLException ex)
{System.err.println(ex.getMessage());}
return rs;
}
ConnectDatabase conn=new ConnectDatabase();
conn.GetConnection();
String sql="select * from ware";
ResultSet rs=conn.ExecuteSELECT(sql);
if(rs.last())
out.print("success");
else
out.print("lost");
问题:javax.servlet.ServletException: com.microsoft.sqlserver.jdbc.SQLServerException: 只进结果集不支持请求的操作。
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:852)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:781)
org.apache.jsp.shop.sort.MyJsp_jsp._jspService(MyJsp_jsp.java:113)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
------解决方案--------------------
在JAVA中获取ResultSet的长度时提示错误:对只转发结果集的无效操作: last
主要是加上这个就可以了:ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY
Java code
ps = conn
.prepareStatement("SELECT * FROM aa",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs.last();
System.out.println("长度:"+rs.getRow());