본문 바로가기

개발/JSP

MySQL에서의 DAO 구현

(1) MySQL 테이블 생성 쿼리

D:\>mysqladmin -u root -p create chap13

D:\>mysql -u root -p

mysql> grant select, insert, update, delete, create, drop
-> on chap14.* to 'jernin20'@'localhost' identified by '******';

mysql> grant select, insert, update, delete, create, drop
-> on chap14.* to 'jernin20'@'$' identified by '******';

mysql> quit

D:\>mysql -u jernin20 -p chap13

mysql> create table guestbook_message (
-> message_id int not null auto_increment primary key,
-> guest_name varchar(50) not null,
-> password varchar(10) not null,
-> message text not null
-> );


(2) MySQLMessageDao 구현

chap13\WEB-INF\src\kame\chap13\dao\mysql\MySQLMessageDao.java
package kame.chap13.dao.mysql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import kame.chap13.dao.MessageDao;
import kame.chap13.model.Message;
import kame.jdbc.JdbcUtil;

public class MySQLMessageDao extends MessageDao {

	public int insert(Connection conn, Message message) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			pstmt = conn.prepareStatement( 
					"insert into guestbook_message " +
					"(guest_name, password, message) values (?, ?, ?)");
			pstmt.setString(1, message.getGuestName());
			pstmt.setString(2, message.getPassword());
			pstmt.setString(3, message.getMessage());
			return pstmt.executeUpdate();
		} finally {
			JdbcUtil.close(pstmt);
		}
	}

	public List<Message> selectList(Connection conn, int firstRow, int endRow)
			throws SQLException {
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			pstmt = conn.prepareStatement(
					"select * from guestbook_message " + 
					"order by message_id desc limit ?, ?");
			pstmt.setInt(1, firstRow - 1);
			pstmt.setInt(2, endRow - firstRow + 1);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				List<Message> messageList = new ArrayList<Message>();
				do {
					messageList.add(super.makeMessageFromResultSet(rs));
				} while (rs.next());
				return messageList;
			} else {
				return Collections.emptyList();
			}
		} finally {
			JdbcUtil.close(rs);
			JdbcUtil.close(pstmt);
		}
	}

}

(3) MySQL DBCP 설정 파일

chap13\WEB-INF\classes\guestbook.jocl
<object class="org.apache.commons.dbcp.PoolableConnectionFactory" 
		xmlns="http://apache.org/xml/xmlns/jakarta/commons/jocl">

	<object class="org.apache.commons.dbcp.DriverManagerConnectionFactory">
		<string value="jdbc:mysql://localhost:3306/chap13?useUnicode=true&amp;characterEncoding=euckr"/>
		<string value="jernin20"/>
		<string value="******"/>
	</object>
	
	<object class="org.apache.commons.pool.impl.GenericObjectPool">
		<object class="org.apache.commons.pool.PoolableObjectFactory" null="true" />
	</object>
	
	<object class="org.apache.commons.pool.KeyedObjectPoolFactory" null="true"/>
	
	<string null="true"/>
	
	<boolean value="false"/>
	
	<boolean value="true"/>
</object>

...