본문 바로가기
프로젝트/JSP 게시판 만들기

JSP 게시판 만들기 5

by 리드민 2022. 8. 28.
반응형

[1] 개발 환경

hardware
cpu : intel i7-5820k, gpu : nvidia gtx 970, ram : ddr4 16GB, mainboard : x99a chipset 2011V3 board, secondary storage : 1TB SSD, 4TB HDD
OS
windows pro 64bits

 

software
jdk version 1.8.0_301
eclipse version 2022-06 (4.24.0)
apache-tomcat version 9.0.54
oracle Database 18c

 

[2] 목적

  이클립스 프로그램, oracle Database, apache-tomcat을 이용해서 게시판을 만드는 것을 목적으로 한다. 사용 언어는 html, css, java, javascript이다. sql 쿼리문도 사용한다. 과정이 길기 때문에 시리즈로 쓸 예정이다.

 

[3] 이 게시물에서 하는 것

  DTO class 파일 설정, DAO class 파일 설정

 

[4] 프로그램 코드

 

() DAO 파일

  Data Access Object 데이터베이스의 data에 접근하기 위한 객체. DataBase에 접근 하기 위한 로직 & 비지니스 로직을 분리하기 위해 사용. 서비스와 DB를 연결한다. 직접 DB에 접근하여 

위치 : project > src/main/java > com.jsp.board > BoardDAO.java

package com.jsp.board;

import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;

public class BoardDAO {
	
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;
	
	private DataSource ds;
	
