实面页面数据按编号排序,下面的我实现的代码,以前做集合排序,但在项目的无法排序,谁能给我找出问题所在吗,谢谢@Override
public Collection<UserModel> getAll() {
Collection<UserModel> col = new TreeSet();
Connection conn = null;
try{
conn = DBconn.getConn();
String sql = "select * from scott.tb1_user ";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
String id = rs.getString("id");
String name = rs.getString("name");
String pwd = rs.getString("pwd");
int type = rs.getInt("type");
UserModel um = new UserModel();
um.setId(id);
um.setName(name);
um.setPwd(pwd);
um.setType(type);
col.add(um);
}
}catch(Exception err){
err.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return col;
}
Model的实现在Comparadle
@Override
//排序
public int compareTo(Object obj) {
UserModel um = (UserModel) obj;
if(this.getId().compareTo(um.getId())>0){
return 1;
}else if(this.getId().compareTo(um.getId())==0){
return 0;
}else{
return 1;
}
}list.jsp:Collection<UserModel> col = new TreeSet();
String isQuery = (String)request.getAttribute("isQuery");
if(isQuery!=null && isQuery.equals("true")){
col = (Collection<UserModel>)request.getAttribute("myCol");
}else{
col = Factory.getUserEbi().getAll();
for(UserModel um : col){
................
}
public Collection<UserModel> getAll() {
Collection<UserModel> col = new TreeSet();
Connection conn = null;
try{
conn = DBconn.getConn();
String sql = "select * from scott.tb1_user ";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
String id = rs.getString("id");
String name = rs.getString("name");
String pwd = rs.getString("pwd");
int type = rs.getInt("type");
UserModel um = new UserModel();
um.setId(id);
um.setName(name);
um.setPwd(pwd);
um.setType(type);
col.add(um);
}
}catch(Exception err){
err.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return col;
}
Model的实现在Comparadle
@Override
//排序
public int compareTo(Object obj) {
UserModel um = (UserModel) obj;
if(this.getId().compareTo(um.getId())>0){
return 1;
}else if(this.getId().compareTo(um.getId())==0){
return 0;
}else{
return 1;
}
}list.jsp:Collection<UserModel> col = new TreeSet();
String isQuery = (String)request.getAttribute("isQuery");
if(isQuery!=null && isQuery.equals("true")){
col = (Collection<UserModel>)request.getAttribute("myCol");
}else{
col = Factory.getUserEbi().getAll();
for(UserModel um : col){
................
}
就是按ID排序
哦,懂了!LZ不用单独处理对象集合,直接在查询语句里面排序就可以了!改一下你的SQL语句如:select * from scott.tb1_user order by id ASC