본문 바로가기
JSP 웹 프로젝트

JSP 웹 프로젝트 (5) 게시판 글 작성

by s_hoonee 2023. 1. 3.
반응형

대학생을 위한 웹 페이지 제작

사용 언어 : java (jdk 12.0.2), JavaScript, css, html
사용 툴 : Eclipse
데이터베이스 : Mysql
서버 : Tomcat
테스트 브라우저 : Chrome, Edge
파일 : jsp

프로젝트로 만든 대학생을 위한 학교 웹 페이지입니다. 

기능

1. 자유 게시판, 중고책, 분실물 게시판 등 운영 ( 이미지 첨부, 댓글 달기, 수정 등 가능 )

2. 학교 공지사항 게시

3. 기타 대외활동 포스터 게시

4. 학점 계산기, 자신만의 시간표, 건물 정보, 주차 구역 확인

5. 회원가입시 AES 암호화 + salt 값을 추가하여 비밀번호를 해쉬값으로 저장하여 암호화

6. 카카오 지도 API

 

시스템 구성도


글 작성 셋팅

연동된 DB에 해당 컬럼들을 넣어주고 변수 선언, Getter, Setter 세팅 해줍니다.

단축키
Alt + Shift + S -> Generate Getter and Setter -> 필드 선택(select all) -> enter

post.java

public class Post {
	private int boardID;  //게시판 번호
    private int postID; //게시물 번호
	private String postTitle; //게시물 제목
	private String userID; //작성자 id
	private String postDate; // 작성 날짜
	private String postContent; //게시물 내용
	private int postAvailable; // 게시물 삭제 여부
    
	public int getBoardID() { 
		return boardID;
	}
	public void setBoardID(int boardID) {
		this.boardID = boardID;
	}
	public int getPostID() {
		return postID;
	}
	public void setPostID(int postID) {
		this.postID = postID;
	}
	public String getPostTitle() {
		return postTitle;
	}
	public void setPostTitle(String postTitle) {
		this.postTitle = postTitle;
	}
	public String getUserID() {
		return userID;
	}
	public void setUserID(String userID) {
		this.userID = userID;
	}
	public String getPostDate() {
		return postDate;
	}
	public void setPostDate(String postDate) {
		this.postDate = postDate;
	}
	public String getPostContent() {
		return postContent;
	}
	public void setPostContent(String postContent) {
		this.postContent = postContent;
	}
	public int getPostAvailable() {
		return postAvailable;
	}
	public void setPostAvailable(int postAvailable) {
		this.postAvailable = postAvailable;
	}
}

게시글 작성 기능 처리

getData(). getNext()로 각 각 현재 시간, 게시물 번호 +1 를 가져오고 최종적으로 write() 함수에서 입력한 값들로 데이터베이스에 게시물을 레코드 단위로 저장합니다.

PostDAO.java

public String getData() {  //현재 날짜 값을 가져옴
		String SQL = "select NOW()";
		try {
			 PreparedStatement pstmt = conn.prepareStatement(SQL);
			 rs = pstmt.executeQuery();
			 if(rs.next()) {
				 return rs.getString(1);
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ""; 
	}
	
	public int getNext() { // 작성된 마지막 게시물의 번호가 10이라면 11을 리턴하여 새로운 게시물 
                           // 번호를 +1 씩 증가시킴
		String SQL = "select postID from post order by postID DESC";
		try {
			 PreparedStatement pstmt = conn.prepareStatement(SQL);
			 rs = pstmt.executeQuery();
			 if(rs.next()) {
				 return rs.getInt(1)+1;
			 }
			 return 1; 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1; //
	}
	
	public int write(String postTitle, String userID, String postContent) {
		String SQL = "insert into post(postID, postTitle, userID, postDate, postContent,"
				+ "postAvailable, boardID) values(?, ?, ?, ?, ?,?,?)";
		try {
			 PreparedStatement pstmt = conn.prepareStatement(SQL);
			 pstmt.setInt(1, getNext()); //게시물 번호 최신화
			 pstmt.setString(2, postTitle);
			 pstmt.setString(3, userID);
			 pstmt.setString(4, getData());  //현재 시간
			 pstmt.setString(5, postContent);
			 pstmt.setInt(6, 1);
			 pstmt.setInt(7, 4);
			
			 return pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1; 
	}