开发者社区> 问答> 正文

Arraylist的一个问题从数据库拿到多组数据但是只能读出一组

这是数据库select方法public List getgameperson() {

    List<Gameperson> list = new ArrayList<Gameperson>();
    Connection conn = null;
    Statement  st = null;
    ResultSet  rs = null;
    try {
        conn = DBO.getconnection();
        st =(Statement) conn.createStatement();
        String  sql = "select *  from gameperson";
        rs = (ResultSet) st.executeQuery(sql);
        if(rs.next()){
            Gameperson gp = new Gameperson();
            gp.setId(rs.getInt("id"));
            gp.setCompetitorId(rs.getInt("competitorId"));
            gp.setGameId(rs.getInt("gameId"));

            list.add(gp);
        }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }finally{
        DBO.close(st, rs, conn);
    }
    return list;
}

这是拿出方法:

<%
List list = new ArrayList();
GamepersonDao gpo = new GamepersonDaoImpl();
list = gpo.getgameperson();
if(list!=null){
for (int i=0;i<list.size();i++){
Gameperson gp = list.get(i);
%>
<%=gp.getId()%>

<%=gp.getGameId() %>

<%=gp.getCompetitorId()%>

<%
}
} %>

现在数据库有很多组数据,但是为什么页面只能显示遍历一组?

展开
收起
蛮大人123 2016-03-13 19:09:09 2211 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    public List getgameperson() {
     // TODO Auto-generated method stub
        List<Gameperson> list = new ArrayList<Gameperson>();
        Connection conn = null;
        Statement  st = null;
        ResultSet  rs = null;
        try {
            conn = DBO.getconnection();
            st =(Statement) conn.createStatement();
            String  sql = "select *  from gameperson";
            rs = (ResultSet) st.executeQuery(sql);
           while(rs.next()){
                Gameperson gp = new Gameperson();
                gp.setId(rs.getInt("id"));
                gp.setCompetitorId(rs.getInt("competitorId"));
                gp.setGameId(rs.getInt("gameId"));
    
                list.add(gp);
            }
    
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
            DBO.close(st, rs, conn);
        }
        return list;
    }
    2019-07-17 19:03:03
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载
云时代的数据库技术趋势 立即下载
超大型金融机构国产数据库全面迁移成功实践 立即下载