[TIL] 2020.07.19_TIL-200719

To-Do

  • [복습] JSP

상세페이지 구현 jsp 코드

<%@ page import="kr.ac.kopo.board.*"%>
<%@ page import="kr.ac.kopo.util.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>

<%
	// DB 연결
	String url = "jdbc:oracle:thin:@localhost:1521:XE";
	String user = "system";
	String password = "1234";
	Connection conn = new ConnectionFactory().getConnection(url, user, password);
	StringBuilder sql = new StringBuilder();
	
	// 페이지 번호 가져옴
	String num = request.getParameter("no");
	int boardNum = Integer.parseInt(num);

	// 조회수 카운트
	sql.append(" update my_board set view_count = view_count + 1 ");
  sql.append(" where no = ? ");

	PreparedStatement pstmt = conn.prepareStatement(sql.toString());
  pstmt.setInt(1, boardNum);
  pstmt.executeUpdate();
    
  // 게시글 상세 조회
  sql = new StringBuilder();
	sql.append("select no, title, content, writer, to_char(reg_date, 'yyyy-mm-dd') as reg_date, view_count ");
	sql.append("  from my_board ");
	sql.append(" where no = ? ");
	
	pstmt = conn.prepareStatement(sql.toString());
	pstmt.setInt(1, boardNum);
	
	ResultSet rs = pstmt.executeQuery();
	List<BoardVO> list = new ArrayList<>();
	
	while(rs.next()){
      int no = rs.getInt("no");
      String title = rs.getString("title");
      String content = rs.getString("content");
      String writer = rs.getString("writer");
      String regDate = rs.getString("reg_date");
      int viewCnt = rs.getInt("view_count");
      BoardVO board = new BoardVO(no, title, content, writer, regDate, viewCnt);
      list.add(board);
	}

	JDBCClose.close(conn, pstmt);
	pageContext.setAttribute("boardContents", list);	
%>

태그:

카테고리:

업데이트:

댓글남기기