2013년 3월 18일 월요일

[JSP] 게시판 만들기!

게시판을 배워보는 것은 프로그래밍의 필수적인 요소를 모두 익힐 수 있는 좋은 과정이다.

그 이유는 게시물을 생성(Create), 읽기(Read), 수정(Update), 삭제(Delete) 하면서 데이터의 흐름을 이해하고 어떻게 데이터를 전달하고 저장하는지에 대해 전반적으로 알아갈 수 있기 때문이다. 프로그래밍 실력을 키울 수 있는 좋은 예인 것이다.

제작할 게시판에는 다음과 같은 기능을 추가할 것이다.
1. 답글
2. 페이지 처리
3. 검색
4. 파일 업/다운
5. 이미지 게시판

지금까지 배운 JSP를 위주로 구현해 나갈 것이며 EL, JSTL태그도 많이 사용할 것이다.

코드를 위주로 알아보자.
우선 게시판을 위한 DB 테이블이 필요하다.

create table board(
b_id number(5) primary key,
b_name varchar2(20),
b_title varchar2(50),
b_content varchar2(3000),
b_pwd varchar2(10),
b_date date,
b_hit number(5) default 0,
b_ref number(5),
b_step number(5),
b_level number(5),
b_parentid number(5)
)

보통 게시판에 글을 올릴 때 필요한 데이터를 확인할 수 있다. 그러나 ref, step, level, parent라는 컬럼에 대해서는 의아할 것이다. 이 컬럼들은 해당하는 글의 답글을 달기 위해 필요한 요소이다.

ref : 부모의 그룹번호
step : 부모의 글을 기준으로 순서 결정
level : 답글을 나타내는 들여쓰기를 위한 속성
parentid : 부모의 글 번호

다음으로 게시판에 삽입될 하나의 글 객체를 위한 클래스이다.
public class Board {
private int b_id;
private String b_name;
private String b_title;
private String b_content;
private String b_pwd;
private Timestamp b_date;
private int b_hit;
private int b_ref;
private int b_step;
private int b_level;
private int b_parentid;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
글 하나를 게시판에 삽입할 Dao클래스
//////////////////////////////////////////////////////////////////////////////////////////////////
public class BoardDao {
private static BoardDao dao = new BoardDao();

public static BoardDao getInstance() {
return dao;
}

public Connection getConnection() throws Exception {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/oracle");
return ds.getConnection();
}

public int insertBoard(Board board) {
int re = -1;
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "";
int number = 0;// 글번호를 저장

int b_id = board.getB_id();
int b_ref = board.getB_ref();
int b_step = board.getB_step();
int b_level = board.getB_level();
int b_parentid = board.getB_parentid();

try {
con = getConnection();
// 글번호를 미리 생성
sql = "select max(b_id) from board";
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();

if (rs.next()) {
number = rs.getInt(1) + 1;// 현재 작성한 글번호를 이전에 작성한 글에 +1을 해서 구한다
}
// 답변글일 때
if (b_id != 0) {
sql = "update board set b_step = b_step + 1 where b_ref = ? AND b_step > ?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, b_ref);
pstmt.setInt(2, b_step);
pstmt.executeUpdate();

b_step = b_step + 1;// 나의 b_step
b_level = b_level + 1;// 나의 b_level
b_parentid = b_id;// 나의 b_parentid
} else {
// 원본글일 때
b_ref = number;
b_step = 0;
b_level = 0;
b_parentid = 0;
}

sql = "insert into board values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, number);
pstmt.setString(2, board.getB_name());
pstmt.setString(3, board.getB_title());
pstmt.setString(4, board.getB_content());
pstmt.setString(5, board.getB_pwd());
pstmt.setTimestamp(6, board.getB_date());
pstmt.setInt(7, board.getB_hit());
pstmt.setInt(8, b_ref);
pstmt.setInt(9, b_step);
pstmt.setInt(10, b_level);
pstmt.setInt(11, b_parentid);

re = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return re;
}

게시물 등록 메서드이다. 나머지는 조금씩의 수정이 필요.

댓글 없음:

댓글 쓰기