728x90
반응형
■ jspBoard
□ board package
* BoardDTO
package board;
public class BoardDTO {
private int num;
private String writer;
private String email;
private String subject;
private String passwd;
private String reg_date;
private int readcount;
private String content;
private String ip;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getReg_date() {
return reg_date;
}
public void setReg_date(String reg_date) {
this.reg_date = reg_date;
}
public int getReadcount() {
return readcount;
}
public void setReadcount(int readcount) {
this.readcount = readcount;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
}
* BoardDAO
package board;
import java.sql.*;
import java.util.*;
public class BoardDAO {
Connection con;
PreparedStatement ps;
ResultSet rs;
String url, user, pass;
public BoardDAO() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
url = "jdbc:oracle:thin:@localhost:1521:xe";
user = "web01";
pass = "web01";
}
public List<BoardDTO> listBoard() throws SQLException {
try {
con = DriverManager.getConnection(url, user, pass);
String sql = "select * from board order by num desc";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
List<BoardDTO> list = makeList(rs);
return list;
}finally {
if (rs != null) rs.close();
if (ps != null) ps.close();
if (con != null) con.close();
}
}
public List<BoardDTO> makeList(ResultSet rs) throws SQLException{
List<BoardDTO> list = new ArrayList<>();
while(rs.next()) {
BoardDTO dto = new BoardDTO();
dto.setNum(rs.getInt("num"));
dto.setWriter(rs.getString("writer"));
dto.setEmail(rs.getString("email"));
dto.setSubject(rs.getString("subject"));
dto.setPasswd(rs.getString("passwd"));
dto.setReg_date(rs.getString("reg_date"));
dto.setReadcount(rs.getInt("readcount"));
dto.setContent(rs.getString("content"));
dto.setIp(rs.getString("ip"));
list.add(dto);
}
return list;
}
public int insertBoard(BoardDTO dto) throws SQLException{
try {
con = DriverManager.getConnection(url, user, pass);
String sql = "insert into board values(board_seq.nextval, ?,?,?,?,sysdate,0,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1, dto.getWriter());
ps.setString(2, dto.getEmail());
ps.setString(3, dto.getSubject());
ps.setString(4, dto.getPasswd());
ps.setString(5, dto.getContent());
ps.setString(6, dto.getIp());
int res = ps.executeUpdate();
return res;
}finally {
if (ps != null) ps.close();
if (con != null) con.close();
}
}
protected void plusReadcount(int num) throws SQLException{
con = DriverManager.getConnection(url, user, pass);
String sql = "update board set readcount = readcount+1 where num = ?";
ps = con.prepareStatement(sql);
ps.setInt(1, num);
ps.executeUpdate();
}
public BoardDTO getBoard(int num, String mode) throws SQLException{
try {
if(mode.equals("content")) {
plusReadcount(num);
}
con = DriverManager.getConnection(url, user, pass);
String sql = "select * from board where num=?";
ps = con.prepareStatement(sql);
ps.setInt(1, num);
rs = ps.executeQuery();
List<BoardDTO> list = makeList(rs);
return list.get(0);
}finally {
if (rs != null) rs.close();
if (ps != null) ps.close();
if (con != null) con.close();
}
}
protected boolean isPassword(int num, String passwd) throws SQLException{
BoardDTO dto = getBoard(num, "password");
if(dto.getPasswd().equals(passwd.trim()))
return true;
else
return false;
}
public int deleteBoard(int num, String passwd) throws SQLException{
if(isPassword(num, passwd)) {
try {
con = DriverManager.getConnection(url, user, pass);
String sql = "delete from board where num=?";
ps = con.prepareStatement(sql);
ps.setInt(1, num);
int res = ps.executeUpdate();
return res;
} finally {
if (ps != null) ps.close();
if (con != null) con.close();
}
}
return -1;
}
public int updateBoard(BoardDTO dto) throws SQLException{
if(isPassword(dto.getNum(), dto.getPasswd())) {
try {
con = DriverManager.getConnection(url, user, pass);
String sql = "update board set writer=?, email=?, subject=?, content=? where num=?";
ps = con.prepareStatement(sql);
ps.setString(1, dto.getWriter());
ps.setString(2, dto.getEmail());
ps.setString(3, dto.getSubject());
ps.setString(4, dto.getContent());
ps.setInt(5, dto.getNum());
int res = ps.executeUpdate();
return res;
} finally {
if (ps != null) ps.close();
if (con != null) con.close();
}
}
return -1;
}
}
□ jsp 페이지
* index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- index.jsp -->
<html>
<head>
<title>jsp게시판</title>
</head>
<body>
<h1 align="center">JSP로 해보는 게시판 만들기</h1>
<ul>
<li><a href="board/list.jsp">게시판 가기</a>
</ul>
</body>
</html>
* board/list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*, board.*"%>
<!-- list.jsp -->
<!DOCTYPE html>
<html>
<head>
<title>글목록</title>
</head>
<body>
<div align="center">
<b>글 목 록</b>
<table border="0" width="800">
<tr bgcolor="yellow">
<td align="right"><a href="writeForm.jsp">글쓰기</a></td>
</tr>
</table>
<table border="1" width="800">
<tr bgcolor="green">
<th>번호</th>
<th width="30%">제목</th>
<th>작성자</th>
<th>작성일</th>
<th>조회</th>
<th>IP</th>
</tr>
<jsp:useBean id="bdao" class="board.BoardDAO" />
<%
List<BoardDTO> list = bdao.listBoard();
if(list == null || list.size() == 0){
%>
<tr>
<td colspan="6">등록된 게시글이 없습니다.</td>
<tr>
<%
} else{
for(BoardDTO dto : list){
%>
<tr>
<td><%=dto.getNum() %></td>
<td><a href="content.jsp?num=<%=dto.getNum()%>"><%=dto.getSubject() %></a></td>
<td><%=dto.getWriter() %></td>
<td><%=dto.getReg_date() %></td>
<td><%=dto.getReadcount() %></td>
<td><%=dto.getIp() %></td>
</tr>
<%
}
}
%>
</table>
</div>
</body>
</html>
* board/writeForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- writeForm.jsp -->
<html>
<head>
<title>글쓰기</title>
<script type="text/javascript">
function check(){
if(f.writer.value==""){
alert('이름을 입력해 주세요!')
f.writer.focus()
return false
}
if(f.subject.value==""){
alert('제목을 입력해 주세요!')
f.subject.focus()
return false
}
if(f.content.value==""){
alert('내용을 입력해 주세요!')
f.content.focus()
return false
}
if(f.passwd.value==""){
alert('비밀번호를 입력해 주세요!')
f.passwd.focus()
return false
}
return true
}
</script>
</head>
<body>
<div align="center">
<form name="f" action="writePro.jsp" method="post" onsubmit="return check()">
<table border="1" width="500">
<tr bgcolor="yellow">
<td colspan="2" align="center">글 쓰 기</td>
</tr>
<tr>
<th width="20%" bgcolor="yellow">이 름</th>
<td><input type="text" name="writer" ></td>
</tr>
<tr>
<th width="20%" bgcolor="yellow">제 목</th>
<td><input type="text" name="subject" size="50" ></td>
</tr>
<tr>
<th width="20%" bgcolor="yellow">Email</th>
<td><input type="text" name="email" size="50" ></td>
</tr>
<tr>
<th width="20%" bgcolor="yellow">내 용</th>
<td><textarea name="content" rows="11" cols="50"></textarea></td>
</tr>
<tr>
<th width="20%" bgcolor="yellow">비밀번호</th>
<td><input type="password" name="passwd" ></td>
</tr>
<tr bgcolor="yellow">
<td colspan="2" align="center">
<input type="submit" value="글쓰기">
<input type="reset" value="다시작성">
<input type="button" value="목록보기" onclick="window.location='list.jsp'">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
* board/writePro.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- writePro.jsp -->
<%
request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean id="bdto" class="board.BoardDTO" />
<jsp:setProperty property="*" name="bdto"/>
<jsp:useBean id="bdao" class="board.BoardDAO" />
<%
if (bdto.getWriter() == null || bdto.getWriter().trim().equals("")){
response.sendRedirect("writeForm.jsp");
return;
}
bdto.setIp(request.getRemoteAddr());
int res = bdao.insertBoard(bdto);
String msg = null, url = null;
if(res > 0){
msg = "게시글 등록 성공!! 게시글 목록 페이지로 이동합니다.";
url = "list.jsp";
} else{
msg = "게시글 등록 실패!! 게시글 등록 페이지로 이동합니다.";
url = "writeForm.jsp";
}
%>
<script type="text/javascript">
alert('<%=msg %>')
location.href="<%=url %>"
</script>
* board/content.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="board.*"%>
<!-- content.jsp -->
<%
String num = request.getParameter("num");
if(num == null || num.trim().equals("")){
response.sendRedirect("list.jsp");
return;
}
%>
<jsp:useBean id="bdao" class="board.BoardDAO" />
<% BoardDTO dto = bdao.getBoard(Integer.parseInt(num), "content");%>
<html>
<head>
<title>글내용</title>
</head>
<body>
<div align="center">
<b>글내용 보기</b><br><br>
<table border="1" width="600">
<tr>
<th width="20%" bgcolor="yellow">글번호</th>
<td align="center" width="30%"><%=dto.getNum() %></td>
<th width="20%" bgcolor="yellow">조회수</th>
<td align="center" width="30%"><%=dto.getReadcount() %></td>
</tr>
<tr>
<th width="20%" bgcolor="yellow">작성자</th>
<td align="center" width="30%"><%=dto.getWriter() %></td>
<th width="20%" bgcolor="yellow">작성일</th>
<td align="center" width="30%"><%=dto.getReg_date() %></td>
</tr>
<tr>
<th width="20%" bgcolor="yellow">글제목</th>
<td colspan="3"><%=dto.getSubject() %></td>
</tr>
<tr>
<th width="20%" bgcolor="yellow">글내용</th>
<td colspan="3"><%=dto.getContent() %></td>
</tr>
<tr bgcolor="yellow">
<td colspan="4" align="right">
<input type="button" value="글수정" onclick="window.location='updateForm.jsp?num=<%=dto.getNum()%>'">
<input type="button" value="글삭제" onclick="window.location='deleteForm.jsp?num=<%=dto.getNum()%>'">
<input type="button" value="글목록" onclick="window.location='list.jsp'">
</td>
</tr>
</table>
</div>
</body>
</html>
* board/updateForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="board.*"%>
<!-- updateForm.jsp -->
<%
String num = request.getParameter("num");
if(num == null || num.trim().equals("")){
response.sendRedirect("list.jsp");
return;
}
%>
<jsp:useBean id="bdao" class="board.BoardDAO" />
<% BoardDTO dto = bdao.getBoard(Integer.parseInt(num), "update");%>
<html>
<head>
<title>글수정</title>
<script type="text/javascript">
function check(){
if(f.writer.value == ""){
alert('이름을 입력해 주세요!')
f.writer.focus()
return false
}
if(f.subject.value == ""){
alert('제목을 입력해 주세요!')
f.subject.focus()
return false
}
if(f.content.value == ""){
alert('내용을 입력해 주세요!')
f.content.focus()
return false
}
if(f.passwd.value == ""){
alert('비밀번호를 입력해 주세요!')
f.passwd.focus()
return false
}
}
</script>
</head>
<body>
<div align="center">
<form name="f" action="updatePro.jsp" method="post" onsubmit="return check()">
<h3>글수정</h3>
<input type="hidden" name="num" value="<%=dto.getNum() %>">
<table border="1" width="500">
<tr>
<th width="20%" bgcolor="yellow">이 름</th>
<td><input type="text" name="writer" value="<%=dto.getWriter() %>" ></td>
</tr>
<tr>
<th width="20%" bgcolor="yellow">제 목</th>
<td><input type="text" name="subject" size="50" value="<%=dto.getSubject() %>" ></td>
</tr>
<tr>
<th width="20%" bgcolor="yellow">Email</th>
<td><input type="text" name="email" size="50" value="<%=dto.getEmail() %>" ></td>
</tr>
<tr>
<th width="20%" bgcolor="yellow">내 용</th>
<td><textarea name="content" rows="11" cols="50"><%=dto.getContent() %></textarea></td>
</tr>
<tr>
<th width="20%" bgcolor="yellow">비밀번호</th>
<td><input type="password" name="passwd" ></td>
</tr>
<tr bgcolor="yellow">
<td colspan="2" align="center">
<input type="submit" value="글쓰기">
<input type="reset" value="다시작성">
<input type="button" value="목록보기" onclick="window.location='list.jsp'">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
* board/updatePro.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- updatePro.jsp -->
<%
request.setCharacterEncoding("UTF-8");
%>
<jsp:useBean id="bdto" class="board.BoardDTO" />
<jsp:setProperty property="*" name="bdto"/>
<jsp:useBean id="bdao" class="board.BoardDAO" />
<%
if (bdto.getWriter() == null || bdto.getWriter().trim().equals("")){
response.sendRedirect("list.jsp");
return;
}
int res = bdao.updateBoard(bdto);
String msg = null, url = null;
if(res > 0){
msg = "게시글 수정 성공!! 게시글 목록 페이지로 이동합니다.";
url = "list.jsp";
} else if(res < 0){
msg = "비밀번호가 틀렸습니다!! 다시 입력해주세요!";
url = "updateForm.jsp?num=" + bdto.getNum();
}
%>
<script type="text/javascript">
alert('<%=msg %>')
location.href="<%=url %>"
</script>
* board/deleteForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="board.*"%>
<!-- deleteForm.jsp -->
<%
String num = request.getParameter("num");
if(num == null || num.trim().equals("")){
response.sendRedirect("list.jsp");
return;
}
%>
<html>
<head>
<title>글삭제</title>
</head>
<body>
<div align="center">
<b>글삭제</b><br><br>
<form name="f" action="deletePro.jsp" method="post">
<input type="hidden" name="num" value="<%=num %>">
<table border="1" width="300">
<tr bgcolor="yellow">
<th>비밀번호를 입력해 주세요.</th>
</tr>
<tr>
<td align="center">비밀번호 : <input type="password" name="passwd" ></td>
</tr>
<tr>
<td align="center">
<input type="submit" value="글삭제">
<input type="button" value="글목록" onclick="window.location='list.jsp'">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
* board/deletePro.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- deletePro.jsp -->
<%
String num = request.getParameter("num");
String passwd = request.getParameter("passwd");
if(num == null || passwd == null || num.trim().equals("")){
response.sendRedirect("list.jsp");
return;
}
%>
<jsp:useBean id="bdao" class="board.BoardDAO" />
<%
int res = bdao.deleteBoard(Integer.parseInt(num), passwd);
String msg = null, url = null;
if(res > 0){
msg = "게시글 삭제 성공!! 게시글 목록 페이지로 이동합니다.";
url = "list.jsp";
} else if(res < 0){
msg = "비밀번호가 틀렸습니다. 다시 입력해주세요.";
url = "deleteForm.jsp?num=" + num;
} else{
msg = "게시글 삭제 실패!! 게시글 보기 페이지로 이동합니다.";
url = "content.jsp?num=" + num;
}
%>
<script type="text/javascript">
alert('<%=msg %>')
location.href="<%=url %>"
</script>
728x90
반응형
'IT&코딩 > 자바 프로젝트' 카테고리의 다른 글
자바 프로젝트 6일차 - Servlet (testServlet) (0) | 2023.01.26 |
---|---|
자바 프로젝트 5일차 - JSP 복습 3 (jspMember) (0) | 2023.01.24 |
자바 프로젝트 3일차 - JSP 복습 1 (2) | 2023.01.21 |
자바 프로젝트 2일차 - Java 복습2 (0) | 2023.01.21 |
자바 프로젝트 1일차 - Java 복습 1 (0) | 2023.01.14 |