기록해! 정리해!

Model 1 아키텍처로 게시판 개발 본문

Spring

Model 1 아키텍처로 게시판 개발

zsuling 2022. 9. 23. 16:37

BoardWeb.zip
0.07MB

1. 로그인 창

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<br><br>
<h1> 로그인 </h1>
<hr />

<form action=login_proc.jsp  method=post>
  <table border=1>
  <tr><td>아이디 </td>  <td><input type="text"  name="id" >   </td></tr> 
  <tr><td>비밀번호 </td>  <td><input type="text"  name="password" > </td></tr> 
  <tr><td colspan=2  align="center">
    <input type="submit"  value="로그인" > 
  
   </td></tr> 
  </table>

</form>

<hr />
</div>
</body>
</html>

2. 목록보기

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.springbook.biz.board.impl.*"  %>
<%@ page import="com.springbook.biz.board.*"  %>
<%@ page import="java.util.*"  %>

<%
 String searchCondition=request.getParameter("searchCondition");
 String searchKeyword=request.getParameter("searchKeyword");

  BoardVo vo = new BoardVo();
  vo.setSearchCondition(searchCondition);
  vo.setSearchKeyword(searchKeyword);
  
  BoardDao   dao = new BoardDaoImpl();
  List<BoardVo> li = dao.getBoardList(vo);
%>
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<br><br>
<h1> 글목록 </h1>
<h3> 테스트님 환영합니다. <a href=logout_proc.jsp>Log-out</a> </h3>

<!-- 검색 시작 -->
<table border=1 width=700>
<tr><td align="right">
<form action=getBoardList.jsp >
<select name="searchCondition">
<option value="title"> 제목 </option>
<option value="content"> 내용 </option>
</select>
<input  type=text  name="searchKeyword" >
<input  type=submit value="검색하기" >
</form>
</td></tr>
</table>
<table border=1 width=700>
<tr> 
 <th> 번호 </th> <th> 제목 </th> <th> 작성자 </th>
 <th> 등록일 </th> <th> 조회수 </th>
</tr>

<% for(BoardVo m : li) {%>
<tr> 
 <td> <%=m.getSeq() %> </td> <td>  <%=m.getTitle() %> </td>
 <td>  <%=m.getWriter() %> </td>
 <td>  <%=m.getRegDate() %> </td> <td>  <%=m.getCnt() %> </td>
</tr>
<% } %>

</table>


</div>
</body>
</html>
package com.springbook.biz.board.impl;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.springbook.biz.board.BoardDao;
import com.springbook.biz.board.BoardVo;
import com.springbook.biz.common.JDBCUtil;

// @Repository
public class BoardDaoImpl implements BoardDao {
	
	public	BoardDaoImpl(){
		dbConn = JDBCUtil.getInstance();
	}
	
	private JDBCUtil dbConn =null;
	private Connection conn =null;
	private PreparedStatement pstmt =null;
	private ResultSet rs=null;
	
	String BOARD_INSERT="insert into board(seq, title, writer, content)"
			+ " values((select nvl(max(seq),0)+1 from board), ?,?,?)";

	String BOARD_SELECT=" select seq, title, writer, content , regDate, cnt from board order by seq  desc " ;

	String BOARD_SELECT_TITLE = " select * from board where title like ?  order by seq  desc " ;
	String BOARD_SELECT_CONTENT = " select * from board where content like ?  order by seq  desc " ;
	
	String BOARD_EDIT="select seq, title, writer, content,  regDate, cnt "
			+ " from board where seq = ? " ;

	String BOARD_DELETE=" delete from board where seq = ? " ;
	
	String BOARD_UPDATE="update board set title=?, writer=?, content=? where seq=?";

	
	@Override
	public void insert(BoardVo vo) {
		try {
		System.out.println("===> insert(BoardVo vo) 실행 ");	
		conn =dbConn.getConnection();
		pstmt=conn.prepareStatement(BOARD_INSERT);
		pstmt.setString(1, vo.getTitle());
		pstmt.setString(2, vo.getWriter());
		pstmt.setString(3, vo.getContent());		
		pstmt.executeUpdate();
		
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			dbConn.close(pstmt, conn);
		}
	}

	@Override
	public void update(BoardVo vo) {
		try {
			System.out.println("===> void update(BoardVo vo) 실행 ");	
		conn =dbConn.getConnection();
		pstmt=conn.prepareStatement(BOARD_UPDATE);
		pstmt.setString(1, vo.getTitle());
		pstmt.setString(2, vo.getWriter());
		pstmt.setString(3, vo.getContent());	
		pstmt.setInt(4, vo.getSeq());
		pstmt.executeUpdate();
		
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			dbConn.close(pstmt, conn);
		}
		
	}

	@Override
	public void delete(int seq) {
		try {
			System.out.println("===> void delete(int seq) 실행 ");	
		conn =dbConn.getConnection();
		pstmt=conn.prepareStatement(BOARD_DELETE);
		pstmt.setInt(1, seq);		
		pstmt.executeUpdate();
		
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			dbConn.close(pstmt, conn);
		}
		
	}

	@Override
	public BoardVo getBoard(int seq) {
		BoardVo vo = new BoardVo();
		try {
			System.out.println("===> BoardVo edit(int seq) 실행 ");	
		conn =dbConn.getConnection();
		pstmt=conn.prepareStatement(BOARD_EDIT);
		pstmt.setInt(1, seq);
		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"));

		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			dbConn.close(rs, pstmt, conn);
		}
		return vo;
	}

	@Override
	public List<BoardVo> getBoardList(BoardVo v) {
		List<BoardVo> li = new ArrayList<BoardVo>();
		try {
			System.out.println("===> List<BoardVo> selectAll() 실행 ");	
			System.out.println("===> v 확인 : " + v );	
		conn =dbConn.getConnection();
		
		if (v.getSearchCondition() == null || v.getSearchKeyword() == "") {
			pstmt=conn.prepareStatement(BOARD_SELECT);	
		}else if(v.getSearchCondition().equals("title")) {
			pstmt=conn.prepareStatement(BOARD_SELECT_TITLE);
			pstmt.setString(1,"%" +v.getSearchKeyword()+ "%");
		}else if(v.getSearchCondition().equals("content")) {
			pstmt=conn.prepareStatement(BOARD_SELECT_CONTENT);
			pstmt.setString(1,"%" +v.getSearchKeyword()+ "%");
		}		
		
		rs=pstmt.executeQuery();
		while(rs.next()) {
			BoardVo 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"));
			li.add(vo);
		}		
		
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			dbConn.close(rs, pstmt, conn);
		}
		return li;
	}

}

 

'Spring' 카테고리의 다른 글

MVC 프레임워크 구현하기  (0) 2022.09.23
Model 2 : DispatcherServlet 만들기  (0) 2022.09.23
트랜잭션 처리  (0) 2022.09.22
Spring JDBC 방법 2  (0) 2022.09.22
Spring JDBC 방법  (0) 2022.09.22
Comments