학원/JSP - 학원
게시판 1단계 5~6
수풀속의고라니
2022. 3. 28. 16:08
728x90
그냥 다 한줄 정도씩만 추가해주면 됨
write_ok.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<%@page import="java.sql.Timestamp"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<% request.setCharacterEncoding("utf-8"); %>
<jsp:useBean id="board" class="magic.board.BoardBean"></jsp:useBean>
<jsp:setProperty property="*" name="board"/>
<%
BoardDBBean manager = BoardDBBean.getInstance();
board.setB_date(new Timestamp(System.currentTimeMillis()));
if(manager.insertBoard(board)==1){
response.sendRedirect("list.jsp");
}else{
response.sendRedirect("write.jsp");
}
%>
|
cs |
list.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="java.util.ArrayList"%>
<%@page import="magic.member.MemberBean"%>
<%@page import="magic.board.BoardDBBean"%>
<%@page import="magic.board.BoardBean"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
BoardDBBean bd = BoardDBBean.getInstance();
ArrayList<BoardBean> boardList = bd.listBoard();
int b_id, b_hit = 0;
String b_name, b_email, b_title, b_content;
Timestamp b_date;
%>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>게시판에 등록된 글 목록 보기</h1>
<table width="600">
<tr>
<td align="right">
<a href="write.jsp">글쓰기</a>
</td>
</tr>
</table>
</center>
<center>
<table border="1" width="800" cellspacing="0">
<tr height="25">
<td width="40" align="center">번호</td>
<td width="350" align="center">글제목</td>
<td width="120" align="center">작성자</td>
<td width="120" align="center">작성일</td>
<td width="50" align="center">조회수</td>
</tr>
<%
for(int i=0;i<boardList.size();i++){
BoardBean board = boardList.get(i);
b_id = board.getB_id();
b_name = board.getB_name();
b_email = board.getB_email();
b_title = board.getB_title();
b_content = board.getB_content();
b_date = board.getB_date();
b_hit = board.getB_hit();
%>
<tr height="25" bgcolor="#f7f7f7"
onmouseover="this.style.backgroundColor='#eeeeef'"
onmouseout="this.style.backgroundColor='#f7f7f7'">
<td align="center"><%= b_id%></td>
<td><a href="show.jsp?b_id=<%= b_id%>"><%= b_title %></a></td>
<td align="center">
<a href="mailto:<%=b_email %>">
<%= b_name %>
</a>
</td>
<td><%=sdf.format(b_date) %></td>
<td><%= b_hit %></td>
</tr>
<%
}
%>
</table>
</center>
</body>
</html>
|
cs |
- 날짜랑 조회수 <td>에 가운데 정렬
show.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
|
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<jsp:useBean id="board" class="magic.board.BoardBean"></jsp:useBean>
<jsp:setProperty property="*" name="board"/>
<%
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
int id = Integer.parseInt(request.getParameter("b_id"));
BoardDBBean db = BoardDBBean.getInstance();
board = db.getBoard(id);
%>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<center>
<table border="1" cellspacing="0">
<h1>글 내 용 보 기</h1>
<form>
<tr align="center">
<td width="160">글번호</td>
<td width="160"><%= board.getB_id() %></td>
<td width="160">조회수</td>
<td width="240"><%= board.getB_hit() %></td>
</tr>
<tr align="center">
<td width="160">작성자</td>
<td width=160><%= board.getB_name() %></td>
<td width="160">작성일</td>
<%-- <td width="240"><%= board.getB_date() %></td> --%>
<td width="240"><%= sdf.format(board.getB_date()) %></td>
</tr>
<tr>
<td width="160" align="center">글제목</td>
<td align="left" width="400" colspan="3"><%= board.getB_title() %></td>
</tr>
<tr>
<td width="160" align="center">글내용</td>
<td align="left" width="400" colspan="3"><%= board.getB_content() %></td>
</tr>
</form>
</table>
</center>
</body>
</html>
|
cs |
BoardDBBean.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
package magic.board;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import magic.member.MemberBean;
public class BoardDBBean {
private static BoardDBBean instance = new BoardDBBean();
public static BoardDBBean getInstance() {
return instance;
}
public Connection getConnection() throws Exception {
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/oracle");
return ds.getConnection();
}
public int insertBoard(BoardBean board) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql="";
int re = -1;
int number;
try {
con = getConnection();
sql = "select max(b_id) from boardT";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()) {
number = rs.getInt(1)+1;
}else {
number=1;
}
sql = "insert into boardT (b_id,b_name,b_email,b_title,b_content,b_date,b_hit) values(?,?,?,?,?,?,?)";
ps = con.prepareStatement(sql);
ps.setInt(1, number);
ps.setString(2, board.getB_name());
ps.setString(3, board.getB_email());
ps.setString(4, board.getB_title());
ps.setString(5, board.getB_content());
ps.setTimestamp(6, board.getB_date());
ps.setInt(7, board.getB_hit());
ps.executeUpdate();
re = 1;
ps.close();
con.close();
System.out.println("추가 성공");
} catch (Exception e) {
System.out.println("추가 실패");
e.printStackTrace();
}finally {
try {
if (ps != null) ps.close();
if (con != null) con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return re;
}
public ArrayList<BoardBean> listBoard(){
ArrayList<BoardBean> boardList = new ArrayList<BoardBean>();
Connection con = null;
Statement st = null;
ResultSet rs = null;;
String sql = "select * from boardt order by b_id";
try {
con = getConnection();
st = con.createStatement();
rs = st.executeQuery(sql);
while (rs.next()) {
BoardBean board = new BoardBean();
board.setB_id(rs.getInt("b_id"));
board.setB_name(rs.getString("b_name"));
board.setB_email(rs.getString("b_email"));
board.setB_title(rs.getString("b_title"));
board.setB_content(rs.getString("b_content"));
board.setB_date(rs.getTimestamp("b_date"));
board.setB_hit(rs.getInt("b_hit"));
boardList.add(board);
}
rs.close();
st.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(rs != null) rs.close();
if(st != null) st.close();
if(con != null) con.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return boardList;
}
public BoardBean getBoard(int id) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
BoardBean board = new BoardBean();
String sql = "";
try {
con = getConnection();
sql = "update boardt set b_hit=b_hit+1 where b_id=?";
ps = con.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
sql = "select * from boardT where b_id=?";
ps = con.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
board.setB_id(rs.getInt("b_id"));
board.setB_name(rs.getString("b_name"));
board.setB_email(rs.getString("b_email"));
board.setB_title(rs.getString("b_title"));
board.setB_content(rs.getString("b_content"));
board.setB_date(rs.getTimestamp("b_date"));
board.setB_hit(rs.getInt("b_hit"));
}
rs.close();
ps.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return board;
}
}
|
cs |
728x90