파일명 : BbsMgr.java
-----------------------------------------------------
/**
* 레코드 목록 가져오기
*/
public ArrayList list(String col, String word, int nowPage){
Connection con = this.dbconnect.getConnection();
ArrayList list = null;
try{
list = getDAO().list(con,col,word,nowPage);
}catch(Exception e){
System.out.println(e.toString());
}finally{
DBClose.close(con);
}
return list;
}
//레코드 카운트
public int recordCount(){
Connection con = this.dbconnect.getConnection();
int retVal=0;
try{
retVal=getDAO().recordCount(con);
}catch(Exception e){
System.out.println(e.toString());
}finally{
DBClose.close(con);
}
return retVal;
}
-----------------------------------------------------
파일명 : BbsDAO.java
-----------------------------------------------------
//검색,페이징
public ArrayList list(Connection con,String col,String word,int nowPage) throws SQLException{
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer sql = null;
ArrayList list = new ArrayList(); // dto 목록을 저장
String where = ""; // 검색 조건
// 아무런 검색을 하지 않는 목록
if(word.equals("null") || word.equals("")){
where = "";
}
else{
// 단일 컬럼으로 검색하는 목록
// 컬럼과 검색어를 동적으로 지정
// wname LIKE '%apple%'";
// subject LIKE '%JSP Job%'";
// content LIKE '%Incheon%'";
where = col +" LIKE '%" + word + "%'";
}
sql = new StringBuffer();
sql.append(" SELECT bbsno, wname, subject, regdate, cnt,grpno,indent,ansnum ") ;
sql.append(" FROM "+table_name);
if(!(where.trim()).equals("") == true){
sql.append(" WHERE " + where);
}
sql.append(" ORDER BY grpno DESC,ansnum ASC LIMIT "+(nowPage-1)*5+", 5");
//SELECT 결과 중 11번째부터 20개 가져오기
//LIMIT 10,20
pstmt = con.prepareStatement(sql.toString());
rs = pstmt.executeQuery();
while(rs.next()) {
BbsDTO dto = new BbsDTO();
dto.setBbsno(rs.getInt("bbsno"));
dto.setWname(rs.getString("wname"));
dto.setSubject(rs.getString("subject"));
dto.setRegdate(rs.getString("regdate"));
dto.setCnt(rs.getInt("cnt"));
dto.setGrpno(rs.getInt("grpno"));
dto.setIndent(rs.getInt("indent"));
dto.setAnsnum(rs.getInt("ansnum"));
list.add(dto);
}
DBClose.close(pstmt,rs);
return list;
}
public int recordCount(Connection con) throws SQLException{
PreparedStatement pstmt = null;
ResultSet rs = null;
StringBuffer sql = null;
int retVal = 0;
sql = new StringBuffer();
sql.append(" SELECT count(*) as cnt");
sql.append(" FROM " + table_name);
System.out.println("SQL: " + sql.toString());
pstmt = con.prepareStatement(sql.toString());
rs = pstmt.executeQuery();
if(rs.next()){ // 레코드가 있는 경우 첫번째 레코드로 이동
retVal = rs.getInt("cnt");
}
DBClose.close(pstmt, rs);
return retVal;
}
-----------------------------------------------------
파일명 : bbsList.jsp
-----------------------------------------------------
<%@ page contentType="text/html; charset=utf-8"%>
<%@ include file="./ssi.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> bbsList.jsp </title>
</head>
<body>
<center>
** 게시판 목록 **<br>
<table border=1>
<tr>
<td>번호</td>
<td>제목</td>
<td>작성자</td>
<td>조회수</td>
<td>작성일</td>
</tr>
<%
int nowPage;
if(request.getParameter("nowPage")==null)
nowPage=1;
else
nowPage=Integer.parseInt(request.getParameter("nowPage"));
//검색 컬럼값 추출, null --> ""
String col = Utility.checkNull(request.getParameter("col"));
//검색어 추출, null --> ""
String word = Utility.checkNull(request.getParameter("word"));
ArrayList list=bbsMgr.list(col,word,nowPage);
Iterator iter=list.iterator();
String date = Utility.getDate(); //오늘날짜
while(iter.hasNext()){
bbsDTO=(BbsDTO)iter.next();
int bbsno = bbsDTO.getBbsno();
String wname = bbsDTO.getWname();
String subject = bbsDTO.getSubject();
String regdate = (bbsDTO.getRegdate()).substring(0, 10);
int indent = bbsDTO.getIndent();
int cnt = bbsDTO.getCnt();
%>
<tr>
<td><%=bbsno%></td>
<td align="left">
<%
String dto_date = bbsDTO.getRegdate();
//답변여부 출력
if (indent > 0) {
//답변이라면 들여쓰기를 합니다.
for (int re = 0; re < indent; re++) {
out.print(" ");
}
//이미지를 출력합니다.
out.print("<img src='./images/reply.jpg'>");
}
%>
<a href="./bbsRead.jsp?nowPage=<%=nowPage%>&bbsno=<%=bbsno%>"><%=subject%></a>
<%
if (dto_date.substring(0, 10).equals(date)){
out.print("<img src='./images/new.gif' border=0> ");
}
%>
</td>
<td><%=wname%></td>
<td><%=cnt%></td>
<td><%=regdate%></td>
</tr>
<%
}
%>
</table>
<%
//페이지 리스트------------------------------------------------------
int pageNum=nowPage;
int totalCount=bbsMgr.recordCount();
int totalPage=(totalCount-1)/5+1;
int start_page=(pageNum-1)/5*5+1;
if(start_page>5){
%>
<a href="./bbsList.jsp?nowPage=<%=start_page-1%>">back</a>
<%
}
for(int i=start_page;i<start_page+5;i++){
if(i<=totalPage){
if(i==nowPage)
out.print("<strong> "+i+" </strong>");
else
out.print(" <a href='./bbsList.jsp?nowPage="+i+"'>"+i+"</a> ");
}
}
if(totalPage>=start_page+5){
%>
<a href="./bbsList.jsp?nowPage=<%=start_page+5%>">next</a>
<%
}
//------------------------------------------------------------
%>
<br><br>
현재페이지 : <%=nowPage%> / 전체글갯수 : <%=totalCount %>
<br><br>
<form method="post" action="bbsList.jsp">
<select name="col">
<option value="subject" selected>제목
<option value="content">내용
<option value="wname">작성자
</select>
<input type="text" name="word">
<input type="submit" value="검색">
</form>
<br>
<a href="./bbsForm.jsp">[게시판 등록]</a>
<br>
</center>
</body>
</html>
-----------------------------------------------------
'..열심히 공부하세.. > JSP' 카테고리의 다른 글
[25] 파일업로드 테이블 설계 (0) | 2012.03.05 |
---|---|
[24] 파일업로드 테스트 (0) | 2012.03.05 |
[22] 답변게시판 Manager Class (목록,추가,상세보기) (0) | 2012.02.29 |
[21] Manager Class (ConnectionMgr.java) (0) | 2012.02.27 |
[유틸리티] Utility.java (0) | 2012.02.27 |