๊ฒ์๊ธ ๋ชฉ๋ก
โข
๊ธฐ๋ฅ ์ค๋ช
โข
์์
์์
โข
์ค์ต ์ฝ๋
โข
ํ
์คํธ
โข
๊ฒฐ๊ณผ ํ๋ฉด
๊ธฐ๋ฅ ์ค๋ช
์์ ์์
1.
๊ฒ์๊ธ ERD ์์ฑ
2.
๊ฒ์๊ธ ํ
์ด๋ธ ์์ฑ
3.
Posts.java - ๋ฐ์ดํฐ ์ ์ก ๊ฐ์ฒด ์์ฑ
4.
Category.java - ๋ฐ์ดํฐ ์ ์ก ๊ฐ์ฒด ์์ฑ
5.
6.
7.
8.
9.
10.
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
๋ณต์ฌ