<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<script language="JavaScript" src="board.js" charset="utf-8"></script>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>글올리기</h1>
<form name="reg_frm" method="post" action="write_ok.jsp">
<table align ="center">
<tr height ="30">
<td width="80">작성자 </td>
<td width="140">
<input type="text" name="b_name" size="10" maxlength="20">
</td>
<td ="80">이메일</td>
<td width="240">
<input type="text" name="b_email"size="24" maxlength="50">
</td>
</tr>
<tr height ="30">
<td width ="80">글제목 </td>
<td colspan="3" width="460">
<input type="text"name="b_title" size="55" maxlength="80">
</td>
</tr>
<tr>
<td colspan="4" align ="center">
<textarea cols="65" rows="10" name="b_content"></textarea>
</td>
</tr>
<tr height ="50" align ="center">
<td colspan = "4" >
<input type="button" value="글쓰기" onclick="check_ok()">
<input type="reset" value="다시입력">
<input type="button" value="글목록" onclick="location.href='list.jsp'">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<jsp:useBean id="board" class="magic.board.BoardBean"/>
<jsp:setProperty property="*" name="board"/>
<%
BoardDBBean db = BoardDBBean.getInstance();
//instance를 db객체로 받음.
int re = db.insertBoard(board);//분기처리
System.out.println("@@@###getMem_uid ===>"+ re); //getMem_uid를 잘 받는지 확인
if(re == 1) {//분기처리
%>
<script>
alert("글을 등록 하셨습니다.");
</script>
<%
response.sendRedirect("list.jsp"); //이동
}else {
%>
<script>
alert("글 등록에 실패했습니다.");
</script>
<%
response.sendRedirect("write.jsp"); //이동
}
%>
package magic.board;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class BoardDBBean {
//1. 전역 BoardDBBean 객체 레퍼런스를 리턴하는 메소드
private static BoardDBBean instance = new BoardDBBean();
public static BoardDBBean getInstance(){
return instance;
//getConnection이나 getInstance는 계속 사용하기 좋음(재사용하기 좋다는 뜻)
//jsp에서 메소드 호출하면 편하기 때문
}
//2.쿼리 작업에 사용할 커넥션 객체를 리턴하는 메소드
public Connection getConnection( ) throws Exception{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/oracle");
//경로 찾기
return ds.getConnection();
//getConnection이나 getInstance는 계속 사용하기 좋음(재사용하기 좋다는 뜻)
//jsp에서 메소드 호출하면 편하기 때문
}
//3. 전달인자로 받은 BoardBean board를 BOARDT 테이블에 삽입하는 메소드
public int insertBoard(BoardBean board) throws Exception{
int re = -1;
int number;
Connection conn = null;//데이터 베이스 접속
PreparedStatement pstmt = null;// DB에 SQL 전달
ResultSet rs = null;
String sql ="";
try {
conn = getConnection();
sql = "SELECT MAX(B_ID) FROM BOARDT";//글번호 추가
pstmt = conn.prepareStatement(sql);// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
rs = pstmt.executeQuery();//쿼리 결과를 rs에 받음(select 조회문은 executeQuery 메소드 처리
if(rs.next()) {//다음 쿼리 데이터가 있는 경우를 조건문으로 줌.
number = rs.getInt(1)+1;
//글번호가 가장 큰 값을 조회하여 1증가 한 값을 INSERT쿼리에 추가
} else {
number = 1;
}
sql = "INSERT INTO BOARDT VALUES (?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
// 데이터가 제대로 들어갔는지 확인 해야함
System.out.println("@@@@#### number=====>"+number);
System.out.println("@@@@#### board.getB_name()=====>"+board.getB_name());
System.out.println("@@@@#### board.getB_email()=====>"+board.getB_email());
System.out.println("@@@@#### board.getB_title()=====>"+board.getB_title());
System.out.println("@@@@#### board.getB_content()=====>"+board.getB_content());
pstmt.setInt(1, number);//인덱스 번호 변경 및 글번호 (number) 추가
pstmt.setString(2, board.getB_name());
pstmt.setString(3, board.getB_email());
pstmt.setString(4, board.getB_title());
pstmt.setString(5, board.getB_content());
pstmt.executeUpdate();
re =1;
} catch(SQLException ex) {
System.out.println("추가 실패");
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 re;
}
//4. 리턴타입이 ArrayList<BoardBean> 인 listBoard() 메소드 추가(글목록을 위한 메소드)
public ArrayList<BoardBean> listBoard() throws Exception{//제네릭 사용함 , 파라미터는 BoardBean
Connection conn = null;//데이터 베이스 접속
Statement stmt = null;// DB에 SQL 전달
ResultSet rs = null;
//db 정보 받기 위함
String sql = "SELECT b_id\r\n" +
" , b_name\r\n" +
" , b_email\r\n" +
" , b_title\r\n" +
" , b_content \r\n" +
" FROM BOARDT \r\n" +
" ORDER BY B_ID";//해당 데이터 쿼리 정렬되게끔 함.
ArrayList<BoardBean> boardList = new ArrayList<BoardBean>();//ArrayList로 게시글들을 받음
try {
conn = getConnection();
//pstmt = conn.prepareStatement(sql);// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
stmt = conn.createStatement();// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
rs = stmt.executeQuery(sql);//쿼리 결과를 rs에 받음(select 조회문은 executeQuery 메소드 처리
while(rs.next()) {//다음(여러개의) 쿼리 데이터가 있는 경우를 조건문으로 줌.
BoardBean board = new BoardBean();
board.setB_id(rs.getInt(1));//아이디는 INT이므로 getInt로 해야함
board.setB_name(rs.getString(2));
board.setB_email(rs.getString(3));
board.setB_title(rs.getString(4));
board.setB_content(rs.getString(5));
// 데이터가 제대로 들어갔는지 확인 해야함
// System.out.println("@@@@#### board.getB_id()=====>"+board.getB_id());
// System.out.println("@@@@#### board.getB_name()=====>"+board.getB_name());
// System.out.println("@@@@#### board.getB_email()=====>"+board.getB_email());
// System.out.println("@@@@#### board.getB_title()=====>"+board.getB_title());
// System.out.println("@@@@#### board.getB_content()=====>"+board.getB_content());
boardList.add(board);//반복하면서 게시글들을 쌓음
}
} catch(SQLException ex) {
ex.printStackTrace();
} finally {
try{//자원반납(순서 중요)
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return boardList;
}
}
<%@page import="magic.board.BoardDBBean"%>
<%@page import="magic.board.BoardBean"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
BoardDBBean db = BoardDBBean.getInstance();
//listBoard 메소드를 가지고 와야함(제네릭) 그러기 위해서는 객체생성을 먼저해야함
//instance를 db객체로 받음.(getInstace하면 instance가 리턴됨)
ArrayList<BoardBean> boardList = db.listBoard();
//listBoard메소드 호출하여 ArrayList로 받음(리턴타입은 ArrayList<BoardBean>)
String b_name, b_title, b_email, b_content = "";
int b_id;
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<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 width="800" border="1" cellspacing="0">
<!-- <form name="reg_frm" method="post" action="write_ok.jsp"> -->
<tr height="25">
<td width="40" align="center">번호</td>
<td width="450" align="center">글제목</td>
<td width="120" align="center">작성자</td>
</tr>
<%
for(int i=0; i<boardList.size(); i++){//게시글 갯수만큼 반복
BoardBean board = boardList.get(i);
//ArrayList에 담았던 객체들의 값을 가지고 옴
//가지고 올때는 반대방향으로 (listBoard 메소드에서 보낸 값을 반대로 풀면 됨)
//포장했던걸 푸는 느낌
//board객체를 하나씩 풀기
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();
%>
<tr height="25"
onmouseout="this.style.backgroundColor = '#f7f7f7'"
onmouseover="this.style.backgroundColor = '#eeeeef'">
<td align="center">
<%= b_id %>
</td>
<td>
<%= b_title %>
</td>
<td align="center">
<a href="mailto:<%= b_email %>"><%= b_name %></a>
</td>
</tr>
<%
}
%>
<!-- </form> -->
</table>
</center>
</body>
</html>
<%@page import="magic.board.BoardDBBean"%>
<%@page import="magic.board.BoardBean"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
BoardDBBean db = BoardDBBean.getInstance();
//listBoard 메소드를 가지고 와야함(제네릭) 그러기 위해서는 객체생성을 먼저해야함
//instance를 db객체로 받음.(getInstace하면 instance가 리턴됨)
ArrayList<BoardBean> boardList = db.listBoard();
//listBoard메소드 호출하여 ArrayList로 받음(리턴타입은 ArrayList<BoardBean>)
String b_name, b_title, b_email, b_content = "";
int b_id;
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<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 width="800" border="1" cellspacing="0">
<!-- <form name="reg_frm" method="post" action="write_ok.jsp"> -->
<tr height="25">
<td width="40" align="center">번호</td>
<td width="450" align="center">글제목</td>
<td width="120" align="center">작성자</td>
</tr>
<%
for(int i=0; i<boardList.size(); i++){//게시글 갯수만큼 반복
BoardBean board = boardList.get(i);
//ArrayList에 담았던 객체들의 값을 가지고 옴
//가지고 올때는 반대방향으로 (listBoard 메소드에서 보낸 값을 반대로 풀면 됨)
//포장했던걸 푸는 느낌
//board객체를 하나씩 풀기
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();
%>
<tr height="25"
onmouseout="this.style.backgroundColor = '#f7f7f7'"
onmouseover="this.style.backgroundColor = '#eeeeef'">
<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>
</tr>
<%
}
%>
<!-- </form> -->
</table>
</center>
</body>
</html>
package magic.board;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class BoardDBBean {
//1. 전역 BoardDBBean 객체 레퍼런스를 리턴하는 메소드
private static BoardDBBean instance = new BoardDBBean();
public static BoardDBBean getInstance(){
return instance;
//getConnection이나 getInstance는 계속 사용하기 좋음(재사용하기 좋다는 뜻)
//jsp에서 메소드 호출하면 편하기 때문
}
//2.쿼리 작업에 사용할 커넥션 객체를 리턴하는 메소드
public Connection getConnection( ) throws Exception{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/oracle");
//경로 찾기
return ds.getConnection();
//getConnection이나 getInstance는 계속 사용하기 좋음(재사용하기 좋다는 뜻)
//jsp에서 메소드 호출하면 편하기 때문
}
//3. 전달인자로 받은 BoardBean board를 BOARDT 테이블에 삽입하는 메소드
public int insertBoard(BoardBean board) throws Exception{
int re = -1;
int number;
Connection conn = null;//데이터 베이스 접속
PreparedStatement pstmt = null;// DB에 SQL 전달
ResultSet rs = null;
String sql ="";
try {
conn = getConnection();
sql = "SELECT MAX(B_ID) FROM BOARDT";//글번호 추가
pstmt = conn.prepareStatement(sql);// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
rs = pstmt.executeQuery();//쿼리 결과를 rs에 받음(select 조회문은 executeQuery 메소드 처리
if(rs.next()) {//다음 쿼리 데이터가 있는 경우를 조건문으로 줌.
number = rs.getInt(1)+1;
//글번호가 가장 큰 값을 조회하여 1증가 한 값을 INSERT쿼리에 추가
} else {
number = 1;
}
sql = "INSERT INTO BOARDT VALUES (?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
// 데이터가 제대로 들어갔는지 확인 해야함
System.out.println("@@@@#### number=====>"+number);
System.out.println("@@@@#### board.getB_name()=====>"+board.getB_name());
System.out.println("@@@@#### board.getB_email()=====>"+board.getB_email());
System.out.println("@@@@#### board.getB_title()=====>"+board.getB_title());
System.out.println("@@@@#### board.getB_content()=====>"+board.getB_content());
pstmt.setInt(1, number);//인덱스 번호 변경 및 글번호 (number) 추가
pstmt.setString(2, board.getB_name());
pstmt.setString(3, board.getB_email());
pstmt.setString(4, board.getB_title());
pstmt.setString(5, board.getB_content());
pstmt.executeUpdate();
re =1;
} catch(SQLException ex) {
System.out.println("추가 실패");
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 re;
}
//4. 리턴타입이 ArrayList<BoardBean> 인 listBoard() 메소드 추가(글목록을 위한 메소드)
public ArrayList<BoardBean> listBoard() throws Exception{//제네릭 사용함 , 파라미터는 BoardBean
Connection conn = null;//데이터 베이스 접속
Statement stmt = null;// DB에 SQL 전달
ResultSet rs = null;
//db 정보 받기 위함
String sql = "SELECT b_id\r\n" +
" , b_name\r\n" +
" , b_email\r\n" +
" , b_title\r\n" +
" , b_content \r\n" +
" FROM BOARDT \r\n" +
" ORDER BY B_ID";//해당 데이터 쿼리 정렬되게끔 함.
ArrayList<BoardBean> boardList = new ArrayList<BoardBean>();//ArrayList로 게시글들을 받음
try {
conn = getConnection();
//pstmt = conn.prepareStatement(sql);// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
stmt = conn.createStatement();// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
rs = stmt.executeQuery(sql);//쿼리 결과를 rs에 받음(select 조회문은 executeQuery 메소드 처리
while(rs.next()) {//다음(여러개의) 쿼리 데이터가 있는 경우를 조건문으로 줌.
BoardBean board = new BoardBean();
//결과값을 세팅
board.setB_id(rs.getInt(1));//아이디는 INT이므로 getInt로 해야함
board.setB_name(rs.getString(2));
board.setB_email(rs.getString(3));
board.setB_title(rs.getString(4));
board.setB_content(rs.getString(5));
// 데이터가 제대로 들어갔는지 확인 해야함
// System.out.println("@@@@#### board.getB_id()=====>"+board.getB_id());
// System.out.println("@@@@#### board.getB_name()=====>"+board.getB_name());
// System.out.println("@@@@#### board.getB_email()=====>"+board.getB_email());
// System.out.println("@@@@#### board.getB_title()=====>"+board.getB_title());
// System.out.println("@@@@#### board.getB_content()=====>"+board.getB_content());
boardList.add(board);//반복하면서 게시글들을 쌓음
}
} catch(SQLException ex) {
ex.printStackTrace();
} finally {
try{//자원반납(순서 중요)
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return boardList;
}
// 5. 리턴타입이 BoardBean인 getBoard() 메소드 추가
public BoardBean getBoard (int bid) throws Exception{//글번호만 넘기면 알아서 내용이 넘어가는 메소드
Connection conn = null;//데이터 베이스 접속
PreparedStatement pstmt = null;// DB에 SQL 전달
ResultSet rs = null;
//db 정보 받기 위함
String sql = "SELECT b_id\r\n" +
" , b_name\r\n" +
" , b_email\r\n" +
" , b_title\r\n" +
" , b_content\r\n" +
" FROM BOARDT\r\n" +
" WHERE b_id=?";//해당 번호의 데이터 출력되게 쿼리 작성
BoardBean board = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);//pstmt객체로 받음
// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
pstmt.setInt(1, bid);//조건이 하나이기 때문에 (?가 하나임)
rs = pstmt.executeQuery();//쿼리 결과를 rs에 받음(select 조회문은 executeQuery 메소드 처리
if(rs.next()) {//다음(여러개의) 쿼리 데이터가 있는 경우를 조건문으로 줌.
board = new BoardBean();//객체생성(리턴값이 board)
board.setB_id(rs.getInt(1));
//board.setB_id(bid);도 가능함
board.setB_name(rs.getString(2));
board.setB_email(rs.getString(3));
board.setB_title(rs.getString(4));
board.setB_content(rs.getString(5));
//
//// 데이터가 제대로 들어갔는지 확인 해야함
// System.out.println("@@@@#### board.setB_id()=====>"+board.getB_id());
// System.out.println("@@@@#### board.setB_name()=====>"+board.getB_name());
// System.out.println("@@@@#### board.setB_email()=====>"+board.getB_email());
// System.out.println("@@@@#### board.setB_title()=====>"+board.getB_title());
// System.out.println("@@@@#### board.setB_content()=====>"+board.getB_content());
}
} catch(SQLException ex) {
ex.printStackTrace();
} finally {
try{//자원반납(순서 중요)
if(rs != null) rs.close();
if(conn != null) conn.close();
if(pstmt != null) pstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
return board;
}
}
<%@page import="magic.board.BoardBean"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<%
int bid = Integer.parseInt(request.getParameter("b_id"));
//list.jsp에서 <a href="show.jsp?b_id=..> 에서 b_id가 넘어옴
//그후 캐스팅!!
BoardDBBean db = BoardDBBean.getInstance();
//getBoard 메소드를 가지고 와야함(제네릭) 그러기 위해서는 객체생성을 먼저해야함
//getBoard db객체로 받음.(getInstace하면 instance가 리턴됨)
BoardBean board =db.getBoard(bid);
//getBoard 메소드 호출하여 BoardBean 받음(리턴타입은 board)
/* String b_name = "";
String b_title = "";
String b_email = "";
String b_content = ""; */
//오류나서 하나하나 넣어줌
/* if(board != null){
int 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();
} */
%>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>글 내 용 보 기</h1>
<table width="600" border="1" cellspacing="0">
<tr height="30" align="center">
<td width="100">글번호</td>
<td width="200">
<%= bid %>
</td>
</tr>
<tr height="30" align="center">
<td width="100">작성자</td>
<td width="200"><%= board.getB_name() %></td>
<%-- board 객체를 이용하여 getB_.. 사용! --%>
</tr>
<tr height="30" align="center">
<td width="100">글제목</td>
<td width="200" align="left"><%= board.getB_title() %></td>
</tr>
<tr height="30" align="center">
<td width="100">글내용</td>
<td width="200" align="left"><%= board.getB_content() %></td>
</tr>
</table>
</center>
</body>
</html>
1-5 PPT
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="magic.board.BoardBean"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<%
int bid = Integer.parseInt(request.getParameter("b_id"));
//list.jsp에서 <a href="show.jsp?b_id=..> 에서 b_id가 넘어옴
//그후 캐스팅!!
BoardDBBean db = BoardDBBean.getInstance();
//getBoard 메소드를 가지고 와야함(제네릭) 그러기 위해서는 객체생성을 먼저해야함
//getBoard db객체로 받음.(getInstace하면 instance가 리턴됨)
BoardBean board =db.getBoard(bid);
//getBoard 메소드 호출하여 BoardBean 받음(리턴타입은 board)
Timestamp b_date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
/* String b_name = "";
String b_title = "";
String b_email = "";
String b_content = ""; */
//오류나서 하나하나 넣어줌
/* if(board != null){
int 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();
} */
%>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>글 내 용 보 기</h1>
<table width="600" border="1" cellspacing="0">
<tr height="30" align="center">
<td width="100">글번호</td>
<td width="200">
<%= bid %>
</td>
</tr>
<tr height="30" align="center">
<td width="100">작성자</td>
<td width="200"><%= board.getB_name() %></td>
<%-- board 객체를 이용하여 getB_.. 사용! --%>
<td width="100">작성일</td>
<td width="200"><%= sdf.format(board.getB_date()) %></td>
<%-- board 객체를 이용하여 getB_.. 사용! --%>
</tr>
<tr height="30" align="center">
<td width="100" >글제목</td>
<td width="200" align="left" colspan = "3">
<%= board.getB_title() %>
<%-- board 객체를 이용하여 getB_.. 사용! --%>
</td>
</tr>
<tr height="30" align="center">
<td width="100">글내용</td>
<td width="200" align="left" colspan = "3">
<%= board.getB_content() %>
<%-- board 객체를 이용하여 getB_.. 사용! --%>
</td>
</tr>
</table>
</center>
</body>
</html>
package magic.board;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class BoardDBBean {
//1. 전역 BoardDBBean 객체 레퍼런스를 리턴하는 메소드
private static BoardDBBean instance = new BoardDBBean();
public static BoardDBBean getInstance(){
return instance;
//getConnection이나 getInstance는 계속 사용하기 좋음(재사용하기 좋다는 뜻)
//jsp에서 메소드 호출하면 편하기 때문
}
//2.쿼리 작업에 사용할 커넥션 객체를 리턴하는 메소드
public Connection getConnection( ) throws Exception{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/oracle");
//경로 찾기
return ds.getConnection();
//getConnection이나 getInstance는 계속 사용하기 좋음(재사용하기 좋다는 뜻)
//jsp에서 메소드 호출하면 편하기 때문
}
//3. 전달인자로 받은 BoardBean board를 BOARDT 테이블에 삽입하는 메소드
public int insertBoard(BoardBean board) throws Exception{
int re = -1;
int number;
Connection conn = null;//데이터 베이스 접속
PreparedStatement pstmt = null;// DB에 SQL 전달
ResultSet rs = null;
String sql ="";
try {
conn = getConnection();
sql = "SELECT MAX(B_ID) FROM BOARDT";//글번호 추가
pstmt = conn.prepareStatement(sql);// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
rs = pstmt.executeQuery();//쿼리 결과를 rs에 받음(select 조회문은 executeQuery 메소드 처리
if(rs.next()) {//다음 쿼리 데이터가 있는 경우를 조건문으로 줌.
number = rs.getInt(1)+1;
//글번호가 가장 큰 값을 조회하여 1증가 한 값을 INSERT쿼리에 추가
} else {
number = 1;
}
sql = "INSERT INTO BOARDT VALUES (?,?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
// 데이터가 제대로 들어갔는지 확인 해야함
// System.out.println("@@@@#### number=====>"+number);
// System.out.println("@@@@#### board.getB_name()=====>"+board.getB_name());
// System.out.println("@@@@#### board.getB_email()=====>"+board.getB_email());
// System.out.println("@@@@#### board.getB_title()=====>"+board.getB_title());
// System.out.println("@@@@#### board.getB_content()=====>"+board.getB_content());
// System.out.println("@@@@#### board.getB_date()=====>"+board.getB_date());
pstmt.setInt(1, number);//인덱스 번호 변경 및 글번호 (number) 추가
pstmt.setString(2, board.getB_name());
pstmt.setString(3, board.getB_email());
pstmt.setString(4, board.getB_title());
pstmt.setString(5, board.getB_content());
pstmt.setTimestamp(6, board.getB_date());//날짜 및 시간 저장한걸 추가(setTimestamp)
pstmt.executeUpdate();
re = 1;
} catch(SQLException ex) {
System.out.println("추가 실패");
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 re;
}
//4. 리턴타입이 ArrayList<BoardBean> 인 listBoard() 메소드 추가(글목록을 위한 메소드)
public ArrayList<BoardBean> listBoard() throws Exception{//제네릭 사용함 , 파라미터는 BoardBean
Connection conn = null;//데이터 베이스 접속
Statement stmt = null;// DB에 SQL 전달
ResultSet rs = null;
//db 정보 받기 위함
String sql = "SELECT b_id\r\n" +
" , b_name\r\n" +
" , b_email\r\n" +
" , b_title\r\n" +
" , b_content \r\n" +
" , b_date\r\n" +
" FROM BOARDT \r\n" +
" ORDER BY B_ID";//해당 데이터 쿼리 정렬되게끔 함.
ArrayList<BoardBean> boardList = new ArrayList<BoardBean>();//ArrayList로 게시글들을 받음
try {
conn = getConnection();
//pstmt = conn.prepareStatement(sql);// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
stmt = conn.createStatement();// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
rs = stmt.executeQuery(sql);//쿼리 결과를 rs에 받음(select 조회문은 executeQuery 메소드 처리
while(rs.next()) {//다음(여러개의) 쿼리 데이터가 있는 경우를 조건문으로 줌.
BoardBean board = new BoardBean();
//결과값을 세팅
board.setB_id(rs.getInt(1));//아이디는 INT이므로 getInt로 해야함
board.setB_name(rs.getString(2));
board.setB_email(rs.getString(3));
board.setB_title(rs.getString(4));
board.setB_content(rs.getString(5));
board.setB_date(rs.getTimestamp(6));
// 데이터가 제대로 들어갔는지 확인 해야함
// System.out.println("@@@@#### board.getB_id()=====>"+board.getB_id());
// System.out.println("@@@@#### board.getB_name()=====>"+board.getB_name());
// System.out.println("@@@@#### board.getB_email()=====>"+board.getB_email());
// System.out.println("@@@@#### board.getB_title()=====>"+board.getB_title());
// System.out.println("@@@@#### board.getB_content()=====>"+board.getB_content());
boardList.add(board);//반복하면서 게시글들을 쌓음
}
} catch(SQLException ex) {
ex.printStackTrace();
} finally {
try{//자원반납(순서 중요)
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return boardList;
}
// 5. 리턴타입이 BoardBean인 getBoard() 메소드 추가
public BoardBean getBoard (int bid) throws Exception{//글번호만 넘기면 알아서 내용이 넘어가는 메소드
Connection conn = null;//데이터 베이스 접속
PreparedStatement pstmt = null;// DB에 SQL 전달
ResultSet rs = null;
//db 정보 받기 위함
String sql = "SELECT b_id\r\n" +
" , b_name\r\n" +
" , b_email\r\n" +
" , b_title\r\n" +
" , b_content\r\n" +
" , b_date\r\n" +
" FROM BOARDT\r\n" +
" WHERE b_id=?";//해당 번호의 데이터 출력되게 쿼리 작성
BoardBean board = null;
try {
conn = getConnection();
pstmt = conn.prepareStatement(sql);//pstmt객체로 받음
// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
pstmt.setInt(1, bid);//조건이 하나이기 때문에 (?가 하나임)
rs = pstmt.executeQuery();//쿼리 결과를 rs에 받음(select 조회문은 executeQuery 메소드 처리
if(rs.next()) {//다음(여러개의) 쿼리 데이터가 있는 경우를 조건문으로 줌.
board = new BoardBean();//객체생성(리턴값이 board)
board.setB_id(rs.getInt(1));
//board.setB_id(bid);도 가능함
board.setB_name(rs.getString(2));
board.setB_email(rs.getString(3));
board.setB_title(rs.getString(4));
board.setB_content(rs.getString(5));
board.setB_date(rs.getTimestamp(6));
//
//// 데이터가 제대로 들어갔는지 확인 해야함
// System.out.println("@@@@#### board.setB_id()=====>"+board.getB_id());
// System.out.println("@@@@#### board.setB_name()=====>"+board.getB_name());
// System.out.println("@@@@#### board.setB_email()=====>"+board.getB_email());
// System.out.println("@@@@#### board.setB_title()=====>"+board.getB_title());
// System.out.println("@@@@#### board.setB_content()=====>"+board.getB_content());
}
} catch(SQLException ex) {
ex.printStackTrace();
} finally {
try{//자원반납(순서 중요)
if(rs != null) rs.close();
if(conn != null) conn.close();
if(pstmt != null) pstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
return board;
}
}
<%@page import="java.sql.Timestamp"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<jsp:useBean id="board" class="magic.board.BoardBean"/>
<jsp:setProperty property="*" name="board"/>
<%
BoardDBBean db = BoardDBBean.getInstance();
//instance를 db객체로 받음.
board.setB_date(new Timestamp(System.currentTimeMillis()));
//BoardBean객체에 b_date 프로퍼티 값에 오늘 날짜 저장
int re = db.insertBoard(board);//분기처리
//System.out.println("@@@###getMem_uid ===>"+ re); //getMem_uid를 잘 받는지 확인
if(re == 1) {//분기처리
%>
<script>
alert("글을 등록 하셨습니다.");
</script>
<%
response.sendRedirect("list.jsp"); //이동
}else {
%>
<script>
alert("글 등록에 실패했습니다.");
</script>
<%
response.sendRedirect("write.jsp"); //이동
}
%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<script language="JavaScript" src="board.js" charset="utf-8"></script>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>글올리기</h1>
<form name="reg_frm" method="post" action="write_ok.jsp">
<table align ="center">
<tr height ="30">
<td width="80">작성자 </td>
<td width="140">
<input type="text" name="b_name" size="10" maxlength="20">
</td>
<td width="80">이메일</td>
<td width="240">
<input type="text" name="b_email"size="24" maxlength="50">
</td>
</tr>
<tr height ="30">
<td width ="80">글제목 </td>
<td colspan="3" width="460">
<input type="text"name="b_title" size="55" maxlength="80">
</td>
</tr>
<tr>
<td colspan="4" align ="center">
<textarea cols="65" rows="10" name="b_content"></textarea>
</td>
</tr>
<tr height ="50" align ="center">
<td colspan = "4" >
<input type="button" value="글쓰기" onclick="check_ok()">
<input type="reset" value="다시입력">
<input type="button" value="글목록" onclick="location.href='list.jsp'">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="magic.board.BoardDBBean"%>
<%@page import="magic.board.BoardBean"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
BoardDBBean db = BoardDBBean.getInstance();
//listBoard 메소드를 가지고 와야함(제네릭) 그러기 위해서는 객체생성을 먼저해야함
//instance를 db객체로 받음.(getInstace하면 instance가 리턴됨)
ArrayList<BoardBean> boardList = db.listBoard();
//listBoard메소드 호출하여 ArrayList로 받음(리턴타입은 ArrayList<BoardBean>)
String b_name, b_title, b_email, b_content = "";
int b_id;
Timestamp b_date;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<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 width="800" border="1" cellspacing="0">
<!-- <form name="reg_frm" method="post" action="write_ok.jsp"> -->
<tr height="25">
<td width="40" align="center">번호</td>
<td width="450" align="center">글제목</td>
<td width="120" align="center">작성자</td>
<td width="160" align="center">작성일</td>
</tr>
<%
for(int i=0; i<boardList.size(); i++){//게시글 갯수만큼 반복
BoardBean board = boardList.get(i);
//ArrayList에 담았던 객체들의 값을 가지고 옴
//가지고 올때는 반대방향으로 (listBoard 메소드에서 보낸 값을 반대로 풀면 됨)
//포장했던걸 푸는 느낌
//board객체를 하나씩 풀기
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();
%>
<tr height="25"
onmouseout="this.style.backgroundColor = '#f7f7f7'"
onmouseover="this.style.backgroundColor = '#eeeeef'">
<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 align="center">
<%-- <%= b_date %> --%>
<%= sdf.format(b_date) %>
</td>
</tr>
<%
}
%>
<!-- </form> -->
</table>
</center>
</body>
</html>
테이블
조회수 컬럼 추가(조회수는 default 0)=>B_HIT NUMBER(5)
BoardBean.java
int 데이터형인 b_hit 조회수 getter, setter 추가
BoardDBBean.java
listBoard() 메소드에 조회수 컬럼 추가
getBoard() 메소드에 조회수 업데이트 및 조회 추가
list.jsp
조회수 추가
show.jsp
글번호를 가지고 BoardBean.java 에서 getBoard() 메소드 로 작성자~조회수까지 조회해서 출력
package magic.board;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class BoardDBBean {
//1. 전역 BoardDBBean 객체 레퍼런스를 리턴하는 메소드
private static BoardDBBean instance = new BoardDBBean();
public static BoardDBBean getInstance(){
return instance;
//getConnection이나 getInstance는 계속 사용하기 좋음(재사용하기 좋다는 뜻)
//jsp에서 메소드 호출하면 편하기 때문
}
//2.쿼리 작업에 사용할 커넥션 객체를 리턴하는 메소드
public Connection getConnection( ) throws Exception{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/oracle");
//경로 찾기
return ds.getConnection();
//getConnection이나 getInstance는 계속 사용하기 좋음(재사용하기 좋다는 뜻)
//jsp에서 메소드 호출하면 편하기 때문
}
//3. 전달인자로 받은 BoardBean board를 BOARDT 테이블에 삽입하는 메소드
public int insertBoard(BoardBean board) throws Exception{
int re = -1;
int number;
Connection conn = null;//데이터 베이스 접속
PreparedStatement pstmt = null;// DB에 SQL 전달
ResultSet rs = null;
String sql ="";
try {
conn = getConnection();
sql = "SELECT MAX(B_ID) FROM BOARDT";//글번호 추가
pstmt = conn.prepareStatement(sql);// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
rs = pstmt.executeQuery();//쿼리 결과를 rs에 받음(select 조회문은 executeQuery 메소드 처리
if(rs.next()) {//다음 쿼리 데이터가 있는 경우를 조건문으로 줌.
number = rs.getInt(1)+1;
//글번호가 가장 큰 값을 조회하여 1증가 한 값을 INSERT쿼리에 추가
} else {
number = 1;
}
sql = "INSERT INTO BOARDT VALUES (?,?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
// 데이터가 제대로 들어갔는지 확인 해야함
// System.out.println("@@@@#### number=====>"+number);
// System.out.println("@@@@#### board.getB_name()=====>"+board.getB_name());
// System.out.println("@@@@#### board.getB_email()=====>"+board.getB_email());
// System.out.println("@@@@#### board.getB_title()=====>"+board.getB_title());
// System.out.println("@@@@#### board.getB_content()=====>"+board.getB_content());
// System.out.println("@@@@#### board.getB_date()=====>"+board.getB_date());
pstmt.setInt(1, number);//인덱스 번호 변경 및 글번호 (number) 추가
pstmt.setString(2, board.getB_name());
pstmt.setString(3, board.getB_email());
pstmt.setString(4, board.getB_title());
pstmt.setString(5, board.getB_content());
pstmt.setTimestamp(6, board.getB_date());//날짜 및 시간 저장한걸 추가(setTimestamp)
pstmt.executeUpdate();
re = 1;
} catch(SQLException ex) {
System.out.println("추가 실패");
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 re;
}
//4. 리턴타입이 ArrayList<BoardBean> 인 listBoard() 메소드 추가(글목록을 위한 메소드)
public ArrayList<BoardBean> listBoard() throws Exception{//제네릭 사용함 , 파라미터는 BoardBean
Connection conn = null;//데이터 베이스 접속
Statement stmt = null;// DB에 SQL 전달
ResultSet rs = null;
//db 정보 받기 위함
String sql = "SELECT b_id\r\n" +
" , b_name\r\n" +
" , b_email\r\n" +
" , b_title\r\n" +
" , b_content \r\n" +
" , b_date\r\n" +
" , b_hit\r\n" +
" FROM BOARDT \r\n" +
" ORDER BY B_ID";//해당 데이터 쿼리 정렬되게끔 함.
ArrayList<BoardBean> boardList = new ArrayList<BoardBean>();//ArrayList로 게시글들을 받음
try {
conn = getConnection();
//pstmt = conn.prepareStatement(sql);// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
stmt = conn.createStatement();// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
rs = stmt.executeQuery(sql);//쿼리 결과를 rs에 받음(select 조회문은 executeQuery 메소드 처리
while(rs.next()) {//다음(여러개의) 쿼리 데이터가 있는 경우를 조건문으로 줌.
BoardBean board = new BoardBean();
//결과값을 세팅
board.setB_id(rs.getInt(1));//아이디는 INT이므로 getInt로 해야함
board.setB_name(rs.getString(2));
board.setB_email(rs.getString(3));
board.setB_title(rs.getString(4));
board.setB_content(rs.getString(5));
board.setB_date(rs.getTimestamp(6));
board.setB_hit(rs.getInt(7));//조회수는 INT이므로 getInt로 해야함
// 데이터가 제대로 들어갔는지 확인 해야함
// System.out.println("@@@@#### board.getB_id()=====>"+board.getB_id());
// System.out.println("@@@@#### board.getB_name()=====>"+board.getB_name());
// System.out.println("@@@@#### board.getB_email()=====>"+board.getB_email());
// System.out.println("@@@@#### board.getB_title()=====>"+board.getB_title());
// System.out.println("@@@@#### board.getB_content()=====>"+board.getB_content());
boardList.add(board);//반복하면서 게시글들을 쌓음
}
} catch(SQLException ex) {
ex.printStackTrace();
} finally {
try{//자원반납(순서 중요)
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
return boardList;
}
// 5. 리턴타입이 BoardBean인 getBoard() 메소드 추가
public BoardBean getBoard (int bid) throws Exception{//글번호만 넘기면 알아서 내용이 넘어가는 메소드
Connection conn = null;//데이터 베이스 접속
PreparedStatement pstmt = null;// DB에 SQL 전달
ResultSet rs = null;
//db 정보 받기 위함
BoardBean board = null;
String sql = "";//해당 번호의 데이터 출력되게 쿼리 작성
try {
conn = getConnection();
//조회수 업데이트 추가함
sql = "UPDATE BOARDT SET b_hit=b_hit+1 WHERE b_id=?";
pstmt = conn.prepareStatement(sql);//pstmt객체로 받음
// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
pstmt.setInt(1, bid);//조건이 하나이기 때문에 (?가 하나임)
// rs = pstmt.executeQuery();//쿼리 결과를 rs에 받음(select 조회문은 executeQuery 메소드 처리
pstmt.executeUpdate();//업데이트니깐 업데이트로 바꿔줌
//업데이트 추가 끝!
sql = "SELECT b_id\r\n" +
" , b_name\r\n" +
" , b_email\r\n" +
" , b_title\r\n" +
" , b_content\r\n" +
" , b_date\r\n" +
" , b_hit\r\n" +
" FROM BOARDT\r\n" +
" WHERE b_id=?";//해당 번호의 데이터 출력되게 쿼리 작성
pstmt = conn.prepareStatement(sql);//pstmt객체로 받음
// DB에 SQL 전달(SQL문 사용하기 위한 참조변수 선언
pstmt.setInt(1, bid);//조건이 하나이기 때문에 (?가 하나임)
rs = pstmt.executeQuery();//select 문이니깐 익스큐트 쿼리
if(rs.next()) {//다음(여러개의) 쿼리 데이터가 있는 경우를 조건문으로 줌.
board = new BoardBean();//객체생성(리턴값이 board)
board.setB_id(rs.getInt(1));
//board.setB_id(bid);도 가능함
board.setB_name(rs.getString(2));
board.setB_email(rs.getString(3));
board.setB_title(rs.getString(4));
board.setB_content(rs.getString(5));
board.setB_date(rs.getTimestamp(6));
board.setB_hit(rs.getInt(7));
//
//// 데이터가 제대로 들어갔는지 확인 해야함
// System.out.println("@@@@#### board.setB_id()=====>"+board.getB_id());
// System.out.println("@@@@#### board.setB_name()=====>"+board.getB_name());
// System.out.println("@@@@#### board.setB_email()=====>"+board.getB_email());
// System.out.println("@@@@#### board.setB_title()=====>"+board.getB_title());
// System.out.println("@@@@#### board.setB_content()=====>"+board.getB_content());
}
} catch(SQLException ex) {
ex.printStackTrace();
} finally {
try{//자원반납(순서 중요)
if(rs != null) rs.close();
if(conn != null) conn.close();
if(pstmt != null) pstmt.close();
}catch(Exception e){
e.printStackTrace();
}
}
return board;
}
}
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<script language="JavaScript" src="board.js" charset="utf-8"></script>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>글올리기</h1>
<form name="reg_frm" method="post" action="write_ok.jsp">
<table align ="center">
<tr height ="30">
<td width="80">작성자 </td>
<td width="140">
<input type="text" name="b_name" size="10" maxlength="20">
</td>
<td width="80">이메일</td>
<td width="240">
<input type="text" name="b_email"size="24" maxlength="50">
</td>
</tr>
<tr height ="30">
<td width ="80">글제목 </td>
<td colspan="3" width="460">
<input type="text"name="b_title" size="55" maxlength="80">
</td>
</tr>
<tr>
<td colspan="4" align ="center">
<textarea cols="65" rows="10" name="b_content"></textarea>
</td>
</tr>
<tr height ="50" align ="center">
<td colspan = "4" >
<input type="button" value="글쓰기" onclick="check_ok()">
<input type="reset" value="다시입력">
<input type="button" value="글목록" onclick="location.href='list.jsp'">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="magic.board.BoardBean"%>
<%@page import="magic.board.BoardDBBean"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<%
int bid = Integer.parseInt(request.getParameter("b_id"));
//list.jsp에서 <a href="show.jsp?b_id=..> 에서 b_id가 넘어옴
//그후 캐스팅!!
BoardDBBean db = BoardDBBean.getInstance();
//getBoard 메소드를 가지고 와야함(제네릭) 그러기 위해서는 객체생성을 먼저해야함
//getBoard db객체로 받음.(getInstace하면 instance가 리턴됨)
BoardBean board =db.getBoard(bid);
//getBoard 메소드 호출하여 BoardBean 받음(리턴타입은 board)
Timestamp b_date;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
/* String b_name = "";
String b_title = "";
String b_email = "";
String b_content = ""; */
//오류나서 하나하나 넣어줌
/* if(board != null){
int 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();
} */
%>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>글 내 용 보 기</h1>
<table width="600" border="1" cellspacing="0">
<tr height="30" align="center">
<td width="100">글번호</td>
<td width="200">
<%= bid %>
</td>
<td width="100">조회수</td>
<td width="200">
<%= board.getB_hit() %>
</td>
</tr>
<tr height="30" align="center">
<td width="100">작성자</td>
<td width="200"><%= board.getB_name() %></td>
<%-- board 객체를 이용하여 getB_.. 사용! --%>
<td width="100">작성일</td>
<td width="200"><%= sdf.format(board.getB_date()) %></td>
<%-- board 객체를 이용하여 getB_.. 사용! --%>
</tr>
<tr height="30" align="center">
<td width="100" >글제목</td>
<td width="200" align="left" colspan = "3">
<%= board.getB_title() %>
<%-- board 객체를 이용하여 getB_.. 사용! --%>
</td>
</tr>
<tr height="30" align="center">
<td width="100">글내용</td>
<td width="200" align="left" colspan = "3">
<%= board.getB_content() %>
<%-- board 객체를 이용하여 getB_.. 사용! --%>
</td>
</tr>
</table>
</center>
</body>
</html>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="magic.board.BoardDBBean"%>
<%@page import="magic.board.BoardBean"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
BoardDBBean db = BoardDBBean.getInstance();
//listBoard 메소드를 가지고 와야함(제네릭) 그러기 위해서는 객체생성을 먼저해야함
//instance를 db객체로 받음.(getInstace하면 instance가 리턴됨)
ArrayList<BoardBean> boardList = db.listBoard();
//listBoard메소드 호출하여 ArrayList로 받음(리턴타입은 ArrayList<BoardBean>)
String b_name, b_title, b_email, b_content = "";
int b_id, b_hit;
Timestamp b_date;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<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 width="800" border="1" cellspacing="0">
<!-- <form name="reg_frm" method="post" action="write_ok.jsp"> -->
<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<boardList.size(); i++){//게시글 갯수만큼 반복
BoardBean board = boardList.get(i);
//ArrayList에 담았던 객체들의 값을 가지고 옴
//가지고 올때는 반대방향으로 (listBoard 메소드에서 보낸 값을 반대로 풀면 됨)
//포장했던걸 푸는 느낌
//board객체를 하나씩 풀기
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"
onmouseout="this.style.backgroundColor = '#f7f7f7'"
onmouseover="this.style.backgroundColor = '#eeeeef'">
<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 align="center">
<%-- <%= b_date %> --%>
<%= sdf.format(b_date) %>
</td>
<td align="center"> <%-- 열추가 --%>
<%= b_hit %>
</td>
</tr>
<%
}
%>
<!-- </form> -->
</table>
</center>
</body>
</html>