티스토리 뷰

1. 자바

control-model-jdbc BoardDao.java

패스트코드블로그 2020. 5. 24. 14:04
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
58
59
60
61
62
63
64
65
66
67
68
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
 
import org.springframework.stereotype.Repository;
 
 
import com.springbook.biz.board.BoardVO;
import com.springbook.biz.common.JDBCUtil;
import com.springbook.biz.user.UserVO;
 
public class BoardDAO {
    private Connection conn = null;
    private PreparedStatement stmt = null;
    private ResultSet rs = null;
    private final String CREATE_BOARD = " CREATE TABLE BOARD ("
            + " SEQ INT(10) AUTO_INCREMENT PRIMARY KEY, TITLE VARCHAR(20), WRITER VARCHAR(20), CONTENT VARCHAR(50))";
    private final String BOARD_INSERT = "INSERT INTO BOARD(TITLE, WRITER, CONTENT) VALUES(?,?,?)";
    private final String BOARD_UPDATE = "UPDATE BOARD SET TITLE=?, CONTENT=? WHERE SEQ=?";
    private final String BOARD_DELETE = "DELETE FROM BOARD WHERE SEQ=?";
    private final String BOARD_GET = "SELECT * FROM BOARD WHERE SEQ=?";
    private final String BOARD_LIST = "SELECT * FROM BOARD ORDER BY SEQ DESC";
    
    public void insertBoard(BoardVO vo) {
        System.out.println("===> JDBC insertBoard() ");
        conn = JDBCUtil.getConnection();
        try {
            stmt = conn.prepareStatement(BOARD_INSERT);
            stmt.setString(1, vo.getTitle());
            stmt.setString(2, vo.getWriter());
            stmt.setString(3, vo.getContent());
            stmt.execute();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            JDBCUtil.close(stmt, conn);
        }
        
    }
    public List<BoardVO> getBoardList() {
        BoardVO board = null;
        List<BoardVO> boardList = new ArrayList<BoardVO>();
        try {
            System.out.println("===> JDBC로 getBoardList() 기능 처리");
            conn = JDBCUtil.getConnection();
            stmt = conn.prepareStatement(BOARD_LIST);
            
            rs = stmt.executeQuery();
            while(rs.next()) {
                board = new BoardVO();
                board.setTitle(rs.getString("TITLE"));
                board.setWriter(rs.getString("WRITER"));
                board.setContent(rs.getString("CONTENT"));
                boardList.add(board);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return boardList;
        
    }
}
cs
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
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
글 보관함