首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

求高手帮忙看看下面这个代码!解决方法

2012-04-02 
求高手帮忙看看下面这个代码!!!!这是GoodsBean代码package com.shop.modelpublic class GoodBean {privat

求高手帮忙看看下面这个代码!!!!
这是GoodsBean代码
package com.shop.model;

public class GoodBean {


private long goodsId;
private String goodsName;
private String goodsIntro;
private float goodsPrice;
private int goodsNum;
private String publisher;
private String photo;
private String type;
public long getGoodsId() {
return goodsId;
}
public void setGoodsId(long goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public String getGoodsIntro() {
return goodsIntro;
}
public void setGoodsIntro(String goodsIntro) {
this.goodsIntro = goodsIntro;
}
public float getGoodsPrice() {
return goodsPrice;
}
public void setGoodsPrice(float goodsPrice) {
this.goodsPrice = goodsPrice;
}
public int getGoodsNum() {
return goodsNum;
}
public void setGoodsNum(int goodsNum) {
this.goodsNum = goodsNum;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getPhoto() {
return photo;
}
public void setPhoto(String photo) {
this.photo = photo;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}

}


这是显示分页代码


//这是对数据库中的表进行操作
package com.shop.model;

import java.sql.*;
import java.util.ArrayList;

public class GoodsBeanBO {

private Connection con=null;
private PreparedStatement ps=null;
private ResultSet rs=null;


//分页显示
public ArrayList getGoodsByPage(int pageSize,int pageNow){
ArrayList al=new ArrayList();
try {

con=new DBConnection().getConnection();
ps=con.prepareStatement("select top "+pageSize+" * from goods where goodsId not in(select top "+pageSize*(pageNow-1)+" goodsId from goods);");
rs=ps.executeQuery();

while(rs.next()){
GoodBean gb=new GoodBean();
gb.setGoodsId(rs.getInt(1));
gb.setGoodsName(rs.getString(2));
gb.setGoodsIntro(rs.getString(3));
gb.setGoodsPrice(rs.getFloat(4));
gb.setGoodsNum(rs.getInt(5));
gb.setPublisher(rs.getString(6));
gb.setPhoto(rs.getString(7));
gb.setType(rs.getString(8));
al.add(al);
}

} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally{
this.close();

}
return al;

}

//关闭资源
public void close(){
try {
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();

}
if(con!=null){
con.close();
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
}


这是jsp代码:
<%@ page language="java" import="java.util.*,com.shop.model.*"
pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

//接受al集合
ArrayList<GoodBean> al=new GoodsBeanBO().getGoodsByPage(6,1);
for(int i=0;i<al.size();i++){
 GoodBean gb=(GoodBean)al.get(i);
 System.out.println("商品的编号是:"+gb.getGoodsId());
}

%>

GoodBean gb=(GoodBean)al.get(i);
运行时显示这句有错误,希望高手解决下?

这是错误结果:
二月 05, 2012 5:08:55 下午 org.apache.catalina.core.ApplicationDispatcher invoke


严重: Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.shop.model.GoodBean
at org.apache.jsp.right_jsp._jspService(right_jsp.java:53)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:659)
at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:565)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:493)
at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:965)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:86)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:879)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:722)

二月 05, 2012 5:08:55 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.shop.model.GoodBean
at org.apache.jsp.right_jsp._jspService(right_jsp.java:53)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:659)


at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:565)
at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:493)
at org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:965)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:86)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:371)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:308)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:259)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:879)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:722)



[解决办法]
异常都已经告诉你狠清楚了:java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.shop.model.GoodBean
类转换异常啊,你的list用了泛型,但是你在getGoodsByPage()方法里面往list塞的是什么对象啊??也不看看?不报错就见鬼了

while(rs.next()){
GoodBean gb=new GoodBean();
gb.setGoodsId(rs.getInt(1));
gb.setGoodsName(rs.getString(2));
gb.setGoodsIntro(rs.getString(3));
gb.setGoodsPrice(rs.getFloat(4));
gb.setGoodsNum(rs.getInt(5));
gb.setPublisher(rs.getString(6));
gb.setPhoto(rs.getString(7));
gb.setType(rs.getString(8));
al.add(al);
}

要往al里面add(gb)才对......
好好读读异常信息吧

热点排行