일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- jsp게시판만들기
- spring crud
- CRUD게시판만들기
- 2020정보처리기사실기정리
- html기초
- 프로그래머스 쿼리문
- 자바 정규표현식 예제
- 자바연산자
- 날짜지정팝업
- PLSQL
- js datepicker
- 오버라이딩
- 2020정보처리기사실기요약
- 프로그래머스 SQL
- 2020정보처리기사실기
- crud게시판
- 스프링 CRUD게시판
- 스프링게시판만들기
- 스프링 crud
- 자바배열예제
- 정처기실기정리
- 프로그래머스 MYSQL
- 정보처리기사실기정리
- 오라클설치
- 자바기초
- jsp 날짜팝업
- jsp 팝업띄우기
- 게시판만들기
- Oracle기초
- spring crud게시판
Archives
- Today
- Total
영보의 SystemOut.log
[Web] h2 DB연동하여 Model1 CRUD board 구현하기 본문
반응형
Model1 구조 board
실습
http://h2database.com/html/download.html
먼저 Platform-independent.zip 파일을 다운받는다.
경로대로 들어가서 h2를 실행하면
cmd 창과 함께 웹브라우저로 h2 가 실행된다.
# user 테이블 생성
Create table users(
id varchar2(8) primary key,
password varchar2(10) not null,
name varchar2(20) not null,
role varchar2(10)
);
# user 테이블 값 넣어주기
insert into users values('test','test1234','관리자','Admin');
insert into users values('user','user1234','손유일','User');
# board 테이블 생성
create table board(
seq int(5) primary key,
title varchar2(30),
writer varchar2(20),
content varchar2(200),
regdate date default sysdate,
cnt int(5) default 0
);
# board 테이블 값 넣어주기
insert into board(seq,title,writer,content) values(1,'가입인사','관리자','잘 운영하겠습니다.');
insert into board(seq,title,writer,content) values(2,'게시판구현','손유일','모델1구조로 구현');
insert into board(seq,title,writer,content) values(3,'MVC패턴으로구현','홍길동','등업신청이요.');
insert into board(seq,title,writer,content) values(4,'게시판프로젝트','안보현','마이네임 시청 부탁드려요');
# 커밋
commit;
# slect 문으로 데이터 확인하기
데이터가 아주 잘 들어가있는걸 볼 수 있다.
# UserDO.java
package user;
public class UserDO {
private String id;
private String password;
private String name;
private String role;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
# BoardDO.java
package board;
import java.sql.Date;
public class BoardDO {
private int seq;
private String title;
private String writer;
private String content;
private Date regdate;
private int cnt;
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;
}
}
# BoardDAO.java
package board;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import common.JDBCUtil;
public class BoardDAO {
//DB 관련 변수 선언
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
//전체 게시글 목록 조회 메소드
public List<BoardDO> getBoardList(String searchField, String searchText){
System.out.println("==> getBoardList() 기능 처리됨!");
List<BoardDO> boardList = new ArrayList<BoardDO>(); //가변 배열 객체 생성
try {
conn = JDBCUtil.getConnection();
/*
* [중요] 게시물 검색 시 => '제목' or '작성자'로 검색 조건 제시하는 SQL 문장을 어떻게 작성할 것 인가?
* 하나의 sql 문장을 두가지 용도로 사용
* 1. 검색 조건이 없을 때 => 전체 검색
* 2. 검색 조건이 있을 때 => 조건 검색
*/
String where = "";
if( searchField != null && searchText != null ) {
where = "where "+ searchField + " like '%" + searchText+"%'";
}
System.out.println("where: " + where);
String Condition_SQL = "select * from board "+ where +" order by seq desc";
pstmt = conn.prepareStatement(Condition_SQL);
rs = pstmt.executeQuery();
while(rs.next()) {
BoardDO board = new BoardDO();
board.setSeq(rs.getInt("SEQ"));
board.setTitle(rs.getString("TITLE"));
board.setContent(rs.getString("CONTENT"));
board.setWriter(rs.getString("WRITER"));
board.setCnt(rs.getInt("CNT"));
board.setRegdate(rs.getDate("REGDATE"));
boardList.add(board);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
JDBCUtil.close(rs, pstmt, conn);
}
return boardList;
} // ======================================= end getBoardList() =================================================
//게시글 번호 조건에 맞는 해당 게시글만 검색하는 메소드
public BoardDO getBoard(BoardDO boardDO) {
System.out.println("==> getBoard() 처리됨");
BoardDO board = null;
try {
conn = JDBCUtil.getConnection();
//[중요] 해당 게시글의 조회수(cnt)를 1 증가 시킨다.
String UPDATE_CNT = "update board set cnt=cnt+1 where seq=?";
pstmt = conn.prepareStatement(UPDATE_CNT);
pstmt.setInt(1, boardDO.getSeq());
pstmt.executeUpdate(); // DML 작업 시에는 executeUpdate로 호출
//그런 다음 해당 게시글 가져오기
String BOARD_GET = "select * from board where seq=?";
pstmt = conn.prepareStatement(BOARD_GET);
pstmt.setInt(1, boardDO.getSeq());
rs = pstmt.executeQuery();
if(rs.next()) {
board = new BoardDO();
board.setSeq(rs.getInt("seq"));
board.setTitle(rs.getString("title"));
board.setWriter(rs.getString("writer"));
board.setContent(rs.getString("content"));
board.setRegdate(rs.getDate("regdate"));
board.setCnt(rs.getInt("cnt"));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
JDBCUtil.close(rs, pstmt, conn);
}
return board;
} // ======================================= end getBoard() =================================================
//게시글 수정 처리 메소드
public int updateBoard(BoardDO boardDO) {
System.out.println("==> updateBoard() 처리됨!");
int result=0;
try {
conn = JDBCUtil.getConnection();
String BOARD_UPDATE = "update board set title=?, content=? where seq=?";
pstmt = conn.prepareStatement(BOARD_UPDATE);
pstmt.setString(1, boardDO.getTitle());
pstmt.setString(2, boardDO.getContent());
pstmt.setInt(3, boardDO.getSeq());
result = pstmt.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
} finally {
}
System.out.println(result);
return result;
} // ======================================= end updateBoard() =================================================
public void deleteBoard(BoardDO boardDO) {
System.out.println("==> deleteBoard() 처리됨!");
try {
conn = JDBCUtil.getConnection();
String BOARD_DELETE = "delete from board where seq=?";
pstmt = conn.prepareStatement(BOARD_DELETE);
pstmt.setInt(1, boardDO.getSeq());
pstmt.executeUpdate();
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
JDBCUtil.close(pstmt, conn);
}
} // ======================================= end deleteBoard() =================================================
public void insertBoard(BoardDO boardDO) {
System.out.println("==> insertBoard() 처리됨!");
int result=0;
try {
conn = JDBCUtil.getConnection();
String BOARD_INSERT = "insert into board(seq,title,writer,content) values((select nvl(max(seq),0)+1 from board),?,?,?)";
// 게시글이 없어도 seq 는 1부터 시작해서 1씩 증가
pstmt = conn.prepareStatement(BOARD_INSERT);
pstmt.setString(1, boardDO.getTitle());
pstmt.setString(2, boardDO.getContent());
pstmt.setString(3, boardDO.getWriter());
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally {
JDBCUtil.close(pstmt, conn);
}
} // ======================================= end insertBoard() =================================================
}
# UserDAO.java
package user;
import java.sql.*;
import common.JDBCUtil;
public class UserDAO {
// DB 관련 변수 선언
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
// SQL 명령어
private final String USER_GET = "select id, password from users where id=? and password=?";
// 로그인 user 조회 (select) 메소드 구현
public UserDO getUser(UserDO userObj) {
UserDO user = null;
try {
System.out.println("===> JDBC로 getUser() 기능 처리됨 ! ");
conn = JDBCUtil.getConnection();
pstmt = conn.prepareStatement(USER_GET);
pstmt.setString(1, userObj.getId());
pstmt.setString(2, userObj.getPassword());
rs = pstmt.executeQuery();
if(rs.next()) {
user = new UserDO();
user.setId(rs.getString("ID"));
user.setPassword(rs.getString("PASSWORD"));
}
}catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtil.close(rs, pstmt, conn);
}
return user;
}
}
# JDBCUtil.java
package common;
import java.sql.*;
public class JDBCUtil {
// H2 DB 연동에 관련 소스
static final String driver = "org.h2.Driver";
static final String url = "jdbc:h2:tcp://localhost/~/test";
public static Connection getConnection() throws Exception{
try {
Class.forName(driver);
Connection con = DriverManager.getConnection(url, "sa", "");
return con;
}catch(Exception e) {
e.printStackTrace();
}
return null;
}
/*
* 메소드 오버로딩 => 다형성 구현
*/
// DML(insert, update, delete) 작업 종료 시 호출되는 메소드
public static void close(PreparedStatement pstmt, Connection conn) {
if(pstmt != null) {
try {
if(!pstmt.isClosed()) pstmt.close();
}catch (Exception e) {
e.printStackTrace();
}finally {
pstmt = null;
}
}if(conn != null) {
try {
if(!conn.isClosed()) conn.close();
}catch (Exception e) {
e.printStackTrace();
}finally {
conn = null;
}
}
}
// select 작업 종료 시 호출되는 메소드
public static void close(ResultSet rs, PreparedStatement pstmt, Connection conn) {
if(rs != null) {
try {
if(!rs.isClosed()) rs.close(); // 자원 해제
}catch (Exception e) {
e.printStackTrace();
}finally {
rs = null;
}
}if(conn != null) {
try {
if(!conn.isClosed()) conn.close(); // 자원 해제
}catch (Exception e) {
e.printStackTrace();
}finally {
conn = null;
}
}
}
}
# login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>로그인 페이지</title>
<style>
#div_box {
position : absolute;
top: 10%;
left: 40%;
}
</style>
</head>
<body>
<div id="div_box">
<h1 align="center">로그인</h1>
<form name="loginForm" method="POST" action="login_proc.jsp">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="skyblue">아이디</td>
<td><input type="text" name="id"/></td>
</tr>
<tr>
<td bgcolor="skyblue">비밀번호</td>
<td><input type="text" name="password"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="로그인"/>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
# login_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" errorPage="error.jsp" %>
<!-- 추가 자바 클래스 임포트-->
<%@ page import="user.UserDO" %>
<%@ page import="user.UserDAO" %>
<%
// 사용자 입력 정보 추출
String id = request.getParameter("id");
String password = request.getParameter("password");
// 2. UserDO 클래스 객체 생성 후 맴버변수에 값 초기화
UserDO userDO = new UserDO();
userDO.setId(id);
userDO.setPassword(password);
// 3. UserDO 클래스 객체 생성 후 getUser() 메소드 호출하면서 userDO 객체를 넘겨준다
UserDAO userDAO = new UserDAO();
UserDO user = userDAO.getUser(userDO);
// 화면 네비게이션
if(user != null){
session.setAttribute("IdKey", id);
response.sendRedirect("getBoardList.jsp");
/* out.println("<script>alert('로그인 성공');</script>"); */
}else{
/* out.println("<script>alert('로그인 실패');</script>"); */
response.sendRedirect("login.jsp");
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
# logout_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// 1.브라우저에 연결된 세션 객체를 강제 종료(세션 무효화)
session.invalidate();
// 2. 세션 종료 후 로그인 페이지로 이동
response.sendRedirect("login.jsp");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>insert_proc.jsp 페이지 => 게시글 등록 컨트롤러 페이지</title>
</head>
<body>
</body>
</html>
# getBoard.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" errorPage="error.jsp"%>
<!-- 자바 클래스 import -->
<%@ page import = "board.BoardDO" %>
<%@ page import = "board.BoardDAO" %>
<%
String seq = request.getParameter("seq");
BoardDO boardDO = new BoardDO();
boardDO.setSeq(Integer.parseInt(seq)); //문자열로 넘어온 값을 정수값으로 변환
BoardDAO boardDAO = new BoardDAO();
BoardDO board = boardDAO.getBoard(boardDO); // 중요
request.setAttribute("board", board);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>getBoard.jsp페이지 -> 게시글 상세보기 페이지</title>
<style>
#div_box {
position: absolute;
top: 10%;
left: 40%;
}
</style>
</head>
<body>
<div id="div_box">
<h1>게시글 상세보기</h1>
<a href="logout_proc.jsp">로그아웃</a>
<hr>
<form name="getBoardForm" method="POST" action="updateBoard_proc.jsp">
<input type="hidden" name="seq" value="${board.seq}"/>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="orange" width="70">제목</td>
<td align="left">
<input name="title" type="text" value="${board.title}" />
</td>
</tr>
<tr>
<td bgcolor="orange">작성자</td>
<td align="left">${board.writer}</td>
</tr>
<tr>
<td bgcolor="orange">내용</td>
<td align="left">
<textarea name="content" rows="10" cols="40">${board.content}</textarea>
</td>
</tr>
<tr>
<td bgcolor="orange">등록일</td>
<td align="left">${board.regdate}</td>
</tr>
<tr>
<td bgcolor="orange">조회수</td>
<td align="left">${board.cnt}</td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="글 수정" />
</td>
</tr>
</table>
</form>
<hr>
<a href="insertBoard.jsp">새 게시글 등록</a>
<a href="deleteBoard_proc.jsp?seq=${board.seq}">게시글 삭제</a>
<a href="getBoardList.jsp">전체 게시글 목록 보기</a>
</div>
</body>
</html>
# getBoardList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" errorPage="error.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ page import="board.BoardDO" %>
<%@ page import="board.BoardDAO" %>
<%@ page import="java.util.List" %>
<%
request.setCharacterEncoding("UTF-8");
String searchField = ""; //검색 대상(즉, 제목 or 작성자)
String searchText = ""; //검색 내용
if(request.getParameter("searchCondition")!="" && request.getParameter("searchKeyword")!=""){
searchField= request.getParameter("searchCondition");
searchText = request.getParameter("searchKeyword");
}
BoardDAO boardDAO = new BoardDAO();
List<BoardDO> boardList = boardDAO.getBoardList(searchField, searchText);
request.setAttribute("boardList", boardList);
// session과 request 차이점 : request는 현재 페이지 / session은 여러 페이지에서 공유할 때 쓴다.
request.setAttribute("totalList", boardList.size());
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>getBoardList 페이지</title>
<style>
#div_box {
position: absolutel;
top: 10%;
left: 20%;
}
</style>
</head>
<body>
<div id="div_box">
<h1>전체 게시글 목록 보기</h1>
<h3>${IdKey}님 환영합니다. <a href="logout_proc.jsp">로그아웃</a></h3>
<form name="boardListForm" method="POST" action="getBoardList.jsp">
<p>총 게시글: ${totalList} 건 </p>
<table border="1" cellpadding="0" cellspacing="0" width="700">
<tr>
<td align="right">
<select name="searchCondition">
<option value="TITLE">제목</option>
<option value="WRITER">작성자</option>
<input name="searchKeyword" type="text" />
<input type="submit" value="검색" />
</select>
</td>
</tr>
</table>
</form>
<table border="1" cellpadding="0" cellspacing="0" width="700">
<tr>
<th bgcolor="skyblue" width="100">번호</th>
<th bgcolor="skyblue" width="200">제목</th>
<th bgcolor="skyblue" width="150">작성자</th>
<th bgcolor="skyblue" width="150">등록일</th>
<th bgcolor="skyblue" width="100">조회수</th>
</tr>
<%-- 방법 1
<%
for(BoardDO board : boardList){
%>
<tr>
<td align="center"><%= board.getSeq() %></td>
<td align="left"><a href="getBoard.jsp?seq=<%= board.getSeq()%>"><%=board.getTitle() %></a></td>
<!-- 제목으로 갈 때 게시글 번호를 같이 넘겨줘라. -->
<td align="center"><%= board.getWriter() %></td>
<td align="center"><%= board.getRegdate()%></td>
<td align="center"><%= board.getCnt() %></td>
</tr>
<% } %>
--%>
<%-- 방법 2 표현 언어와 JSTL을 적용하여 소스 변경 --%>
<c:forEach var="board" items="${boardList}">
<tr>
<td align="center">${board.seq}</td>
<td align="left"><a href="getBoard.jsp?seq=${board.seq}">${board.title}</a></td>
<td align="center">${board.writer}</td>
<td align="center">${board.regdate}</td>
<td align="center">${board.cnt}</td>
</tr>
</c:forEach>
</table>
<br>
<a href="insertBoard.jsp">새 게시글 등록</a>
<a href="getBoardList.jsp">전체 게시물 목록 보기</a>
</div>
</body>
</html>
# updateBoard_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" errorPage="error.jsp"%>
<!DOCTYPE html>
<!-- 자바 클래스 임포트 -->
<%@ page import="board.BoardDO" %>
<%@ page import="board.BoardDAO" %>
<%@ page import="java.util.List" %>
<%
request.setCharacterEncoding("UTF-8");
String seq = request.getParameter("seq");
String title = request.getParameter("title");
String content = request.getParameter("content");
BoardDO boardDO = new BoardDO();
boardDO.setSeq(Integer.parseInt(seq));
boardDO.setTitle(title);
boardDO.setContent(content);
BoardDAO boardDAO = new BoardDAO();
boardDAO.updateBoard(boardDO);
response.sendRedirect("getBoardList.jsp");
%>
<html>
<head>
<meta charset="UTF-8">
<title>updateBoard_proc.jsp =? "수정" 컨트롤러 페이지</title>
</head>
<body>
</body>
</html>
# deleteBoard_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<!-- 자바 클래스 임포트 -->
<%@ page import="board.BoardDO" %>
<%@ page import="board.BoardDAO" %>
<%@ page import="java.util.List" %>
<%
request.setCharacterEncoding("UTF-8");
String seq = request.getParameter("seq");
BoardDO boardDO = new BoardDO();
boardDO.setSeq(Integer.parseInt(seq));
BoardDAO boardDAO = new BoardDAO();
boardDAO.deleteBoard(boardDO);
response.sendRedirect("getBoardList.jsp");
%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
# insertBoard.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" errorPage="error.jsp"%>
<!DOCTYPE html>
<!-- 자바 클래스 임포트 -->
<%@ page import="board.BoardDO" %>
<%@ page import="board.BoardDAO" %>
<%@ page import="java.util.List" %>
<html>
<head>
<meta charset="UTF-8">
<title>insertBoard.jsp => 게시글 등록 페이지</title>
</head>
<body>
<h1>새 게시글 등록</h1>
<a href="logout_proc.jsp">로그아웃</a>
<hr>
<form name="insertForm" method="POST" action="insertBoard_proc.jsp">
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="orange" width="70">제목</td>
<td align="left"><input type="text" name="title" /></td>
</tr>
<tr>
<td bgcolor="orange">작성자</td>
<td align="left"><input type="text" name="writer" /></td>
</tr>
<tr>
<td bgcolor="orange">내용</td>
<td align="left">
<textarea name="content" rows="10" cols="40"></textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="글 등록" />
</td>
</tr>
</table>
</form>
<hr>
<a href="getBoardList.jsp">전체 게시글 목록 보기</a>
</body>
</html>
# insertBoard_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<!-- 자바 클래스 임포트 -->
<%@ page import="board.BoardDO" %>
<%@ page import="board.BoardDAO" %>
<%@ page import="java.util.List" %>
<%
request.setCharacterEncoding("UTF-8");
String writer = request.getParameter("writer");
String title = request.getParameter("title");
String content = request.getParameter("content");
BoardDO boardDO = new BoardDO();
boardDO.setWriter(writer);
boardDO.setTitle(title);
boardDO.setContent(content);
BoardDAO boardDAO = new BoardDAO();
boardDAO.insertBoard(boardDO);
response.sendRedirect("getBoardList.jsp");
%>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
구현 화면
반응형
'국비교육(아이티센 입사교육)' 카테고리의 다른 글
[DB] 개체관계도 (ERD; Entity Relatioship Diagram) 개념과 작성방법 (0) | 2021.11.04 |
---|---|
[Mybatis] Mybatis 와 연동하여 동적 웹페이지 구현하기 (0) | 2021.10.21 |