	private BoardDAO() {
		try {
			
			Context context = new InitialContext();
			ds = (DataSource)context.lookup("java:comp/env/jdbc/oracle");
			
		}catch(Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private static BoardDAO instance = new BoardDAO();
	
	public static BoardDAO getInstance() {
		return instance;
	}
	
	
	private void close(Connection conn) {
		try {
			if(conn != null) {
				conn.close();
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private void close(PreparedStatement pstmt) {
		try {
			if(pstmt != null) {
				pstmt.close();
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	private void close(ResultSet rs) {
		try {
			if(rs != null) {
				rs.close();
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	
	// 게시판 목록을 보여주기 위한 메소드
	public ArrayList<BoardDTO> boardList(){
		
		ArrayList<BoardDTO> list = new ArrayList<BoardDTO>();
		
		String query = "select * from board order by bId DESC";
		// bId를 내림차순으로 정렬
		
		try {
			
			conn = ds.getConnection();
			pstmt = conn.prepareStatement(query);
			
			rs = pstmt.executeQuery();
			
			while(rs.next()) {
				
				int bId = rs.getInt("bId");
				String bName = rs.getString("bName");
				String bTitle = rs.getString("bTitle");
				String bContent = rs.getString("bContent");
				Timestamp bDate = rs.getTimestamp("bDate");
				int bHit = rs.getInt("bHit");
				
				list.add(new BoardDTO(bId, bName, bTitle, bContent, bDate, bHit));
				
			}
			
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			
			close(rs);
			close(pstmt);
			close(conn);
			
		}
		
		
		return list;
	}
	
	// 글 작성 후 데이터를 넣는 메소드
	public int write(String bName, String bTitle, String bContent) {
		int result = 0;
		
		String query = "insert into board(bId, bName, bTitle, bContent) "
				+ "values(board_seq.nextval, ?, ?, ?)";
		
		try {
			
			conn = ds.getConnection();
			pstmt = conn.prepareStatement(query);
			
			pstmt.setString(1, bName);
			pstmt.setString(2, bTitle);
			pstmt.setString(3, bContent);
			
			result = pstmt.executeUpdate();
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			
			close(pstmt);
			close(conn);
			
		}
		
		return result;
	}
	
	// 리스트에 제목 클릭 시 해당 내용을 보여주기 위한 메소드
	public BoardDTO contentView(String strID) {
		BoardDTO dto = null;
		upHit(strID);
		
		String query = "select * from board where bId = ?";
		
		try {
			
			conn = ds.getConnection();
			pstmt = conn.prepareStatement(query);
			
			pstmt.setInt(1, Integer.parseInt(strID));
			
			rs = pstmt.executeQuery();
			
			if(rs.next()) {
				
				int bId = rs.getInt("bId");
				String bName = rs.getString("bName");
				String bTitle = rs.getString("bTitle");
				String bContent = rs.getString("bContent");
				Timestamp bDate = rs.getTimestamp("bDate");
				int bHit = rs.getInt("bHit");
				
				dto = new BoardDTO(bId, bName, bTitle, bContent, bDate, bHit);
				
			}
			
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			
			close(rs);
			close(pstmt);
			close(conn);
			
		}
		
		return dto;
	}


	// 조회수 증가시켜주는 메소드 (bHit 하나 올려주는 메소드)
	private void upHit(String strID) {
		
		String query = "update board set bHit = bHit + 1 where bId = ?";
		
		try {
			
			conn = ds.getConnection();
			pstmt = conn.prepareStatement(query);
			pstmt.setInt(1, Integer.parseInt(strID));
			
			pstmt.executeUpdate();
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			
			close(pstmt);
			close(conn);
			
		}
		
		
	}
	
	
	// 해당글을 수정 해주는 메소드
	public int modify(String strID, String bName, String bTitle, String bContent) {
		int result = 0;
		
		String query = "update board set bName = ?, "
				+ "bTitle = ?, bContent = ? where bId = ?";
		
		try {
			
			conn = ds.getConnection();
			pstmt = conn.prepareStatement(query);
			
			pstmt.setString(1, bName);
			pstmt.setString(2, bTitle);
			pstmt.setString(3, bContent);
			pstmt.setInt(4, Integer.parseInt(strID));
			
			result = pstmt.executeUpdate();
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			
			close(pstmt);
			close(conn);
			
		}
		
		
		return result;
	}
	
	// 해당글을 삭제 해주는 메소드
	public int delete(String strID) {
		int result = 0;
		
		String query = "delete from board where bId = ?";
		
		try {
			
			conn = ds.getConnection();
			pstmt = conn.prepareStatement(query);
			pstmt.setInt(1, Integer.parseInt(strID));
			
			result = pstmt.executeUpdate();
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			
			close(pstmt);
			close(conn);
			
		}
		
		return result;
	}
	
	
}

 

 

() DTO 파일

project > src/main/java > com.jsp.board > BoardDTO.java

자바 클래스 파일을 생성해야 한다.

 

package com.jsp.board;

import java.sql.Timestamp;

public class BoardDTO {
	
	private int bId;
	private String bName;
	private String bTitle;
	private String bContent;
	private Timestamp bDate;
	private int bHit;
	
	public BoardDTO() {}

	public BoardDTO(int bId, String bName, String bTitle, String bContent, Timestamp bDate, int bHit) {
		super();
		this.bId = bId;
		this.bName = bName;
		this.bTitle = bTitle;
		this.bContent = bContent;
		this.bDate = bDate;
		this.bHit = bHit;
	}

	public int getbId() {
		return bId;
	}

	public void setbId(int bId) {
		this.bId = bId;
	}

	public String getbName() {
		return bName;
	}

	public void setbName(String bName) {
		this.bName = bName;
	}

	public String getbTitle() {
		return bTitle;
	}

	public void setbTitle(String bTitle) {
		this.bTitle = bTitle;
	}

	public String getbContent() {
		return bContent;
	}

	public void setbContent(String bContent) {
		this.bContent = bContent;
	}

	public Timestamp getbDate() {
		return bDate;
	}

	public void setbDate(Timestamp bDate) {
		this.bDate = bDate;
	}

	public int getbHit() {
		return bHit;
	}

	public void setbHit(int bHit) {
		this.bHit = bHit;
	}
	
}

 

 

반응형

'프로젝트 > JSP 게시판 만들기' 카테고리의 다른 글

JSP 게시판 만들기 7  (0) 2022.08.30
JSP 게시판 만들기 6  (0) 2022.08.28
JSP 게시판 만들기 4  (0) 2022.08.24
JSP 게시판 만들기 3  (0) 2022.08.20
JSP 게시판 만들기 2  (0) 2022.08.20