wasup

게시판 만들기#ver1_list and view 본문

IT/Java

게시판 만들기#ver1_list and view

wasupup 2021. 5. 28. 17:15
반응형

목록출력

게시판에 등록한 제목과 내용 출력

board_list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="boardpackage.boardtable"%>
<%@ page import="control.ConnectionInfo" %>

<%

//1. 업무규칙검사

//2. 데이터 수집
ArrayList<boardtable> boardPosts = new ArrayList<boardtable>();

try {
	// 0. 드라이버로드
	Class.forName("com.mysql.cj.jdbc.Driver");
	
	// 1. 드라이버 관리자를 통해 DB연결자를 얻음
	Connection DBConManagement = DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=utf8", ConnectionInfo.DBIP, ConnectionInfo.DBPort, ConnectionInfo.DBName), ConnectionInfo.DBID, ConnectionInfo.DBPASSWORD);
	
	// 2. 연결자를 통해 명령 전달자를 얻음
	Statement CommandState = DBConManagement.createStatement();
	
	// 3. 명령
	// 3_1 총(전체가 아닌 수집한 것)게시물수 구하기
	String totalCountSQL = "select count(*) as 갯수 from boardtable";
	ResultSet setCTManagement = CommandState.executeQuery(totalCountSQL);
	setCTManagement.next();
	totalCount = setCTManagement.getInt("갯수");
	
	setCTManagement.close();
	
	// 3_2 게시물수집
	String CollecSQL = String.format("select boNum, boTit, boDate, boHit from boardtable ORDER BY boDate desc limit %d, %d", startNum, postsPerPage);
	ResultSet CollectManager = CommandState.executeQuery(CollecSQL);
	
	while(CollectManager.next()) {//next() 다음줄로
		int boNum = CollectManager.getInt("boNum");
		String boTit = CollectManager.getString("boTit");
		java.sql.Date boDate = CollectManager.getDate("boDate");
		int boHit = CollectManager.getInt("boHit");
		
		boardtable boardPost = new boardtable();
		boardPost.setBoNum(boNum);
		boardPost.setBoTit(boTit);
		boardPost.setBoDate(boDate);
		boardPost.setBoHit(boHit);
		
		boardPosts.add(boardPost);
	}
	CollectManager.close();
	
	// 4. 연결끊기
	DBConManagement.close();
} catch (Exception e) {
	e.printStackTrace();
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>board list</title>
</head>
<body>

<h3>게시물 목록</h3>
<!-- 3_1. 목록출력 -->
<ul class="list_box">
	<%//3. 목록출력
	for(int num = 0; num < boardPosts.size(); num++){
		//3-1. 게시물 1개 출력
		boardtable boardPost = boardPosts.get(num);
	%>
		<li class="list_block">
			<a href="./board_view.jsp?no=<%= boardPost.getBoNum() %>"><span><%= num+1 %></span><%= boardPost.getBoTit() %></a>
			<p>
			<%= boardPost.getBoDate().toString() %>
			<%= boardPost.getBoHit() %>
			</p>
		</li>
	<%	}	%>
</ul>

</body>
</html>

목록에서 상세보기로 이동

board_view.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="boardpackage.boardtable"%>
<%@ page import="control.ConnectionInfo" %>
<%
//입력
String strBoardNo = request.getParameter("no");
int boardNo  = Integer.valueOf(strBoardNo);

//1. 업무규칙검사

//2. 데이터 수집
boardtable boardPost = null;
try {
	// 0. 드라이버로드
	Class.forName("com.mysql.cj.jdbc.Driver");
		
	// 1. 드라이버 관리자를 통해 DB연결자를 얻음
	Connection DBConManagement = DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=utf8", ConnectionInfo.DBIP, ConnectionInfo.DBPort, ConnectionInfo.DBName), ConnectionInfo.DBID, ConnectionInfo.DBPASSWORD);
		
	// 2. 연결자를 통해 명령 전달자를 얻음
	Statement CommandState = DBConManagement.createStatement();
		
	// 3. 명령
	String IncreaseHitSQL = String.format("UPDATE boardtable SET boHit = boHit+1 WHERE boNum=%d", boardNo);
	CommandState.executeUpdate(IncreaseHitSQL);
	
	String CollecSQL = String.format("SELECT * FROM boardtable WHERE boNum=%d", boardNo);
	ResultSet CollectManager = CommandState.executeQuery(CollecSQL);

	if(CollectManager.next()) {//next() 다음줄로
		int boNum = CollectManager.getInt("boNum");
		String boTit = CollectManager.getString("boTit");
		String boCon = CollectManager.getString("boCon");
		java.sql.Date boDate = CollectManager.getDate("boDate");
		int boHit = CollectManager.getInt("boHit");
		
		boardPost = new boardtable();
		boardPost.setBoNum(boNum);
		boardPost.setBoTit(boTit);
		boardPost.setBoCon(boCon);
		boardPost.setBoDate(boDate);
		boardPost.setBoHit(boHit);
	}
	CollectManager.close();
	
	// 4. 연결끊기
DBConManagement.close();
} catch (Exception e) {
	e.printStackTrace();
}

%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>상세보기</title>
</head>
<body>
<%@ include file="./nav.jsp" %>
<h3>상세보기</h3>
<!-- 3. 게시물 출력 -->
<div>
	<div>
		<string><%= boardPost.getBoTit() %></string>
		<p>
		date <%= boardPost.getBoDate().toString() %>
		hit <%= boardPost.getBoHit() %>
		</p>
	</div>
	<div>
		<%= boardPost.getBoCon() %>
	</div>
</div>


</body>
</html>
반응형

'IT > Java' 카테고리의 다른 글

게시판 만들기#ver1_paging  (0) 2021.05.30
게시판 만들기#ver1_basedata  (0) 2021.05.29
게시판 만들기#ver1_write  (0) 2021.05.27
Java) Scanner test  (0) 2021.05.15
Java) Array 배열이 뭐냐  (0) 2021.05.13
Comments