본문 바로가기
프로젝트/Summernote를 활용한 WYSIWYG 게시판 만들기

7. 상세보기

by IT여행자 2023. 1. 1.
728x90

목록에서 한건의 serial 번호를 받아와 해당 자료를 표시합니다.

 

package bean;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SnDao {
	String uploadPath = "N:\\workspace\\summernote\\src\\main\\webapp\\";
	Connection conn;
	PreparedStatement ps;
	ResultSet rs;
	String sql;
	public SnDao() {
		try {
			conn = new DBConn().getConn();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public boolean insert(SnVo vo) {
		boolean b = false;
		sql = "insert into summernote(subject, doc) values(?,?)";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setString(1, vo.getSubject());
			ps.setString(2, vo.getDoc());

			int c = ps.executeUpdate();
			if (c > 0) {
				conn.commit();
				b = true;
			} else {
				conn.rollback();
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return b;

	}

	public boolean modify(SnVo vo) {
		boolean b = false;
		sql = "update summernote set subject=? , doc=? where serial = ? ";
		try {
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setString(1, vo.getSubject());
			ps.setString(2, vo.getDoc());
			ps.setInt(3, vo.getSerial());

			int c = ps.executeUpdate();
			if (c > 0) {
				conn.commit();
				b = true;
			} else {
				conn.rollback();
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return b;

	}

	public boolean delete(String serial) {
		boolean b = false;

		// img 태그안에 있는 파일명을 찾기 위한 패턴
		String regex = "(<img src=\")(.+?)(\")";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = null;
		try {

			// 삭제해야할 파일명을 찾기 위해
			SnVo vo = view(serial);

			// doc 안에 있는 img 태그 정보를 가져와 파일 삭제
			matcher = pattern.matcher(vo.getDoc());

			sql = "delete from summernote where serial = " + serial;
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);

			int c = ps.executeUpdate();
			if (c > 0) {
				conn.commit();
				b = true;

				// upload 경로에 있는 파일 삭제
				while (matcher.find()) {
					File delFile = new File(uploadPath + matcher.group(2));
					if (delFile.exists())
						delFile.delete();

				}

			} else {
				conn.rollback();
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return b;

	}

	public List<SnVo> select(String findStr) {
		List<SnVo> list = new ArrayList<SnVo>();
		sql = "select * from summernote where subject like ? order by serial desc";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, "%" + findStr + "%");
			rs = ps.executeQuery();

			while (rs.next()) {
				SnVo vo = new SnVo();
				vo.setSerial(rs.getInt("serial"));
				vo.setSubject(rs.getString("subject"));

				list.add(vo);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return list;

	}

	public SnVo view(String serial) {
		SnVo vo = new SnVo();
		sql = "select * from summernote where serial = ? ";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, serial);
			rs = ps.executeQuery();

			while (rs.next()) {
				vo.setSerial(rs.getInt("serial"));
				vo.setSubject(rs.getString("subject"));
				vo.setDoc(rs.getString("doc"));
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return vo;

	}

}

 

1) 상세 보기 페이지

 

<%@page import="bean.SnVo"%>
<%@page import="bean.SnDao"%>
<%@page import="bean.DBConn"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@ 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>
<link rel='stylesheet' type='text/css' href='./css/sn.css'>


</head>
<body>

<%

request.setCharacterEncoding("utf-8");
SnDao dao = new SnDao();
String serial = request.getParameter("serial");
SnVo vo = dao.view(serial);

%>
<div id='list'>
	<h1>상세보기</h1>
	<div class='item'>
		<div class='serial'>serial : <%=vo.getSerial()%></div>
		<div class='subject'><%=vo.getSubject() %></div>
		<div class='doc'><%=vo.getDoc() %></div>
		</a>
	</div>
	<hr/>
	<a href='modify.jsp?serial=<%=vo.getSerial()%>'>수정하기</a>
	<a href='delete.jsp?serial=<%=vo.getSerial()%>'>삭제하기</a>
	<a href='list.jsp'>목록으로</a>
</div> 

</body>
</html>

 

 

 

 

 

'프로젝트 > Summernote를 활용한 WYSIWYG 게시판 만들기' 카테고리의 다른 글

2. 기본 구조  (0) 2023.01.01
3. 에디터 화면 및 저장 만들기  (0) 2023.01.01
4. 수정하기  (0) 2023.01.01
5. 삭제하기  (0) 2023.01.01
6. 조회하기  (0) 2023.01.01