1. BoardDBBean.java
public ArrayList<BoardBean> listBoard() throws Exception {
String selectQuery = "SELECT B_ID, B_NAME, B_EMAIL, B_TITLE, B_CONTENT, B_DATE, B_HIT, B_PWD, B_IP FROM boardT ORDER BY B_ID";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
ArrayList<BoardBean> list = new ArrayList<BoardBean>();
try {
conn = getConnection();
pstmt = conn.prepareStatement(selectQuery);
rs = pstmt.executeQuery();
while (rs.next()) {
BoardBean bb = new BoardBean();
bb.setB_id(rs.getInt(1));
bb.setB_name(rs.getString(2));
bb.setB_email(rs.getString(3));
bb.setB_title(rs.getString(4));
bb.setB_content(rs.getString(5));
bb.setB_date(rs.getTimestamp(6));
bb.setB_hit(rs.getInt(7));
bb.setB_pwd(rs.getString(8));
bb.setB_ip(rs.getString(9));
list.add(bb); // 반복하면서 list 객체에 bb객체 내용을 담음
}
} catch (SQLException ex) {
System.out.print("조회 실패");
ex.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return list;
}
public BoardBean getBoard(int b_id, boolean hitAdd) throws Exception {
String sql = "";
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
BoardBean bb = null;
try {
if (hitAdd == true) { // 게시글 볼때는 true, 수정할 때는 false
sql = "UPDATE boardT SET B_HIT=B_HIT+1 WHERE B_ID = ?";
// 조회수 수정하는 쿼리문 해당 메소드가 실행될 때 조회수 1씩 증가해서 업데이트 쿼리 실행
conn = getConnection();
;
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, b_id);
pstmt.executeUpdate(); // 쿼리 실행
} else {
conn = getConnection();
}
sql = "SELECT B_ID, B_EMAIL, B_TITLE, B_NAME, B_CONTENT, B_DATE, B_HIT, B_PWD, B_IP FROM boardT where B_ID = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, b_id);
rs = pstmt.executeQuery();
if (rs.next()) {
bb = new BoardBean();
bb.setB_id(b_id);
bb.setB_name(rs.getString("B_NAME"));
bb.setB_email(rs.getString("B_EMAIL"));
bb.setB_title(rs.getString("B_TITLE"));
bb.setB_content(rs.getString("B_CONTENT"));
bb.setB_date(rs.getTimestamp("B_DATE"));
bb.setB_hit(rs.getInt("B_HIT"));
bb.setB_pwd(rs.getString("B_PWD"));
bb.setB_ip(rs.getString("B_IP"));
}
} catch (SQLException ex) {
System.out.print("조회 실패");
ex.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return bb;
}
- ArrayList<T> : 배열의 크기가 가변인 배열, add() 메소드로 배열 값 추가, set() 메소드로 배열 값 수정, remove() 메소드로 배열 값 삭제
- <T>(제네릭) : 클래스 내부에서 타입을 지정하는 것이 아닌 외부에서 사용자에 의해 지정되는 것을 의미
2. list.jsp
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="magic.board.BoardBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<% request.setCharacterEncoding("UTF-8"); %>
<%
BoardDBBean bdb = BoardDBBean.getInstance();
ArrayList<BoardBean> list = bdb.listBoard();
int b_id,b_hit;
String b_name, b_email, b_title, b_content;
Timestamp b_date;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
// Timestamp로 받아온 날짜의 형식을 지정
%>
<!DOCTYPE html>
<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>
<table border="1" width="800">
<tr height="25">
<td width="40" align="center">번호</td>
<td width="450" align="center">글제목</td>
<td width="120" align="center">작성자</td>
<td width="130" align="center">작성일</td>
<td width="60" align="center">조회수</td>
</tr>
<%
for(int i=0; i < list.size(); i++) {
BoardBean board = list.get(i); // 배열에 넣은 역순으로 board 객체에 값을 넣어줌
b_id=board.getB_id();
b_name=board.getB_name();
b_email=board.getB_email();
b_title= board.getB_title();
b_date= board.getB_date();
b_hit= board.getB_hit();
%>
<tr height="25" bgcolor="#E8E8E8" onmouseover="this.style.backgroundColor='#AEBAB4'"
onmouseout="this.style.backgroundColor='#E8E8E8'">
<td align="center"><%= b_id %></td>
<td id="title" >
<a href="show.jsp?b_id=<%=b_id %>"><%= b_title %></a>
</td>
<td align="center">
<a href="mailto:<%= b_email %>>"><!-- 작성자에게 메일을 보내는 a태그 -->
<%= b_name %>
</a>
</td>
<td align="center"><%= sdf.format(b_date) %></a>
</td>
</td>
<td align="center"><%= b_hit %></a>
</td>
</tr>
<% } %>
</table>
</center>
</body>
</html>
3. show.jsp
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="magic.board.BoardBean"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ 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>
<%
BoardDBBean bdb = BoardDBBean.getInstance();
BoardBean bb = bdb.getBoard(Integer.parseInt(request.getParameter("b_id")),true);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
%>
<center>
<h1>글 내 용 보 기</h1>
<table border="1" width="600" cellspacing="0">
<tr height="30" align="center">
<td width="100">글번호</td>
<td width="200"><%= bb.getB_id() %></td>
<td width="100">조회수</td>
<td width="200"><%= bb.getB_hit() %></td>
</tr>
<tr height="30" align="center">
<td width="100" >작성자</td>
<td width="200"><%= bb.getB_name() %></td>
<td width="100" >작성일</td>
<td width="200"><%= sdf.format(bb.getB_date()) %></td>
<tr height="30">
<td width="100" align="center">글제목</td>
<td width="200" colspan="3"><%= bb.getB_title() %></td>
</tr>
<tr height="30">
<td width="100" align="center">글내용</td>
<td width="200" colspan="3"><%= bb.getB_content() %></td>
</tr>
<tr height="30">
<td colspan="4" align="right">
<input type="button" value="글수정" onclick="location.href='edit.jsp?b_id=<%= bb.getB_id() %>'" />
<input type="button" value="글삭제" onclick="location.href='delete.jsp?b_id=<%= bb.getB_id() %>'" />
<input type="button" value="글목록" onclick="location.href='list.jsp'" />
</td>
</tr>
</table>
</center>
</body>
</html>
'java,jsp,spring > JSP' 카테고리의 다른 글
JSP 게시판 (4) 글 삭제 (0) | 2022.09.06 |
---|---|
JSP 게시판 (3) 글 수정 (0) | 2022.09.06 |
JSP 게시판 (1) 글쓰기 (0) | 2022.09.06 |
JSP 게시판 (0) | 2022.09.06 |
JSP 예제 - 사용자 관리(3단계) (0) | 2022.09.03 |