[SPRING] 게시판만들기 7 [ Spring MVC 적용-복습하기]

🐷Jinie (juniorDeveloper)·2020년 12월 1일
0

JSP/MVC/SPRING

목록 보기
57/81

1. Spring MVC 과정 살펴보기



Test하기



2. DAO / Service / JDBCUtill / DTO(VO)

1. BoardVO

package com.springbook.biz.common;

import java.sql.Date;

public class BoardVO {
	private  int seq ;
	private String  title;
	private String  writer;
	private String content;
	private  Date  regDate;
	private  int  cnt;
	private int mainSeq;
	private String ch1;
	private String ch2;
	public int getSeq() {
		return seq;
	}
	public void setSeq(int seq) {
		this.seq = seq;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getWriter() {
		return writer;
	}
	public void setWriter(String writer) {
		this.writer = writer;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public Date getRegDate() {
		return regDate;
	}
	public void setRegDate(Date regDate) {
		this.regDate = regDate;
	}
	public int getCnt() {
		return cnt;
	}
	public void setCnt(int cnt) {
		this.cnt = cnt;
	}
	public int getMainSeq() {
		return mainSeq;
	}
	public void setMainSeq(int mainSeq) {
		this.mainSeq = mainSeq;
	}
	public String getCh1() {
		return ch1;
	}
	public void setCh1(String ch1) {
		this.ch1 = ch1;
	}
	public String getCh2() {
		return ch2;
	}
	public void setCh2(String ch2) {
		this.ch2 = ch2;
	}
	@Override
	public String toString() {
		return "BoardVO [seq=" + seq + ", title=" + title + ", writer=" + writer + ", content=" + content + ", regDate="
				+ regDate + ", cnt=" + cnt + ", mainSeq=" + mainSeq + ", ch1=" + ch1 + ", ch2=" + ch2 + "]";
	}
	
	
}

2. JDBCUtill

package com.springbook.k1.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCUtill {
	private  static	JDBCUtill   db = new JDBCUtill() ;   
	   private JDBCUtill(){}   
	   public  static JDBCUtill getInstance(){
		   return  db;
	   }
	   public Connection getConnection(){
		  Connection conn =null;
		  try {
			  Class.forName("oracle.jdbc.OracleDriver");
			  conn = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/xe","system","1234");
		  } catch( Exception  e) {
			  e.printStackTrace();
		  }	  
		  	return conn;	   
	   }

public  static void  close( PreparedStatement pstmt , Connection conn) {
	    	if(pstmt != null ) {
	    		try {
					if (!pstmt.isClosed()) pstmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					pstmt =null ;
				}
	    	}
	      	if(conn != null ) {
	    		try {
					if (!conn.isClosed()) conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					conn =null ;
				}
	    	}      	
}

public  static void  close(ResultSet rs ,PreparedStatement pstmt , Connection conn) {
		  	if(rs != null ) {
				try {
					if (!rs.isClosed()) rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					rs =null ;
				}
			}
		   
	   	if(pstmt != null ) {
			try {
				if (!pstmt.isClosed()) pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				pstmt =null ;
			}
		}
		
	  	if(conn != null ) {
			try {
				if (!conn.isClosed()) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				conn =null ;
			}
		}  		   
}

}

3. BoardService

package com.springbook.biz.service;

import java.util.List;

import com.springbook.biz.common.BoardVO;

public interface BoardService {
	void insert(BoardVO vo);
	void delete(int seq);
	void update(BoardVO vo);
	List<BoardVO> boardList(BoardVO vo);
	BoardVO content(BoardVO vo);
	void cnt(int seq);
}

4. BoardDAO

package com.springbook.biz.dao;

import java.util.List;

import com.springbook.biz.common.BoardVO;

public interface BoardDAO {
	void insert(BoardVO vo);
	void delete(int seq);
	void update(BoardVO vo);
	List<BoardVO> boardList(BoardVO vo);
	BoardVO content(BoardVO vo);
	void cnt(int seq);
}

5. BoardServiceImpl

package com.springbook.biz.service;

import java.util.List;

import com.springbook.biz.common.BoardVO;
import com.springbook.biz.dao.BoardDAO;
import com.springbook.biz.dao.BoardDAOImpl;

public class BoardServiceImpl implements BoardService{
	BoardDAO dao = new BoardDAOImpl();
	@Override
	public void insert(BoardVO vo) {
		dao.insert(vo);
	}

	@Override
	public void delete(int seq) {
		dao.delete(seq);
	}

	@Override
	public void update(BoardVO vo) {
		dao.update(vo);
	}

	@Override
	public List<BoardVO> boardList(BoardVO vo) {
		return dao.boardList(vo);
	}

	@Override
	public BoardVO content(BoardVO vo) {
		return dao.content(vo);
	}

	@Override
	public void cnt(int seq) {
		dao.cnt(seq);
	}

}

6. BoardDAOImpl

package com.springbook.biz.dao;

import java.sql.*;
import java.util.*;
import com.springbook.biz.common.BoardVO;
import com.springbook.biz.common.JDBCUtill;

public class BoardDAOImpl implements BoardDAO{
	JDBCUtill db = JDBCUtill.getInstance();
	Connection con = null;
	PreparedStatement pstmt = null;
	String insert_sql = "insert into board (seq, title, writer, content) values ((select max(seq) from board)+1 ,?,?,?) ";
	String select_sql = "select * from board order by seq desc ";
	String delete_sql = "delete from board where seq = ? ";
	String select_sqlOne = "select * from board where seq = ? order by seq desc";
	String update_sql = "update board set title=?, writer=?, content=? where seq=? ";
	String cnt_sql = "update board set cnt=cnt+1 where seq=? ";
	ResultSet rs = null;
	List<BoardVO> boardList = null;
	@Override
	public void insert(BoardVO vo) {
		try {
			con = db.getConnection();
			pstmt = con.prepareStatement(insert_sql);
			pstmt.setString(1, vo.getTitle());
			pstmt.setString(2, vo.getWriter());
			pstmt.setString(3, vo.getContent());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtill.close(pstmt, con);
		}
	}
	@Override
	public void delete(int seq) {
		try {
			con = db.getConnection();
			pstmt = con.prepareStatement(delete_sql);
			pstmt.setInt(1, seq);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtill.close(pstmt, con);
		}	
	}

	@Override
	public void update(BoardVO vo) {
		try {
			con = db.getConnection();
			pstmt = con.prepareStatement(update_sql);
			pstmt.setString(1, vo.getTitle());
			pstmt.setString(2, vo.getWriter());
			pstmt.setString(3, vo.getContent());
			pstmt.setInt(4, vo.getSeq());
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtill.close(pstmt, con);
		}
	}

	@Override
	public List<BoardVO> boardList(BoardVO vo) {
		try {
			con = db.getConnection();
		if(vo.getCh1()==null||vo.getCh1()=="") {
			pstmt = con.prepareStatement(select_sql);				
		}else {
			String ch1 = vo.getCh1();
			String ch2 = vo.getCh2();
			String select_sqlSearch = "select * from board where " + ch1 + " like '%'||?||'%' order by seq desc ";
			pstmt = con.prepareStatement(select_sqlSearch);	
			pstmt.setString(1, ch2);
		}
		rs = pstmt.executeQuery();
		boardList = new ArrayList<BoardVO>();
		while(rs.next()) {
			vo = new BoardVO();
			vo.setSeq(rs.getInt("seq"));
			vo.setTitle(rs.getString("title"));
			vo.setWriter(rs.getString("writer"));
			vo.setContent(rs.getString("content"));
			vo.setRegDate(rs.getDate("regdate"));
			vo.setCnt(rs.getInt("cnt"));
			boardList.add(vo);
		}
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		JDBCUtill.close(rs, pstmt, con);
	}
	return boardList;
	}

	@Override
	public BoardVO content(BoardVO vo) {
		try {
			con = db.getConnection();
			pstmt = con.prepareStatement(select_sqlOne);
			pstmt.setInt(1, vo.getSeq());
			rs = pstmt.executeQuery();
			rs.next();
			vo.setSeq(rs.getInt("seq"));
			vo.setTitle(rs.getString("title"));
			vo.setWriter(rs.getString("writer"));
			vo.setContent(rs.getString("content"));
			vo.setRegDate(rs.getDate("regdate"));
			vo.setCnt(rs.getInt("cnt"));
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		JDBCUtill.close(rs, pstmt, con);
	}
	return vo;
	}

	@Override
	public void cnt(int seq) {
		try {
			con = db.getConnection();
			pstmt = con.prepareStatement(cnt_sql);
			pstmt.setInt(1, seq);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtill.close(pstmt, con);
		}
	}
}

3. 파일연결해서 홈페이지 구현하기

1번째. index

2번째. 입력하기(insert)


3번째. 목록보기상태에서 삭제하기 (select, delete)


4번째. 글 수정하기, 상세보기하면 조회수 올리기 (update)



5번째. 검색하기(select)

profile
ᴘᴇᴛɪᴛs ᴅᴇ́ᴠᴇʟᴏᴘᴘᴇᴜʀ. ᴘʀᴏɢʀᴀᴍᴍᴀᴛɪᴏɴ = ᴘʟᴀɪsɪʀ 💕

4개의 댓글

comment-user-thumbnail
2020년 12월 1일

그림판 강좌는 언제올라오나요?

1개의 답글
comment-user-thumbnail
2020년 12월 1일

무셔웡..

1개의 답글