프로그래밍/Java_JSP 게시판 만들기

자바_JSP 게시판 만들기(1) DAO,DTO 분리

개발계발게발 2021. 6. 24. 17:06
반응형

board.jsp

<%@page import="com.knowhoon.dto.BoardDTO"%>
<%@page import="com.knowhoon.dao.BoardDAO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.Connection"%>
<%@page import="com.knowhoon.web.DBConnection"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>보드</title>
<style type = "text/css">
	table {
	width : 800px;
	height : 400px;
	/* background-color: #F0F8FF; */
	border-collapse: collapse;
}
table td{
border-bottom : 1px #F5DEB3 solid;
text-align: center;
}
table th{
	background-color: #F0F8FF;
}

table tr:hover{
	background-color: #F0F8FF;
}


#title{
	width : 400px;
	text-align: left;
}
a{	
	text-decoration: none;
	color: black;
}
a:visited {	/* read 후 */
	color: gray;	
}
a:link {	/* read 전 */
	
}
a:active { /* 클릭시 */
	
}

</style>
</head>
<body>
	<h1>board</h1>
	<%
	//DAO가 데이터베이스와 작업 후 결과를 돌려주면 그걸 받아서 출력
	// DAO / DTO
	BoardDAO dao = new BoardDAO();
	ArrayList<BoardDTO> list = dao.list();
	%>
	<table>
		<thead>
		<tr>
			<th>번호</th>
			<th>제목</th>
			<th>글쓴이</th>
			<th>날짜</th>
			<th>조회수</th>		
		</tr>		
		</thead>
		
		<tbody>
		<%
		for(int i = 0; i < list.size(); i++){			
		%>
		<tr>
			<td><%=list.get(i).getBno()%></td>
			<td id = "title">
			<a href="detail.jsp?bno=<%=list.get(i).getBno()%>">
			<%=list.get(i).getBtitle()%></td>
			</a>
			<td><%=list.get(i).getName()%></td>
			<td><%=list.get(i).getBdate()%></td>
			<td><%=list.get(i).getBcount()%></td>
		
		</tr>		
		<%
		}
		%>	
		</tbody>
		
<%-- 
		<%
		for(BoardDTO dto: list){			
		%>
		<tr>
			<td><%=dto.getBno()%></td>
			<td><%=dto.getBtitle()%></td>
			<td><%=dto.getName()%></td>
			<td><%=dto.getBdate()%></td>
			<td><%=dto.getBcount()%></td>		
		</tr>	
		
		
		<%
		}
		%>
 --%>
 	</table>
</body>
</html>

 

DBConnection.java

package com.knowhoon.web;

import java.sql.Connection;
import java.sql.DriverManager;

public class DBConnection {
	public Connection dbConn() {
		Connection conn = null;
		try {
			Class.forName("org.mariadb.jdbc.Driver");
			String url = "jdbc:mariadb://220.70.33.29:3306/knowhoon";
			String id = "unknown";
			String pw = "01234";
			conn = DriverManager.getConnection(url, id, pw);
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return conn;
	}
}

 

BoardDAO.java

package com.knowhoon.dao;
//데이터베이스와 접속해서 일하는 DAO
//데이터베이스와 관련된 모든 일을 여기서 처리

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.knowhoon.dto.BoardDTO;
import com.knowhoon.web.DBConnection;

public class BoardDAO {
	public ArrayList<BoardDTO> list() {
		ArrayList<BoardDTO> list = new ArrayList<BoardDTO>();
		DBConnection db = new DBConnection();
		Connection conn = db.dbConn();
		PreparedStatement pstmt = null;
		String query = "SELECT * FROM boardview";	// 뷰출력
		ResultSet rs = null;

		try {
			pstmt = conn.prepareStatement(query);
			rs = pstmt.executeQuery();
			while (rs.next()) {
				BoardDTO dto = new BoardDTO();
				dto.setBno(rs.getInt("bno"));
				dto.setBtitle(rs.getString("btitle"));
				dto.setId(rs.getString("id"));
				dto.setName(rs.getString("name"));
				dto.setBdate(rs.getString("bdate"));
				dto.setBcount(rs.getInt("bcount"));
				list.add(dto);		//리스트 담기
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
}

 

BoardDTO.java

package com.knowhoon.dto;
//데이터 전송 객체
//ArraList<BoardDTO>로 담기

public class BoardDTO {
	private int bno, bcount;
	private String btitle, bdate, name, id;
	
	public int getBno() {
		return bno;
	}
	public void setBno(int bno) {
		this.bno = bno;
	}
	public int getBcount() {
		return bcount;
	}
	public void setBcount(int bcount) {
		this.bcount = bcount;
	}
	public String getBtitle() {
		return btitle;
	}
	public void setBtitle(String btitle) {
		this.btitle = btitle;
	}
	public String getBdate() {
		return bdate;
	}
	public void setBdate(String bdate) {
		this.bdate = bdate;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}	
}

 

반응형