jsp中记录集rs传值到行集对象CachedRowSetImpl,有经验的帮忙看看

潜水的小懒猫 2010-05-14 06:59:50
package myservlet.control;
import mybean.data.*;
import com.sun.rowset.*;
import java.sql.*;
import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class HandleDatabase extends HttpServlet{
CachedRowSetImpl rowSet=null;
public void init(ServletConfig config) throws ServletException
{
super.init(config);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e){}
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
HttpSession session=request.getSession(true);
Login login=(Login)session.getAttribute("login"); //session获取login,为空就转到登陆界面
boolean ok=true;
if(login==null)
{
ok=false;
response.sendRedirect("login.jsp");
}
if(ok==true)
{
continueDoPost(request,response); //这是post提交的,在这里就使用了下面的方法,显示全部会员
}
}
public void continueDoPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
HttpSession session=request.getSession(true);
Connection con=null;
StringBuffer presentPageResult=new StringBuffer(); //当前内容
ShowByPage showBean=null; //创建分页对象
try{
showBean=(ShowByPage)session.getAttribute("show"); //获取show对象
if(showBean==null)
{
showBean=new ShowByPage(); //创建分页对象
session.setAttribute("show",showBean); //没有show时,就创建sessin周期的bean
}
}
catch(Exception exp)
{
showBean=new ShowByPage();
session.setAttribute("show",showBean);
}
showBean.setPageSize(3); //设置每页显示3条
int showPage=Integer.parseInt(request.getParameter("showPage")); //获取当前显示页
if(showPage>showBean.getPageAllCount()) //当前页大于总页数,就变为1
showPage=1;
if(showPage<=0)
showPage=showBean.getPageAllCount(); //小于总页数,就现在最后一页
showBean.setShowPage(showPage); //把当前页传到bean
int pageSize=showBean.getPageSize(); //获取每页显示的记录数

try{
con=DriverManager.getConnection("jdbc:odbc:zc","sa","");
Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=sql.executeQuery("select * from member");
rowSet=new CachedRowSetImpl(); //创建行集对象
rowSet.populate(rs);
showBean.setRowSet(rowSet); //再把行集值传到bean
rowSet.last();
int m=rowSet.getRow(); //总行数

int n=pageSize; //n=每页显示的记录数
int pageAllCount=((m%n)==0)?(m/n):(m/n+1); //总页数
showBean.setPageAllCount(pageAllCount); //总页数传到bean
presentPageResult=show(showPage,pageSize,rowSet);
showBean.setPresentPageResult(presentPageResult); //传入当前页的内容
con.close();
}
catch(SQLException exp){
showBean.setPageAllCount(88888);
}
RequestDispatcher dispatcher=request.getRequestDispatcher("showAllMember.jsp");
dispatcher.forward(request,response);
}
public StringBuffer show(int page,int pageSize,CachedRowSetImpl rowSet)
{ // 显示的页数,每页显示的记录数,行集数据
StringBuffer str=new StringBuffer();
try{
rowSet.absolute((page-1)*pageSize+1);
for(int i=1;i<=pageSize;i++)
{
str.append("<tr>");
str.append("<td>"+rowSet.getString(1)+"</td>");
str.append("<td>"+rowSet.getString(2)+"</td>");
str.append("<td>"+rowSet.getString(3)+"</td>");
str.append("<td>"+rowSet.getString(4)+"</td>");
str.append("<td>"+rowSet.getString(5)+"</td>");
str.append("<td>"+rowSet.getString(6)+"</td>");
String s="<img src=/image/"+rowSet.getString(7)+" width=100 height=100/>";
str.append("<td>"+s+"</td>");
str.append("</tr>");
rowSet.next();
}
return str;
}
catch(SQLException exp){}
return str;
}


测试过,rs有值,rowSet为空 ,运行时,直接跳转到showBean.setPageAllCount(7777);
当然showAllMember.jsp页面出现了~~只是数据库的数据没有显示

应该是rowSet为空造成的错误,希望高手看看,帮解决下!为什么rowset为空了?

...全文
243 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
潜水的小懒猫 2010-05-16
  • 打赏
  • 举报
回复
这是 jsp实用教程(二) 耿祥义 编写的那本书 后面实例的代码,自己照打一遍和直接复制都是一样的结果。。。。。。郁闷!。。。。

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