반응형
(안녕하세요. 삶의 질을 높이고자 노력하는 영차영차입니다.
직장생활과 병행하여 22년3월을 기한으로 게시판 만들기에 도전해보고자 합니다.
해당 포스팅은 그 과정에 대한 순수 기록 목적입니다)
---------------------------------------------------------------------------------------------
배운 것을 토대로 일단 만들어봄.
점점 더 기능을 확장시켜보자.
BoardVo.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package com.tistory.mabb.freeboard;
import java.util.Date;
public class BoardVo {
int num;
String sub;
String content;
String writer;
Date writetime; //mysql의 timestamp와 호환이 되는 데이터유형으로 설정을 해야한다.
public BoardVo() {
}
//생성자
public BoardVo(int num,String sub,String content,String writer,Date writetime) {
this.num = num;
this.sub = sub;
this.content = content;
this.writer = writer;
this.writetime = writetime;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getSub() {
return sub;
}
public void setSub(String sub) {
this.sub = sub;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public Date getWritetime() {
return writetime;
}
public void setWritetime(Date writetime) {
this.writetime = writetime;
}
}
|
cs |
BoardDao.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
package com.tistory.mabb.freeboard.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import com.tistory.mabb.freeboard.BoardVo;
public class BoardDao {
private static String dburl = "jdbc:mysql://localhost:3306/board";
private static String dbuser = "root";
private static String dbpassword = "0000";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
public ArrayList<BoardVo> selectBoard() {
ArrayList<BoardVo> list = new ArrayList<BoardVo>();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dburl, dbuser, dbpassword);
String sql = "SELECT * FROM freeboard ORDER BY num desc";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
int num = rs.getInt("num");
String sub = rs.getString("sub");
String content = rs.getString("content");
String writer = rs.getString("writer");
Date writetime = rs.getDate("writetime");
BoardVo vo = new BoardVo(num, sub, content, writer, writetime);
list.add(vo);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(rs != null) {
try {rs.close();} catch (Exception e2) {e2.printStackTrace();}
}
if(ps != null) {
try {ps.close();} catch (Exception e2) {e2.printStackTrace();}
}
if(conn != null) {
try {conn.close();} catch (Exception e2) {e2.printStackTrace();}
}
}
return list;
}
public void insertBoard(String sub,String content,String writer) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dburl, dbuser, dbpassword);
String sql = "insert into freeboard(num,sub,content,writer) "
+ "values( (select num from (select max(num)+1 as num from freeboard)as freeboard_t),"
+ "?,?,?);";
ps = conn.prepareStatement(sql);
ps.setString(1, sub);
ps.setString(2, content);
ps.setString(3, writer);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(ps != null) {
try {ps.close();} catch (Exception e2) {e2.printStackTrace();}
}
if(conn != null) {
try {conn.close();} catch (Exception e2) {e2.printStackTrace();}
}
}
}
public BoardVo contentBoard(int num_c) {
BoardVo vo = new BoardVo();
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(dburl, dbuser, dbpassword);
String sql = "SELECT * FROM freeboard WHERE num = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, num_c);
rs = ps.executeQuery();
while (rs.next()) {
vo.setNum(rs.getInt("num"));
vo.setSub(rs.getString("sub"));
vo.setContent(rs.getString("content"));
vo.setWriter(rs.getString("writer"));
vo.setWritetime(rs.getDate("writetime"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(rs != null) {
try {rs.close();} catch (Exception e2) {e2.printStackTrace();}
}
if(ps != null) {
try {ps.close();} catch (Exception e2) {e2.printStackTrace();}
}
if(conn != null) {
try {conn.close();} catch (Exception e2) {e2.printStackTrace();}
}
}
return vo;
}
}
|
cs |
MainServlet.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package com.tistory.mabb.freeboard.mainsevlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tistory.mabb.freeboard.BoardVo;
import com.tistory.mabb.freeboard.dao.BoardDao;
public class MainServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public MainServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
BoardDao dao = new BoardDao();
ArrayList<BoardVo> list = dao.selectBoard();
request.setAttribute("list", list);
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/BoardMain.jsp");
requestDispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
|
cs |
InsertServlet.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package com.tistory.mabb.freeboard.mainsevlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tistory.mabb.freeboard.dao.BoardDao;
public class InsertServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public InsertServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
RequestDispatcher requestDispatcer = request.getRequestDispatcher("/BoardInsert.jsp");
requestDispatcer.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
String sub = request.getParameter("sub");
String content = request.getParameter("content");
String writer = request.getParameter("writer");
BoardDao dao = new BoardDao();
dao.insertBoard(sub, content, writer);
response.sendRedirect("Main");
}
}
|
cs |
ContentServlet.java
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package com.tistory.mabb.freeboard.mainsevlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tistory.mabb.freeboard.BoardVo;
import com.tistory.mabb.freeboard.dao.BoardDao;
public class ContentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public ContentServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");
String num_c = request.getParameter("num_c");
BoardDao dao = new BoardDao();
BoardVo vo = dao.contentBoard(Integer.parseInt(num_c));
request.setAttribute("vo", vo);
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/BoardContent.jsp");
requestDispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
|
cs |
BoardMain.jsp
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
32
33
34
35
36
37
38
39
40
41
42
43
|
<%@page import="java.util.Date"%>
<%@page import="java.util.List"%>
<%@page import="com.tistory.mabb.freeboard.BoardVo"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>영차영차게시판jsp</h1>
<hr>
<table>
<thead>
<tr><th>번호</th><th>제목</th><th>작성자</th><th>날짜</th></tr>
</thead>
<%
List<BoardVo> list = (List) request.getAttribute("list");
for(BoardVo vo:list){
int num = vo.getNum();
String sub = vo.getSub();
String writer = vo.getWriter();
Date writetime = vo.getWritetime();
%>
<tbody>
<tr><td><%=num%></td><td><a href = "Content?num_c=<%=num%>"><%=sub%></a></td><td><%=writer%></td><td><%=writetime%></td></tr>
</tbody>
<%
}
%>
</table>
<form action = "Insert" >
<input name="insertbutt" type = "submit" value = "작성">
</form>
</body>
</html>
|
cs |
B
BoardInsert.jsp
B
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>글 작성 페이지</h1>
<form action = "Insert" method ="POST" name = "insertBoard">
제목:<input name = "sub" type = "text">작성자:<input name = "writer" type = "text"><br>
내용:<textarea name = "content" type = "text" cols ="50" rows ="30"></textarea><br>
<input name = "subm" type = "submit" value ="저장"><br>
<input name = "listmove" href = "Main" type ="submit" value="목록">
</form>
</body>
</html>
|
cs |
BoardContent.jsp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
<%@page import="java.util.Date"%>
<%@page import="com.tistory.mabb.freeboard.dao.BoardDao"%>
<%@page import="com.tistory.mabb.freeboard.BoardVo"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>게시글 상세보기</h1>
<hr>
<%
BoardVo vo = (BoardVo)request.getAttribute("vo");
int num = vo.getNum();
String sub = vo.getSub();
String content = vo.getContent();
String writer = vo.getWriter();
Date writetime = vo.getWritetime();
%>
<table>
<thead>
<tr>
<th>글번호</th><th><%= num %></th><th>작성자</th><th><%=writer %></th><th>작성시간</th><th><%=writetime %></th>
</tr>
<tr>
<th>제목</th><th><%=sub %></th>
</tr>
</thead>
<tbody>
<tr>
<td><%=content %></td>
</tr>
</tbody>
</table>
<form action = "Main">
<input type="submit" value ="목록">
</form>
</body>
</html>
|
cs |
반응형
'개발 > 코딩' 카테고리의 다른 글
[프로그래밍] 프로그래머스 예제문제 풀어보기 < 신고 결과 받기> (25점..) (0) | 2022.03.12 |
---|---|
[프로그래밍] JAVA 웹 개발 공부 계획, 뭘 공부해야 하는지도 중요하다. (0) | 2022.03.11 |
[프로그래밍] 게시판 만들기 도전,220301,jsp 뷰 (0) | 2022.03.01 |
[프로그래밍] 게시판 만들기 도전,220301, 배운 것을 토대로 연습 (0) | 2022.03.01 |
[프로그래밍] 게시판 만들기 도전,220301, 전일 복습, static (0) | 2022.03.01 |