본문 바로가기

Java

[jdbc] 게시판 만들기 (VO, DAO, Main)

[BoardVO]

package kr.co.mlec.vo;

public class BoardVO {
	private int 	no;
	private String 	title;
	private String 	content;
	private int 	viewCnt;
	private String 	regDate;

	public BoardVO() {
		super();
	}

	public BoardVO(int no, String title, String content, int viewCnt, String regDate) {
		super();
		this.no = no;
		this.title = title;
		this.content = content;
		this.viewCnt = viewCnt;
		this.regDate = regDate;
	}

	public int getNo() {
		return no;
	}

	public void setNo(int 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 int getViewCnt() {
		return viewCnt;
	}

	public void setViewCnt(int viewCnt) {
		this.viewCnt = viewCnt;
	}

	public String getRegDate() {
		return regDate;
	}

	public void setRegDate(String regDate) {
		this.regDate = regDate;
	}

	@Override
	public String toString() {
		return "BoardVO [no=" + no + ", title=" + title + ", content=" + content + ", viewCnt=" + viewCnt + ", regDate="
				+ regDate + "]";
	}
	
}

 

[BoardDAO]

package kr.co.mlec.dao;

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

import kr.ac.mlec.util.ConnectionFactory;
import kr.ac.mlec.util.JDBCClose;
import kr.co.mlec.vo.BoardVO;

/**
 * 오라클 database의 tbl_board를 CRUD하기 위한 클래스
 */
public class BoardDAO {
	private Connection conn;
	private PreparedStatement pstmt;
	
	/**
	 * tbl_board에서 게시물 번호에 해당 게시물 조회 메소드
	 */
	public BoardVO selectByNo(int boardNo) {
		BoardVO board = null;
		try {
			conn = new ConnectionFactory().getConnection();
			
			StringBuilder sql = new StringBuilder();
			sql.append("select no, title, content, view_cnt, reg_date ");
			sql.append("  from tbl_board ");
			sql.append(" where no = ? ");
			
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setInt(1, boardNo);
			
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()) {
				int 	no 		= rs.getInt("no");
				String 	title 	= rs.getString("title");
				String 	content = rs.getString("content");
				int 	viewCnt = rs.getInt("view_cnt");
				String 	regDate = rs.getString("reg_date");
				
				board = new BoardVO(no, title, content, viewCnt, regDate);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCClose.close(conn, pstmt);
		}
		
		return board;
	}
	
	/**
	 * tbl_board의 전체게시글을 조회하는 메소드
	 */
	public List<BoardVO> selectAll() {
		
		List<BoardVO> boardList = new ArrayList<BoardVO>();
		
		try {
			conn = new ConnectionFactory().getConnection();
			
			StringBuilder sql = new StringBuilder();
			sql.append("select no, title, content, view_cnt, reg_date ");
			sql.append("  from tbl_board ");
			sql.append(" order by no desc ");
			
			pstmt = conn.prepareStatement(sql.toString());
			
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()) {
				int 	no 		= rs.getInt("no");
				String 	title 	= rs.getString("title");
				String 	content = rs.getString("content");
				int 	viewCnt = rs.getInt("view_cnt");
				String 	regDate = rs.getString("reg_date");
				
				BoardVO board = new BoardVO(no, title, content, viewCnt, regDate);
				
				boardList.add(board);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCClose.close(conn, pstmt);
		}
		
		return boardList;
	}

	/**
	 * tbl_board 게시글을 등록(insert)하는 메소드
	 */
	public void insert(BoardVO board) {
				
		try {
			conn = new ConnectionFactory().getConnection();

			StringBuilder sql = new StringBuilder();
			sql.append("insert into tbl_board(no, title, content) ");
			sql.append(" values(seq_tbl_board_no.nextval, ?, ?) ");

			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setString(1, board.getTitle());
			pstmt.setString(2, board.getContent());
			
			pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCClose.close(conn, pstmt);
		}
	}
	
	/**
	 * tbl_board 게시글을 수정하는 메소드
	 */
	public void update() {
	}
}


 

[Board Main]

package kr.co.mlec.dao;

import java.util.Scanner;

import kr.co.mlec.vo.BoardVO;

public class BoardMain {

	public static void main(String[] args) {
		
		/*
		 	조회할 게시물 번호 입력 : 3
		 	3번 게시물을 없습니다
		 	or
		 	게시물번호 : 3
		 	제목 : XXXX
		 	내용 : XXXX
		 	조회수 : X
		 	등록일 : xxxx-xx-xx
		 	selectByNo()
		 */
		
		
		Scanner sc = new Scanner(System.in);
		System.out.print("조회할 게시물 번호 입력 : ");
		int no = sc.nextInt();
		
		BoardDAO dao = new BoardDAO();
		BoardVO board = dao.selectByNo(no); 
		if(board == null) {
			System.out.println(no + "번 게시물이 없습니다");
		} else {
			System.out.println("번  호 : " + board.getNo());
			System.out.println("제  목 : " + board.getTitle());
			System.out.println("내  용 : " +  board.getContent());
			System.out.println("조회수 : " + board.getViewCnt());
			System.out.println("등록일 : " + board.getRegDate());
		}
		
		/*
		BoardDAO dao = new BoardDAO();
		List<BoardVO> boardList = dao.selectAll();
		System.out.println("조회된 게시물수 : " + boardList.size() + "개");
		
		for(BoardVO b : boardList) {
			System.out.println(b);
		}
		*/
		
		/*
		BoardDAO dao = new BoardDAO();
		
		BoardVO board = new BoardVO();
		board.setTitle("안녕하세요");
		board.setContent("가입인사합니다");
		
		dao.insert(board);
		System.out.println("삽입완료...");
		*/
	}
}




 

 

 

 

'Java' 카테고리의 다른 글

[Chapter07-1] 객체지향 프로그래밍1  (0) 2021.12.06
[Session] 세션 시간 및 연장 설정  (0) 2021.05.12
[간단한 for문] list  (0) 2021.04.30
[jdbc] update 자바 오라클 연동  (0) 2021.04.28
[jdbc] insert 자바 오라클 연동  (0) 2021.04.28