2011-10-17 64 views
0
public class UserBean<br> 
{ 
public String name; 
public String role; 
} 

class Dao{ 
ArrayList<UserBean> a = new ArrayList<UserBean>(); 
String sql = "Select * from user"; 
ResultSet rs = pStmt.executeQuery(sql); 
while(rs.next()){ 
//----------Problem------------ 
// i want to do something like<br> 
    a.add[0].name = rs.getString("name"); 
    a.add[0].role = rs.getString("role"); 
// Get column from resultset and set the value of userbean column 
} 
} 
+0

抱歉線沒有圖9是ArrayList的一個新= ArrayList的(); –

回答

1

你可以試試:

public class UserBean { 
    public String name; 
    public String role; 
} 

class Dao{ 
    ArrayList<UserBean> a= new ArrayList<UserBean>(); 
    String sql = "Select * from user"; 
    ResultSet rs = pStmt.executeQuery(sql); 
    while(rs.next()){ 
     UserBean ub = new UserBean(); 
     ub.name=rs.getString("name"); 
     ub.role=rs.getString("role"); 
     a.add(ub); 

    } 
} 
4

創建的UserBean一個實例,並把它添加到List

while(rs.next()){ 
    UserBean bean=new UserBean(); 
    bean.name = rs.getString("name"); 
    bean.role = rs.getString("role"); 
    a.add(bean); 
// Get column from resultset and set the value of userbean column 
}