用JDBC取数据放在list里面结果值为空??

public class test {
    public static void main(String[] args){
        List<Goods> goods=new ArrayList<Goods>();
        String sql="select * from goodsinfo";
        DBUtil db=new DBUtil();
        Connection conn=db.getConnection();
        Statement stmt=null;
        ResultSet rs=null;
        Goods goodone;
        try{
            stmt=conn.createStatement();
            rs=stmt.executeQuery(sql);
            Goods good=null;
            while(rs.next()){
                int gid=rs.getInt(1);
                String gname=rs.getString(2);
                double gprice=rs.getDouble(3);
                int gamount=rs.getInt(4);
                String gdate=rs.getString(5);
                String gclass=rs.getString(6);
                String gimgurl=rs.getString(7);
                String gintro=rs.getString(8);
                String gbrief=rs.getString(9);
                good=new Goods(gid,gname,gprice,gclass,gamount,gdate,gimgurl,gintro,gbrief);
                goods.add(good);
                System.out.println(gname+"哈哈");
            }
            db.closeAll(conn, stmt, rs);
        } catch(SQLException e){
            e.printStackTrace();
        }
        goodone=goods.get(1);
        System.out.println(goodone.getGname()+"喽喽");
    }
}

控制台输出为:

        qweq哈哈
         测试哈哈
        第三个哈哈
         篮球哈哈
        null喽喽

代码如上,结果输出只有上面的哈哈,下面的喽喽为空值是为啥,一直想不明白

阅读 2.8k
1 个回答

1、是不是getGname方法返回有问题
2、是不是Goods(gid,gname,gprice,gclass,gamount,gdate,gimgurl,gintro,gbrief)构造方法有问题

宣传栏