모델부를 분리해보자..
Java Resources\src 밑에는 자바코드를
WebContent에는 jsp같은 view파일을 작성하자.
BoardDao클래스 만들어
BoardDto클래스 만들어
alt+shift+A : 열모드
단축키 : ctrl+shift+L
Getter Setter 만들기 : alt+shift+S
package board.model; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; public class BoardDao { public List<BoardDto> getList(){ Connection cn = null; PreparedStatement ps = null; ResultSet rs = null; List<BoardDto> list = new ArrayList(); //upcasting 자식클래스의 인스턴스를 //부모클래스의 참조변수로 참조하는것. String sql = "SELECT seq, title, name, writeday, readcount " + "FROM board " + "ORDER BY seq DESC "; try { Class.forName("oracle.jdbc.OracleDriver"); //드라이버 로드.. cn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "oraclejava", "oraclejava"); ps = cn.prepareStatement(sql); rs = ps.executeQuery(); while (rs.next()) { BoardDto boardDto = new BoardDto(); boardDto.setSeq(rs.getInt("seq")); boardDto.setTitle(rs.getString("title")); boardDto.setName(rs.getString("name")); boardDto.setWriteday(rs.getString("writeday")); boardDto.setReadcount(rs.getInt("readcount")); list.add(boardDto); //list에 붙여.. } }catch (Exception e){ e.printStackTrace(); } finally { if (rs!= null) try { rs.close(); } catch(Exception e) {} if (ps!= null) try { ps.close(); } catch(Exception e) {} if (cn!= null) try { cn.close(); } catch(Exception e) {} } return list; } }
package board.model; public class BoardDto { private int seq; private String title ; private String name; private String password; private String content; private int readcount; private String writeday; public int getSeq() { return seq; } public void setSeq(int seq) { this.seq = seq; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public int getReadcount() { return readcount; } public void setReadcount(int readcount) { this.readcount = readcount; } public String getWriteday() { return writeday; } public void setWriteday(String writeday) { this.writeday = writeday; } }
<%@page import="board.model.BoardDao"%> <%@page import="board.model.BoardDto"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>게시판 리스트</title> </head> <% BoardDao boardDao = new BoardDao(); // List list = boardDao.getList(); List<BoardDto> list = boardDao.getList(); %> <body> <table> <caption>게시판 리스트</caption> <tr> <th>번호</th> <th>제목</th> <th>이름</th> <th>날짜</th> <th>조회수</th> </tr> <% /*//예전문법 for(int i = 0; i <list.size(); i++) { BoardDto boardDto = (BoardDto)list.get(i); */ for(BoardDto boardDto:list) { //List에서 하나씩 꺼내서 dto에 집어넣어. //더이상 없을 때까지(java1.5부터) //이렇게 하기위해선 반드시 제네릭<>해줘야해.. %> <tr> <td><%=boardDto.getSeq() %></td> <td><%=boardDto.getTitle() %></td> <td><%=boardDto.getName() %></td> <td><%=boardDto.getWriteday() %></td> <td><%=boardDto.getReadcount() %></td> </tr> <% } %> </table> </body> </html>
아직 스파게티소스이긴 하지..