resultset 到 arraylist 出错
新手做毕设显示前台分页效果然后卡在这里了其他的页面代码没问题JSP代码HwBeanBohbb=newHwBeanBo();//默认显示第一页ArrayListal=hbb...
新手做毕设 显示前台分页效果 然后卡在这里了 其他的页面代码没问题
JSP代码
HwBeanBo hbb=new HwBeanBo();
//默认显示第一页
ArrayList al=hbb.getGoodsByPage(4,1);
%>
<table width="100%" height="600" border="1" align="center" cellspacing="0" class="mycss3">
<tr>
<td height="50" colspan="3"><img src="images/tabPanel_bg.gif" width="100%" height="50"></td>
</tr>
<%
int t=0;
for(int i=0;i<2;i++){
//2大行
%>
<tr>
<%
//1大行取三个元素
for(int j=0;j<3;j++){
HwBean hb=(HwBean)al.get(t);
t++;
%>
BEAN代码
public ArrayList getGoodsByPage(int pageSize,int pageNow){
ArrayList al=new ArrayList();
try {
ct=new ConnDb().getConn();
ps=ct.prepareStatement("select * from hw where goodsid limit "+pageSize+","+pageSize*(pageNow-1)+"");
//select * from hw where goodsid limit +"pageSize"+,"+pageSize*(pageNow-1)+"
rs=ps.executeQuery();
if(rs.next()){
HwBean hb=new HwBean();
hb.setGoodsid(rs.getInt(1));
hb.setGoodsname(rs.getString(2));
hb.setGoodsxinxi(rs.getString(3));
hb.setGoodsprice(rs.getInt(4));
hb.setGoodsphoto(rs.getString(5));
//加入到AL中
al.add(hb);
以下报的错误
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /right.jsp at line 28
25:
26: for(int j=0;j<3;j++){
27:
28: HwBean hb=(HwBean)al.get(t);
29: t++;
30: %>
31:
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
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)
root cause
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
java.util.ArrayList.RangeCheck(ArrayList.java:547)
java.util.ArrayList.get(ArrayList.java:322)
org.apache.jsp.right_jsp._jspService(right_jsp.java:86)
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)
求解~~愁死 展开
JSP代码
HwBeanBo hbb=new HwBeanBo();
//默认显示第一页
ArrayList al=hbb.getGoodsByPage(4,1);
%>
<table width="100%" height="600" border="1" align="center" cellspacing="0" class="mycss3">
<tr>
<td height="50" colspan="3"><img src="images/tabPanel_bg.gif" width="100%" height="50"></td>
</tr>
<%
int t=0;
for(int i=0;i<2;i++){
//2大行
%>
<tr>
<%
//1大行取三个元素
for(int j=0;j<3;j++){
HwBean hb=(HwBean)al.get(t);
t++;
%>
BEAN代码
public ArrayList getGoodsByPage(int pageSize,int pageNow){
ArrayList al=new ArrayList();
try {
ct=new ConnDb().getConn();
ps=ct.prepareStatement("select * from hw where goodsid limit "+pageSize+","+pageSize*(pageNow-1)+"");
//select * from hw where goodsid limit +"pageSize"+,"+pageSize*(pageNow-1)+"
rs=ps.executeQuery();
if(rs.next()){
HwBean hb=new HwBean();
hb.setGoodsid(rs.getInt(1));
hb.setGoodsname(rs.getString(2));
hb.setGoodsxinxi(rs.getString(3));
hb.setGoodsprice(rs.getInt(4));
hb.setGoodsphoto(rs.getString(5));
//加入到AL中
al.add(hb);
以下报的错误
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /right.jsp at line 28
25:
26: for(int j=0;j<3;j++){
27:
28: HwBean hb=(HwBean)al.get(t);
29: t++;
30: %>
31:
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:416)
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)
root cause
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
java.util.ArrayList.RangeCheck(ArrayList.java:547)
java.util.ArrayList.get(ArrayList.java:322)
org.apache.jsp.right_jsp._jspService(right_jsp.java:86)
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)
求解~~愁死 展开
1个回答
展开全部
数组下标越界,你的getGoodsByPage方法返回的集合最多只有一个元素
if(rs.next()){
HwBean hb=new HwBean();
hb.setGoodsid(rs.getInt(1));
hb.setGoodsname(rs.getString(2));
hb.setGoodsxinxi(rs.getString(3));
hb.setGoodsprice(rs.getInt(4));
hb.setGoodsphoto(rs.getString(5));
//加入到AL中
al.add(hb);
这里的if应改成while
而你的循环无论集合中有没有值都会遍历前三个元素
int t=0;
for(int j=0;j<3;j++){
HwBean hb=(HwBean)al.get(t);
t++;}
你的循环可以加一个判断条件
for(int j=0;j<3&&t<al.size();j++){
if(rs.next()){
HwBean hb=new HwBean();
hb.setGoodsid(rs.getInt(1));
hb.setGoodsname(rs.getString(2));
hb.setGoodsxinxi(rs.getString(3));
hb.setGoodsprice(rs.getInt(4));
hb.setGoodsphoto(rs.getString(5));
//加入到AL中
al.add(hb);
这里的if应改成while
而你的循环无论集合中有没有值都会遍历前三个元素
int t=0;
for(int j=0;j<3;j++){
HwBean hb=(HwBean)al.get(t);
t++;}
你的循环可以加一个判断条件
for(int j=0;j<3&&t<al.size();j++){
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询