240102 HTML BoardDAO.java
2024. 1. 3. 20:01ㆍ2023.11.21-2024.05.31
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | package com.sukgi.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.sukgi.db.DBConnection; public class BoardDAO { DBConnection dbCon = new DBConnection(); // 글 저장하기 2024.01.02 public int write(String title, String content) { int result = 0; Connection con = dbCon.getConn(); PreparedStatement pstmt = null; String sql = "INSERT INTO board (board_title, board_content, board_write)VALUES (?, ?, ?)"; String name = "sukgi";// 나중에 세션에서 받아오기 try { pstmt = con.prepareStatement(sql); pstmt.setString(1, title);// 물음표 3개라 1~3까지 pstmt.setString(2, content); pstmt.setString(3, name); result = pstmt.executeUpdate(); // 결과를 숫자로 되돌려줍니다. = 변경된 레코드 수 /* * execute(); 실행, 참거짓 = update, insert, delete executeQuery(); 실행, rs = select * executeUpdate(); 실행, 숫자 = update, insert, delete */ } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { close(null, pstmt, con); } return result; } // 글 삭제하기 public void delete(String no) { Connection conn = dbCon.getConn(); PreparedStatement pstmt = null; String sql = "DELETE FROM board WHERE board_no = ?"; try { pstmt = conn.prepareStatement(sql); pstmt.setString(1, no); pstmt.execute(); // executeQuery():rs가 필요한 녀석 } catch (SQLException e) { e.printStackTrace(); } finally { close(null, pstmt, conn); } } // 톺아보기 public Map<String, Object> detail(String no) { Map<String, Object> detail = new HashMap<String, Object>(); Connection conn = dbCon.getConn(); PreparedStatement pstmt = null; ResultSet rs = null; String sql = "SELECT * FROM board WHERE board_no = ?"; try { pstmt = conn.prepareStatement(sql); // 상기 sql문의 ? 표를 처리 pstmt.setString(1, no);// DB라서 인덱스는 1부터... rs = pstmt.executeQuery(); if (rs.next()) { detail.put("board_no", rs.getInt("board_no")); detail.put("board_title", rs.getString("board_title")); detail.put("board_write", rs.getString("board_write")); detail.put("board_content", rs.getString("board_content")); detail.put("board_date", rs.getString("board_date")); detail.put("board_count", rs.getInt("board_count")); } } catch (SQLException e) { e.printStackTrace(); } return detail; } // 게시판 글 보기 public List<Map<String, Object>> boardList() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Connection conn = dbCon.getConn(); PreparedStatement pstmt = null; ResultSet rs = null; String sql = "SELECT * FROM boardview"; try { pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) {//rs에 값이 없을때까지 계속 돌아 Map<String, Object> e = new HashMap<String, Object>(); e.put("board_no", rs.getInt("board_no")); e.put("board_title", rs.getString("board_title")); e.put("board_write", rs.getString("board_write")); e.put("board_date", rs.getString("board_date")); e.put("board_count", rs.getInt("board_count")); list.add(e);//리스트에 차곡차곡 담아요 } } catch (SQLException e) { e.printStackTrace(); } finally { close(rs, pstmt, conn); return list; } } private void close(ResultSet rs, PreparedStatement pstmt, Connection conn) { try { if (rs != null) { rs.close(); } if (pstmt != null) { pstmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } // // 글 수정하기 public void update(String no, String title, String content) { Connection con = dbCon.getConn(); PreparedStatement pstmt = null; String sql = "UPDATE board SET board_title=?, board_content=? WHERE board_no=?"; try { pstmt = con.prepareStatement(sql); pstmt.setString(1, title); pstmt.setString(2, content); pstmt.setString(3, no); pstmt.execute(); } catch (SQLException e) { e.printStackTrace(); } finally { close(null, pstmt, con); } } } | cs |
'2023.11.21-2024.05.31' 카테고리의 다른 글
240102 HTML Delete.java (1) | 2024.01.04 |
---|---|
240102 HTML DBConnection.java (1) | 2024.01.04 |
240102 HTML write.css (0) | 2024.01.03 |
240102 HTML write.js (0) | 2024.01.03 |
240102 HTML writeAction.jsp (0) | 2024.01.03 |