프로그래밍/MVC pattern

방명록 구현을 통한 MVC 패턴 이해하기 (2)

Jay Tech 2017. 1. 16. 18:57
반응형

MySql에서 DAO를 만들어 보자.



1. 테이블 생성



MySql에서 테이블을 생성하자




auto_increment는 자동으로 값이 1씩 증가되는 칼럼으로서 insert쿼리를 수행할 때 값을 지정하지 않는경우 자동으로 1이 증가된 값이 삽입된다.



2. 메세지를 관리하는 DAO클래스 작성


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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 mvjsp.chap13.dao.MessageDao;
import mvjsp.chap13.model.Message;
import mvjsp.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);
        }
    }
 
}
 
cs



MySQL의 limit구문은 지정한 행부터 지정한 개수만큼 행을 가져 올 수 있다.


select * from guestbook_message limit [시작행번호], [읽어올개수]



38,39 line - 시작행번호는 0 이므로 1을 뺀다.



3. DBCP 설정 (MySQL)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<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="park"/>
        <string value="park"/>
    </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>
cs










반응형