// 회원메인방의 회원 리스트 정렬 순서 선택
public ArrayList<LoginVO> getListSorted(String sortType) {
ArrayList<LoginVO> vos = new ArrayList<LoginVO>();
try {
sql = sortListCommonSqlQuery(sortType);
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
LoginVO vo = new LoginVO();
vo.setIdx(rs.getInt("idx"));
vo.setMid(rs.getString("mid"));
vo.setPwd(rs.getString("pwd"));
vo.setName(rs.getString("name"));
vo.setAge(rs.getInt("age"));
vo.setGender(rs.getString("gender"));
vo.setAddress(rs.getString("address"));
vos.add(vo);
}
} catch (SQLException e) {
System.out.println("SQL 오류 : " + e.getMessage());
} finally {
rsClose();
}
return vos;
}
// 정렬 순서 선택을 위한 sql 공통 쿼리 + order by 필드명 desc or 생략
private String sortListCommonSqlQuery(String sortType) {
String baseSql = "SELECT * FROM hoewon ";
switch (sortType) {
case "latest": return baseSql + "ORDER BY idx DESC";
case "oldest": return baseSql + "ORDER BY idx";
case "midDesc": return baseSql + "ORDER BY mid DESC";
case "midAsc": return baseSql + "ORDER BY mid";
case "nameDesc": return baseSql + "ORDER BY name DESC";
case "nameAsc": return baseSql + "ORDER BY name";
case "ageDesc": return baseSql + "ORDER BY age DESC";
case "ageAsc": return baseSql + "ORDER BY age";
default: return baseSql;
}
}
첫댓글 다양한 정렬 아주 좋습니다.
dao의 다중선택부분을 조금 줄일수 있듯하네요. 수고하셨습니다.