Search

๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก

๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก

โ€ข
๊ธฐ๋Šฅ ์„ค๋ช…
โ€ข
์ž‘์—… ์ˆœ์„œ
โ€ข
์‹ค์Šต ์ฝ”๋“œ
โ€ข
ํ…Œ์ŠคํŠธ
โ€ข
๊ฒฐ๊ณผ ํ™”๋ฉด

๊ธฐ๋Šฅ ์„ค๋ช…

์ž‘์—… ์ˆœ์„œ

1.
๊ฒŒ์‹œ๊ธ€ ERD ์ž‘์„ฑ
2.
๊ฒŒ์‹œ๊ธ€ ํ…Œ์ด๋ธ” ์ƒ์„ฑ
3.
Posts.java - ๋ฐ์ดํ„ฐ ์ „์†ก ๊ฐ์ฒด ์ž‘์„ฑ
4.
Category.java - ๋ฐ์ดํ„ฐ ์ „์†ก ๊ฐ์ฒด ์ž‘์„ฑ
5.
PostDAO.java - ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ฐ์ฒด ์ž‘์„ฑ
6.
PostService.java - ์„œ๋น„์Šค ์ธํ„ฐํŽ˜์ด์Šค ์ž‘์„ฑ
7.
PostServiceImpl.java - ์„œ๋น„์Šค ๊ตฌํ˜„ ํด๋ž˜์Šค ์ž‘์„ฑ
8.
CateDAO.java - ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ฐ์ฒด ์ž‘์„ฑ
9.
CateService.java - ์„œ๋น„์Šค ์ธํ„ฐํŽ˜์ด์Šค ์ž‘์„ฑ
10.
CateServiceImpl.java - ์„œ๋น„์Šค ๊ตฌํ˜„ ํด๋ž˜์Šค ์ž‘์„ฑ
11.
list.jsp - ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก ํ™”๋ฉด ์ž‘์„ฑ

์‹ค์Šต ์ฝ”๋“œ

๊ฒŒ์‹œ๊ธ€ ERD ์ž‘์„ฑ

๊ฒŒ์‹œ๊ธ€ ํ…Œ์ด๋ธ” ์ƒ์„ฑ

โ€ข
posts ํ…Œ์ด๋ธ”
โ€ข
category ํ…Œ์ด๋ธ”

posts ํ…Œ์ด๋ธ”

CREATE TABLE posts ( id VARCHAR2(255) NOT NULL PRIMARY KEY, users_id VARCHAR2(255) NOT NULL, cate_id VARCHAR2(255) NOT NULL, post_no NUMBER NULL, title VARCHAR2(200) NULL, username VARCHAR2(100) DEFAULT 'NOUSER' NULL, name VARCHAR2(100) NULL, password VARCHAR2(100) NULL, content CLOB NULL, reg_date DATE DEFAULT sysdate NULL, upd_date DATE DEFAULT sysdate NULL ); ALTER TABLE posts ADD CONSTRAINT FK_users_TO_posts_1 FOREIGN KEY ( users_id ) REFERENCES users ( id );
SQL
๋ณต์‚ฌ

category ํ…Œ์ด๋ธ”

CREATE TABLE category ( id VARCHAR2(255) NOT NULL PRIMARY KEY, category_id VARCHAR2(100) NULL, name VARCHAR2(100) NULL, post_no NUMBER NULL, type VARCHAR2(100) NULL, seq NUMBER DEFAULT 0 NOT NULL, reg_date DATE DEFAULT sysdate NULL, upd_date DATE DEFAULT sysdate NULL ); ALTER TABLE posts ADD CONSTRAINT FK_category_TO_posts_1 FOREIGN KEY ( cate_id ) REFERENCES category ( id );
SQL
๋ณต์‚ฌ
์นดํ…Œ๊ณ ๋ฆฌ ๊ด€๋ฆฌ๋Š” ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€์—์„œ ๊ตฌํ˜„ํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด๋ณธ๋‹ค.
Insert into ALOHA.CATEGORY (ID,CATEGORY_ID,NAME,POST_NO,TYPE,SEQ,REG_DATE,UPD_DATE) values ('F35F1FC3C7AE44CA8A335748EC5199FB','B-0002','ํšŒ์› ๊ฒŒ์‹œํŒ',1,'board',2,to_date('24/04/12','RR/MM/DD'),to_date('24/04/12','RR/MM/DD')); Insert into ALOHA.CATEGORY (ID,CATEGORY_ID,NAME,POST_NO,TYPE,SEQ,REG_DATE,UPD_DATE) values ('75773CF72205485E9A75CF67627686DC','B-0001','์ž์œ  ๊ฒŒ์‹œํŒ',1,'board',1,to_date('24/04/12','RR/MM/DD'),to_date('24/04/12','RR/MM/DD'));
SQL
๋ณต์‚ฌ

Posts.java - ๋ฐ์ดํ„ฐ ์ „์†ก ๊ฐ์ฒด ์ž‘์„ฑ

