日期:2014-05-17  浏览次数:20624 次

请问我怎么在severlet里接受DAO里的一个list<>的集啊?
我的DAO:
public class OperateDb {
 
private static Connection conn = null; // 声明数据库连接对象

public OperateDb(){}

public List<Goods> queryhot(){
int i;
String sql = "select goodsname,image from goods where classes=1 order by adtime desc";
DataBaseConnection dbc=new DataBaseConnection();
conn=dbc.getConnection();
List<Goods> hotgoods=new ArrayList<Goods>();
try {
Statement sta = conn.createStatement();
ResultSet res = sta.executeQuery(sql);
for (i=0;i<3;i++) {
String goodsname = res.getString(1);
String image=res.getString(2);
hotgoods.add(new Goods(goodsname,image));
}
res.close();
sta.close();
} catch (SQLException e) {
e.printStackTrace();
}

return hotgoods;
}
}

我的servlet:

private OperateDb dao1;

public void init() throws ServletException {
dao1=new OperateDb();
}
private void queryNewGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Goods> hotgs=dao1.queryhot();
Goods g1=hotgs.get(1);
request.setAttribute("NO1", g1.getGoodsname());
request.getRequestDispatcher("/index.jsp").forward(request,response);
  }


这样写在JSP页面显示为NULL;
请问正确的写法是怎样的呢,就是我想在JSP页面显示从数据库取到的集。
PS:MYeclipse的debug怎么用的啊,我设置了断点,也把tomcat设置了DEBUG模式。但是启动服务后没有任何反应啊,或者在哪里能看到服务器端的显示啊,我在DAO里用main函数也显示不了。



------解决方案--------------------
探讨
for (i=0;i<3;i++) {

------解决方案--------------------
探讨

引用:
for (i=0;i<3;i++) {

这一句可改成
while (res.next()) {

然后再看有没有其他问题。

------解决方案--------------------
游标就没下移 一行信息都取不到

楼上正解 res.next();方法返回布尔类型同时游标还下移了一行
------解决方案--------------------
1.select goodsname,image from goods where classes=1 order by adtime desc检查数据库中能查出值不
2.迭代结果集res不当,
for (i=0;i<3;i++) {//用while(res.hasnext)还是while(res.next)记不清了
String goodsname = res.getString(1);
String image=res.getString(2);
hotgoods.add(new Goods(goodsname,image));
}
3.res.close();
sta.close();
conn最好关了
4.这是主要问题,servlet要么重写service要么写在doGet,要么doPost里面,private void queryNewGoods(HttpServletRequest 自定义方法名不行
5.打了断电,用debug模式启动,不是直接运行,小爬虫图标