关于数据库取数据并显示到html:select的很难描述的问题

[问题]  

原:

     public   void  parentmsg(ActionForm form, HttpServletRequest request) {
        DataConn dc
=new DataConn();
        Connection conn;
        String sql
=null;

        ArrayList fenleis 
= new ArrayList();
                                          FenleiForm fenlei
= new FenleiForm();
        
        
try {
            conn 
= dc.getConnection();
            Statement stmt 
= conn.createStatement();
            sql
="select * from fenlei where parid=1";
            ResultSet rs
=stmt.executeQuery(sql);
            
int i=0;
            
while (rs.next()){
                                
                fenlei.setLbid(rs.getInt(
"lbid"));
                fenlei.setLbname(rs.getString(
"lbname"));
                
                System.out.println(rs.getInt(
"lbid"));  //测试用
                System.out.println(rs.getString("lbname"));
                
                fenleis.add(i, fenlei); 
//这里的fenlei实际是按地址传递的

                i
++;
            }

            request.setAttribute(
"lstfenlei", fenleis);
            
//fenlei.setFenleis(fenleis);
        }
catch(Exception e){
            System.out.println(e.getMessage());
        }

    }

lstfenlei的最终结果:
lbid=3   lbname="女童装"
lbid=3   lbname="女童装"
修改后

     public   void  parentmsg(ActionForm form, HttpServletRequest request) {
        DataConn dc
=new DataConn();
        Connection conn;
        String sql
=null;

        ArrayList fenleis 
= new ArrayList();
        
        
try {
            conn 
= dc.getConnection();
            Statement stmt 
= conn.createStatement();
            sql
="select * from fenlei where parid=1";
            ResultSet rs
=stmt.executeQuery(sql);
            
int i=0;
            
while (rs.next()){
                FenleiForm fenlei
= new FenleiForm();  //要放在循环里面,用来重新在内存中开辟一块区域存储数据
                
                fenlei.setLbid(rs.getInt(
"lbid"));
                fenlei.setLbname(rs.getString(
"lbname"));
                
                System.out.println(rs.getInt(
"lbid"));  //测试用
                System.out.println(rs.getString("lbname"));
                
                fenleis.add(i, fenlei); 
//这里的fenlei实际是按地址传递的

                i
++;
            }

            request.setAttribute(
"lstfenlei", fenleis);
            
//fenlei.setFenleis(fenleis);
        }
catch(Exception e){
            System.out.println(e.getMessage());
        }

    }

lstfenlei最终结果:
lbid=2   lbname="男童装"
lbid=3   lbname="女童装"

jsp页面:

< html:select  property ="lbid" >
    
< html:option  value ="0"   > --请选择父类别-- </ html:option >
    
< html:optionsCollection  name ="lstfenlei"  label ="lbname"  value ="lbid"    />
</ html:select >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值