package posts.model; import java.util.Date; import lombok.Data; @Data public class Posts { private String id; private String usersId; private String cateId; private int postNo; private String title; private String username; private String name; private String password; private String content; private Date regDate; private Date updDate; public Posts() { } public Posts(String title, String username, String content) { this.title = title; this.username = username; this.content = content; } }
Java
๋ณต์‚ฌ

Category.java - ๋ฐ์ดํ„ฐ ์ „์†ก ๊ฐ์ฒด ์ž‘์„ฑ

package posts.model; import java.util.Date; import lombok.Data; @Data public class Category { private String id; private String strId; private String categoryId; private String name; private int postNo; private String type; private int seq; private Date regDate; private Date updDate; public Category() { } public Category(String categoryId, String name, String type) { this.categoryId = categoryId; this.name = name; this.type = type; this.postNo = 1; } }
Java
๋ณต์‚ฌ

PostDAO.java - ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ฐ์ฒด ์ž‘์„ฑ

package posts.data; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import common.jdbc.JDBConnection; import posts.model.Posts; /** * ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ฐ์ฒด * - ๊ฒŒ์‹œ๊ธ€ ๋ฐ์ดํ„ฐ ์ ‘๊ทผ */ public class PostDAO extends JDBConnection { // ๋ฐ์ดํ„ฐ ๋ชฉ๋ก public List<Posts> list() { List<Posts> postList = new ArrayList<Posts>(); String sql = " SELECT * " + " FROM posts "; try { stmt = con.createStatement(); rs = stmt.executeQuery(sql); while( rs.next() ) { Posts post = new Posts(); post.setId( rs.getString("id") ); post.setUsersId( rs.getString("users_id") ); post.setCateId( rs.getString("cate_id") ); post.setPostNo( rs.getInt("post_no") ); post.setTitle( rs.getString("title") ); post.setUsername( rs.getString("username") ); post.setName( rs.getString("name") ); post.setContent( rs.getString("password") ); post.setContent( rs.getString("content") ); post.setRegDate( rs.getTimestamp("reg_date") ); post.setUpdDate( rs.getTimestamp("upd_date") ); postList.add(post); } } catch(SQLException e) { System.err.println("๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก ์กฐํšŒ ์‹œ, ์˜ˆ์™ธ ๋ฐœ์ƒ"); e.printStackTrace(); } return postList; } }
Java
๋ณต์‚ฌ

PostService.java - ์„œ๋น„์Šค ์ธํ„ฐํŽ˜์ด์Šค ์ž‘์„ฑ

package posts.service; import java.util.List; import posts.model.Posts; /** * ๊ฒŒ์‹œํŒ ํ”„๋กœ๊ทธ๋žจ์˜ ์ธํ„ฐํŽ˜์ด์Šค * * ๊ฒŒ์‹œํŒ์˜ ๊ธฐ๋Šฅ ๋ฉ”์†Œ๋“œ * - ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก * - ๊ฒŒ์‹œ๊ธ€ ์กฐํšŒ * - ๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก * - ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • * - ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ * */ public interface PostService { // ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก List<Posts> list(); }
Java
๋ณต์‚ฌ

PostServiceImpl.java - ์„œ๋น„์Šค ๊ตฌํ˜„ ํด๋ž˜์Šค ์ž‘์„ฑ

package posts.service; import java.util.List; import posts.data.PostDAO; import posts.model.Posts; /** * ๊ฒŒ์‹œํŒ ๊ธฐ๋Šฅ - ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ํด๋ž˜์Šค */ public class PostServiceImpl implements PostService { private PostDAO postDAO = new PostDAO(); @Override public List<Posts> list() { List<Posts> postList = postDAO.list(); return postList; } }
Java
๋ณต์‚ฌ

CateDAO.java - ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ฐ์ฒด ์ž‘์„ฑ

package posts.data; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import common.jdbc.JDBConnection; import posts.model.Category; /** * ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ฐ์ฒด * - ์นดํ…Œ๊ณ ๋ฆฌ ๋ฐ์ดํ„ฐ ์ ‘๊ทผ */ public class CateDAO extends JDBConnection { // ๋ฐ์ดํ„ฐ ๋ชฉ๋ก public List<Category> list() { List<Category> categoryList = new ArrayList<Category>(); String sql = " SELECT * " + " FROM category" + " ORDER BY seq ASC "; try { stmt = con.createStatement(); rs = stmt.executeQuery(sql); while( rs.next() ) { // next() : ์‹คํ–‰ ๊ฒฐ๊ณผ์˜ ๋‹ค์Œ ๋ฐ์ดํ„ฐ๋กœ ์ด๋™ Category category = new Category(); category.setId( rs.getString("id") ); category.setCategoryId( rs.getString("category_id") ); category.setName( rs.getString("name") ); category.setPostNo( rs.getInt("post_no") ); category.setType( rs.getString("type") ); category.setSeq( rs.getInt("seq") ); category.setRegDate( rs.getTimestamp("reg_date") ); category.setUpdDate( rs.getTimestamp("upd_date") ); categoryList.add(category); } } catch(SQLException e) { System.err.println("์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก ์กฐํšŒ ์‹œ, ์˜ˆ์™ธ ๋ฐœ์ƒ"); e.printStackTrace(); } return categoryList; } }
Java
๋ณต์‚ฌ

CateService.java - ์„œ๋น„์Šค ์ธํ„ฐํŽ˜์ด์Šค ์ž‘์„ฑ

package posts.service; import java.util.List; import posts.model.Category; /** * ์นดํ…Œ๊ณ ๋ฆฌ ์ธํ„ฐํŽ˜์ด์Šค * * ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธฐ๋Šฅ ๋ฉ”์†Œ๋“œ * - ์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก * - ์นดํ…Œ๊ณ ๋ฆฌ ์กฐํšŒ * - ์นดํ…Œ๊ณ ๋ฆฌ ๋“ฑ๋ก * - ์นดํ…Œ๊ณ ๋ฆฌ ์ˆ˜์ • * - ์นดํ…Œ๊ณ ๋ฆฌ ์‚ญ์ œ * */ public interface CateService { // ์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก List<Category> list(); }
Java
๋ณต์‚ฌ

CateServiceImpl.java - ์„œ๋น„์Šค ๊ตฌํ˜„ ํด๋ž˜์Šค ์ž‘์„ฑ

package posts.service; import java.util.List; import posts.data.CateDAO; import posts.model.Category; /** * ์นดํ…Œ๊ณ ๋ฆฌ ๊ธฐ๋Šฅ - ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ํด๋ž˜์Šค */ public class CateServiceImpl implements CateService { private CateDAO cateDAO = new CateDAO(); @Override public List<Category> list() { List<Category> categoryList = cateDAO.list(); return categoryList; } }
Java
๋ณต์‚ฌ

list.jsp - ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก ํ™”๋ฉด ์ž‘์„ฑ

<%@page import="posts.service.PostServiceImpl"%> <%@page import="posts.model.Posts"%> <%@page import="posts.service.PostService"%> <%@page import="posts.model.Category"%> <%@page import="java.util.List"%> <%@page import="posts.service.CateServiceImpl"%> <%@page import="posts.service.CateService"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% //์นดํ…Œ๊ณ ๋ฆฌ CateService cateService = new CateServiceImpl(); List<Category> categoryList = cateService.list(); pageContext.setAttribute("categoryList", categoryList); // ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก PostService postService = new PostServiceImpl(); List<Posts> postList; String cateId = request.getParameter("cateId"); // ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก ์กฐํšŒ // ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„ ์กฐํšŒ if( cateId != null && !cateId.equals("") ) { postList = postService.listByCateId(cateId); Category category = cateService.select(cateId); pageContext.setAttribute("category", category); } // ์ „์ฒด ์กฐํšŒ else { postList = postService.list(); } pageContext.setAttribute("postList", postList); %> <!DOCTYPE html> <html data-bs-theme="light"> <head> <meta charset="UTF-8"> <jsp:include page="/layout/main/import/common.jsp" /> <jsp:include page="/layout/main/import/meta.jsp" /> <jsp:include page="/layout/main/import/link.jsp" /> </head> <body> <jsp:include page="/layout/main/UI/header.jsp" /> <div class="container"> <div class="px-4 pt-5 my-5 text-center border-bottom"> <h1 class="display-4 fw-bold">๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก</h1> <div class="col-lg-6 mx-auto"> <p class="lead mb-4">JSP ๊ฒŒ์‹œํŒ ํ”„๋กœ์ ํŠธ ์ž…๋‹ˆ๋‹ค.</p> </div> </div> <!-- ๊ฒŒ์‹œํŒ ํ…Œ์ด๋ธ” --> <div class="d-grid gap-2 d-sm-flex justify-content-sm-center mb-5"> <a href="<%= request.getContextPath() %>/posts/insert.jsp" class="btn btn-success btn-lg px-4 me-sm-3">๊ธ€์“ฐ๊ธฐ</a> </div> <table class="table table-hover"> <thead class="table-dark"> <tr class="text-center"> <th scope="col">#</th> <th class="text-left w-50" scope="col">์ œ๋ชฉ</th> <th scope="col">์ž‘์„ฑ์ž</th> <th scope="col">๋‚ ์งœ</th> <th scope="col">์กฐํšŒ์ˆ˜</th> </tr> </thead> <tbody> <c:if test="${ postList.isEmpty() }"> <tr> <td align="center" colspan="5">์กฐํšŒ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.</td> </tr> </c:if> <c:forEach var="post" items="${postList}"> <tr class="text-center"> <td>${post.postNo}</td> <td class="text-start"> <a href="${ root }/posts/read.jsp?id=${ post.id }"> ${post.title} </a> </td> <td>${post.username}</td> <td> <fmt:formatDate value="${post.updDate}" pattern="yyyy-MM-dd HH:mm:ss"/> </td> <td>0</td> </tr> </c:forEach> </tbody> </table> </div> <jsp:include page="/layout/main/UI/footer.jsp" /> <jsp:include page="/layout/main/import/script.jsp" /> </body> </html>
HTML
๋ณต์‚ฌ

ํ…Œ์ŠคํŠธ

๊ฒฐ๊ณผ ํ™”๋ฉด