1. 데이터베이스 스키마 (MySQL 5.x)

-- 기본형 게시판 스키마
DROP TABLE IF EXISTS bboard;
CREATE TABLE bboard (
    no SERIAL, -- 글번호
    title VARCHAR(100) NOT NULL, -- 제목
    content LONGTEXT NOT NULL, -- 본문
    writer VARCHAR(50) NOT NULL, -- 글 올린 이
    wtime DATETIME NOT NULL, -- 글 쓴 시간
    PRIMARY KEY (no)
) ENGINE MyISAM;

2. 모델 설계 (POJO)

1) 데이터빈

package net.jeongsam.models.bboard;

import java.sql.Timestamp;

public class BasicBoardDataBean {
	private long no;
	private String title;
	private String content;
	private String writer;
	private Timestamp wtime;
	
	public long getNo() {
		return no;
	}
	public void setNo(long no) {
		this.no = no;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getWriter() {
		return writer;
	}
	public void setWriter(String writer) {
		this.writer = writer;
	}
	public Timestamp getWtime() {
		return wtime;
	}
	public void setWtime(Timestamp wtime) {
		this.wtime = wtime;
	}
}

2) 데이터 액세스 객체(DAO)

package net.jeongsam.models.bboard;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * 기본형 게시판 Data Access Object
 * @author 정승용
 * 2009.12.03
 */
public class BasicBoardDAO {
	private static final String _INSERT = "INSERT INTO bboard "
		+ "(title, content, writer, wtime) "
		+ "VALUES (?, ?, ?, NOW())"; 
	
	private Connection _getConnection() {
		Connection conn = null;
		DataSource ds = null;
		
		try {
			Context envCtx = new InitialContext();
			ds = (DataSource)envCtx.lookup("java:/comp/env/jdbc/MySamples"); 
			conn = ds.getConnection();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return conn;
	}
	
	/**
	 * 글 쓰기 메서드
	 * 노트: 컬럼 타입을 LONGTEXT로 설정하면 264byte의 글자를 입력할 수 있는데 이때 
	 *     com.mysql.jdbc.PacketTooBigException:
	 *     Packet for query is too large (1048587 > 1048576).
	 *     라는 오류가 발생할 수 있다.
	 *     이를 해결하기 위해 my.ini 설정 파일에 다음과 같이 설정한다.
	 *     max_allowed_packet=1073741824
	 *     위와 같이 설정하면 JDBC를 통해 최대 1GB의 데이터를 전송할 수 있게 된다.
	 * @param bbRow BasicBoardDataBean
	 * @throws SQLException 글 등록 에러
	 */
	public void insert(BasicBoardDataBean bbRow) throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		conn = _getConnection();
		
		try {
			pstmt = conn.prepareStatement(_INSERT);
			pstmt.setString(1, bbRow.getTitle());
			pstmt.setString(2, bbRow.getContent());
			pstmt.setString(3, bbRow.getWriter());
			
			pstmt.executeUpdate();
		} finally {
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			pstmt = null;
			conn = null;
		}	
	}
}

3. 입력 양식과 처리

1) 입력 양식 (insert.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
	"http://www.w3.org/TR/html4/loose.dtd">
<html lang="ko">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>글쓰기</title>
</head>
<body>
<h1>글쓰기</h1>
<form method="post" action="insertAction.jsp">
<table>
	<tr>
		<th>제목:</th>
		<td><input type="text" name="title" /></td>
	</tr>
	<tr>
		<th>글쓴이:</th>
		<td><input type="text" name="writer" /></td>
	</tr>
	<tr>
		<th>내용:</th>
		<td><textarea name="content" cols="50" rows="20"></textarea></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" value="글올리기" /></td>
	</tr>
</table>
</form>
</body>
</html>

3) 처리 (inputAction.jsp)

<%-- 스크립틀릿과 표준액션을 사용한 모델1 --%>

<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.sql.SQLException" %>
<%@ page import="net.jeongsam.models.bboard.BasicBoardDAO" %>

<% request.setCharacterEncoding("UTF-8"); %>

<jsp:useBean id="boardData"
	class="net.jeongsam.models.bboard.BasicBoardDataBean"
	scope="request">
	<jsp:setProperty name="boardData" property="*"/>
</jsp:useBean>

<%
BasicBoardDAO boardMgr = new BasicBoardDAO();
try {
	boardMgr.insert(boardData);
} catch(SQLException e) {
	e.printStackTrace();
}
out.println("글 등록 성공");
%>

+ Recent posts