[JSP]게시판 만들기(글목록/글쓰기/글수정/글삭제)
@욕심쟁이
·2020. 9. 10. 09:53
반응형
(디비관련(insert/update/delete/select)참고자료 https://sseozzzy.tistory.com/99)
1단계(데이터베이스 만들기)
- 게시판 정보를 저장하는 공간 만들기 : 데이터베이스만들기
- board 테이블 생성
create table member (
num int(11) primary key,
name varchar(20),
pass varchar(20),
subject varchar(50),
content varchar(2000),
readcount int(11) ,
date datetime
);
2단계(글쓰기)
writeForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/StudyJSP/jsp/write.jsp</title>
</head>
<body>
<h1>WebContent/StudyJSP/jsp/write.jsp</h1>
<form action="wirtePro.jsp" method="post">
<table>
<tr>
<td>글쓴이</td>
<td> <input type="text" name="name"> </td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="password" name="pass"></td>
</tr>
<tr>
<td>제목</td>
<td><input type="text" name="subject"></td>
</tr>
<tr>
<td>글내용</td>
<td><textarea name="content" rows="10" cols="20"></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="글쓰기"></td>
</tr>
</table>
</form>
</body>
</html>
writePro.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.Timestamp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/StudyJSP/jsp/writePro.jsp</title>
</head>
<body>
<h1>WebContent/StudyJSP/jsp/writePro.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String pass = request.getParameter("pass");
String subject = request.getParameter("subject");
String content = request.getParameter("content");
int readcount = 0;
Timestamp date = new Timestamp(System.currentTimeMillis()); // 현재 날짜 출력
Class.forName("com.mysql.jdbc.Driver");
String dbUrl ="jdbc:mysql://localhost:3306/jspdb1";
String dbId = "jspid";
String dbPass = "jsppass";
Connection con = DriverManager.getConnection(dbUrl,dbId,dbPass);
//num값 구하기
String sql1 = "select max(num) from board";
PreparedStatement pstmt = con.prepareStatement(sql1);
ResultSet rs = pstmt.executeQuery(sql1);
int num = 0;
if(rs.next()){
num = rs.getInt("max(num)")+ 1;
}
String sql2 = "insert into board (num,name,pass,subject,content,readcount,date) values(?,?,?,?,?,?,?) ";
pstmt = con.prepareStatement(sql2);
pstmt.setInt(1,num);
pstmt.setString(2,name);
pstmt.setString(3,pass);
pstmt.setString(4,subject);
pstmt.setString(5,content);
pstmt.setInt(6,readcount);
pstmt.setTimestamp(7,date);
pstmt.executeUpdate();
// response.sendRedirect("boardList.jsp");
%>
<script type="text/javascript">
alert("글쓰기 성공");
location.href="boardList.jsp";
</script>
</body>
</html>
반응형
3단계(글목록)
boardList.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/Study/jsp/boardList.jsp</title>
</head>
<body>
<h1>WebContent/Study/jsp/boardList.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
Class.forName("com.mysql.jdbc.Driver");
String dbUrl ="jdbc:mysql://localhost:3306/jspdb1";
String dbId = "jspid";
String dbPass = "jsppass";
Connection con = DriverManager.getConnection(dbUrl,dbId,dbPass);
String sql = "select * from board order by num desc";
PreparedStatement pstmt = con.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
if(!rs.next()){%>
<table border="1">
<tr>
<td>글자번호</td>
<td>글쓴이</td>
<td>제목</td>
<td>글내용</td>
<td>날짜</td>
<td>조회수</td>
</tr>
<tr>
<td colspan ="6">글없음</td>
</tr>
</table>
<%}else{
%>
<table border="1">
<tr>
<td>글자번호</td>
<td>글쓴이</td>
<td>제목</td>
<td>글내용</td>
<td>날짜</td>
<td>조회수</td>
</tr>
<%while(rs.next()){ %>
<tr>
<td><%=rs.getInt("num") %></td>
<td><%=rs.getString("name") %></td>
<td><a href="view.jsp?num=<%=rs.getInt("num") %>" title="상세보기"><%=rs.getString("subject") %></a></td>
<td><%=rs.getString("content") %></td>
<td><%=rs.getTimestamp("date") %></td>
<td><%=rs.getInt("readcount") %></td>
</tr>
<%} %>
</table>
<%} %>
<input type="button" value="글쓰기" onclick="location.href='writeForm.jsp'">
</body>
</html>
4단계(글수정)
updateForm.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/StudyJSP/jsp/updateForm.jsp</title>
</head>
<body>
<h1>WebContent/StudyJSP/jsp/updateForm.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
Class.forName("com.mysql.jdbc.Driver");
String dbUrl ="jdbc:mysql://localhost:3306/jspdb1";
String dbId = "jspid";
String dbPass = "jsppass";
Connection con = DriverManager.getConnection(dbUrl,dbId,dbPass);
String sql = "select * from board order by num desc";
PreparedStatement pstmt = con.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
%>
<form action="updatePro.jsp" method="post">
<input type="hidden" name="num" value="<%=rs.getInt("num")%>">
<table>
<tr>
<td>글쓴이</td>
<td><input type="text" name="name" value="<%=rs.getString("name") %>"></td>
</tr>
<tr>
<td>비밀번호</td>
<td><input type="password" name="pass"></td>
</tr>
<tr>
<td>제목</td>
<td><input type="text" name="subject" value="<%=rs.getString("subject")%>"></td>
</tr>
<tr>
<td>글내용</td>
<td><textarea name="content" rows="10" cols="20" ><%=rs.getString("content")%></textarea></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="글수정"></td>
</tr>
</table>
</form>
<%} %>
</body>
</html>
updatePro.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.Timestamp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/StudyJSP/jsp/updatePro.jsp</title>
</head>
<body>
<h1>WebContent/StudyJSP/jsp/updatePro.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String pass = request.getParameter("pass");
String subject = request.getParameter("subject");
String content = request.getParameter("content");
int num = Integer.parseInt(request.getParameter("num"));
Class.forName("com.mysql.jdbc.Driver");
String dbUrl ="jdbc:mysql://localhost:3306/jspdb1";
String dbId = "jspid";
String dbPass = "jsppass";
Connection con = DriverManager.getConnection(dbUrl,dbId,dbPass);
String sql2 = "select pass from board where num =?";
PreparedStatement pstmt = con.prepareStatement(sql2);
pstmt.setInt(1, num);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
if(!rs.getString("pass").equals(pass)){%>
<script type="text/javascript">
alert("비밀번호가 틀립니다.");
history.back();
</script>
<%}else{
String sql = "update board set name=?, subject=?, content=? where num = ? ";
pstmt = con.prepareStatement(sql);
pstmt.setString(1,name);
pstmt.setString(2,subject);
pstmt.setString(3,content);
pstmt.setInt(4,num);
pstmt.executeUpdate();
}
}
%>
<script type="text/javascript">
alert("글수정 성공");
location.href="boardList.jsp";
</script>
</body>
</html>
5단계(글보기)
view.jsp
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/Study/jsp/view.jsp</title>
</head>
<body>
<h1>WebContent/Study/jsp/view.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
Class.forName("com.mysql.jdbc.Driver");
String dbUrl ="jdbc:mysql://localhost:3306/jspdb1";
String dbId = "jspid";
String dbPass = "jsppass";
Connection con = DriverManager.getConnection(dbUrl,dbId,dbPass);
int num = Integer.parseInt(request.getParameter("num"));
String sql = "select * from board where num=?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setInt(1, num);
String sql2 = "update board set readcount=(readcount+1) where num=?";
PreparedStatement pstmt2 = con.prepareStatement(sql2);
pstmt2.setInt(1,num);
pstmt2.executeUpdate();
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
%>
<table border="1">
<tr>
<td>글번호</td>
<td> <%=rs.getString("name") %> </td>
<td>날짜</td>
<td><%=rs.getString("date") %></td>
</tr>
<tr>
<td>글쓴이</td>
<td> <%=rs.getString("name") %> </td>
<td>조회수</td>
<td><%=rs.getString("readcount") %></td>
</tr>
<tr>
<td>제목</td>
<td colspan="3"><%=rs.getString("subject") %></td>
</tr>
<tr>
<td>글내용</td>
<td colspan="3"><%=rs.getString("content") %></td>
</tr>
<tr>
<td colspan="4"><input type="button" value="글목록" onclick="location.href='boardList.jsp'">
<input type="button" value="글수정" onclick="location.href='updateForm.jsp?num=<%=rs.getInt("num")%>'">
<input type="button" value="글삭제" onclick="location.href='deleteForm.jsp?num=<%=rs.getInt("num")%>'"></td>
</tr>
</table>
<%} %>
</body>
</html>
6단계(삭제하기)
deleteForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/StudyJSP/jsp/deleteForm.jsp</title>
</head>
<body>
<form action="deletePro.jsp" method="post">
<input type="hidden" name="num" value="<%=request.getParameter("num")%>">
<table border="1">
<tr>
<td>비밀번호</td>
<td><input type="password" name="pass"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="삭제하기"></td>
</tr>
</table>
</form>
</body>
</html>
deletePro.jsp
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.Timestamp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>WebContent/StudyJSP/jsp/deletePro.jsp</title>
</head>
<body>
<h1>WebContent/StudyJSP/jsp/deletePro.jsp</h1>
<%
request.setCharacterEncoding("utf-8");
String pass = request.getParameter("pass");
int num = Integer.parseInt(request.getParameter("num"));
Class.forName("com.mysql.jdbc.Driver");
String dbUrl ="jdbc:mysql://localhost:3306/jspdb1";
String dbId = "jspid";
String dbPass = "jsppass";
Connection con = DriverManager.getConnection(dbUrl,dbId,dbPass);
String sql2 = "select pass from board where num =?";
PreparedStatement pstmt = con.prepareStatement(sql2);
pstmt.setInt(1, num);
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
if(!rs.getString("pass").equals(pass)){%>
<script type="text/javascript">
alert("비밀번호가 틀립니다.");
history.back();
</script>
<%}else{
String sql = "delete from board where num =? ";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1,num);
pstmt.executeUpdate();
%>
<script type="text/javascript">
alert("글삭제");
location.href="boardList.jsp";
</script>
<%
}
}
%>
</body>
</html>
반응형
'IT > JSP' 카테고리의 다른 글
JSTL(JSP Standard Tag Library) (0) | 2021.01.22 |
---|---|
[JSP] 회원가입 (로그인/로그아웃/정보조회/정보수정/정보삭제/모든정보목록) (0) | 2020.08.25 |
[JSP] Cookie란? (0) | 2020.08.14 |
[JSP] Session이란? (0) | 2020.08.14 |
[JSP] Include태그 (0) | 2020.08.13 |