为了练习JSP,正在进行一个建议商城的开发。进行到打印商城商品列表时。出现错误。
我的实现代码如下:
public void getCategoryList(ArrayList<CategoryEntity> list, int pid) {
CategoryDaoImpl dao=new CategoryDaoImpl();
CategoryEntity entity = new CategoryEntity();
Connection conn=null;
ResultSet rs = null;
conn =DB.getConn();
String sql="select * from category where pid="+pid;
try {
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()){
entity.setId(rs.getInt(1));
entity.setName(rs.getString(2));
entity.setDescr(rs.getString(3));
entity.setPid(rs.getInt(4));
entity.setLeaf(rs.getString(5));
entity.setGrade(rs.getInt(6));
list.add(entity);
if(entity.getLeaf().equals("0")){
dao.getCategoryList(list, entity.getId());
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
其中leaf是判断是否存在下一级,是String 类型;我写了一段代码来进行验证,如下:
ArrayList<CategoryEntity> list=new ArrayList<CategoryEntity>();
dao.getCategoryList(list, 0);
for(CategoryEntity e:list){
String str="--";
for(int i=1 ; i<e.getGrade(); i++){
str+=str;
}
System.out.print(str+e.getName()+" ");
System.out.println(e.getId());
}
出现错误,结果图如下:
![图片说明](https://img-ask.csdn.net/upload/201506/24/1435147268_109772.png)
数据库如下:
![图片说明](https://img-ask.csdn.net/upload/201506/24/1435147252_826171.png)