[Day4] JSP/JDBC 4 [10/31]

 

์ฃผ์˜) PL/SQL ์€ ์ €์žฅํ”„๋กœ์‹œ์ €์—์„œ ์˜ˆ์‹œ๋กœ ๋ดค์„ ๋•Œ ํ…Œ์ด๋ธ”๋ช…๊ณผ ์นผ๋Ÿผ๋ช…์€ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•จ

 

๊ฒŒ์‹œํŒ ์ƒ์„ฑ

1. ๊ฒŒ์‹œํŒ(board) ํ…Œ์ด๋ธ” ์ƒ์„ฑ

 http://taeyo.net/

C#  .net (๋‹ท๋„ท)

seq : ์ผ๋ จ๋ฒˆํ˜ธ, ๊ธฐ๋ณธํ‚ค. ๊ฐ ๊ธ€์„ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฒˆํ˜ธ์ž…๋‹ˆ๋‹ค.
writer : ๊ธ€์“ด์ด
pwd : ๋น„๋ฐ€๋ฒˆํ˜ธ
email : ๋ฉ”์ผ์ฃผ์†Œ
title : ์ œ๋ชฉ
writedate : ๊ธ€์“ด ์ผ์‹œ
readed : ์กฐํšŒ์ˆ˜
mode : ๊ธ€์˜ ํ˜•์‹( 0-TEXT, 1-HTML ํƒœ๊ทธ ํ—ˆ์šฉ )
content : ๊ธ€์˜ ๋‚ด์šฉ
CREATE SEQUENCE seq_tbl_cstVSBoard;
CREATE TABLE tbl_cstVSBoard(
	seq       NUMBER NOT NULL PRIMARY KEY           
	, writer VARCHAR2(20) NOT NULL 
	, pwd    VARCHAR2(20) NOT NULL 
	, email  VARCHAR2(100) 
	, title    VARCHAR2(200) NOT NULL 
	, writedate DATE DEFAULT SYSDATE
	, readed NUMBER DEFAULT 0
	, tag   NUMBER(1)  DEFAULT 0 -- 0 ํ…์ŠคํŠธ๋ชจ๋“œ  1 HTML๋ชจ๋“œ 
	, content CLOB 
);

2. ๊ธ€์“ฐ๊ธฐ

๊ธ€๋ชฉ๋ก ํŽ˜์ด์ง€                                  
[๊ธ€์“ฐ๊ธฐ]๋ฒ„ํŠผ         ->  ํด๋ฆญ ->    ๊ธ€์“ฐ๊ธฐ(์ƒˆ๊ธ€์ž‘์„ฑ) ํŽ˜์ด์ง€
                                                   ์ œ๋ชฉ/๋‚ด์šฉ/๋“ฑ๋“ฑ ์ž…๋ ฅ ์ž‘์—…
                                                   [์ €์žฅ][๋ชฉ๋ก][์ทจ์†Œ] ๋“ฑ๋“ฑ
                                                    ใ„ด์ €์žฅ ํด๋ฆญ

 

3. domain.BoardDTO  ์„ ์–ธ     

4. [์ˆœ์„œ]

Controller -> Handler -> Service -> DAO -> DB (Oracle)

์ฒ˜๋ฆฌ ์™„๋ฃŒ๋˜๋ฉด ๋ฐ˜๋Œ€๋กœ ๊ฒฐ๊ณผ๋ฌผ ๋ฐ˜ํ™˜

 

Controller - Service - Handler(Model) -> DAO -> ์˜ค๋ผํด,   

 

                                                                           insert(dto)

                    dto           dto                        dto    

 

*  ์—ฌ๊ธฐ์„  (Controller + Model + View) ํ•˜๋‚˜๋กœ ์ฒ˜๋ฆฌ


[๊ธฐ๋ณธ์„ค์ •]

* main

Connection conn = DBConn.getcConnection();
BoardDAO dao = new BoardDAOImpl(conn);
BoardService service = new BoradService(dao);
BoardController controller = new BoardController(service);
controller.boardStart();

* DTO

package days03.board;

import java.sql.Date;

public class BoardDTO {

	private int seq;           
	private String  writer; 
	private String  pwd;
	private String  email;
	private String  title;
	private Date  writedate;
	private int  readed;
	private int tag;
	private String   content;

	public BoardDTO() {
		super(); 
	}

	public BoardDTO(int seq, String writer, String pwd, String email, String title, Date writedate, int readed, int tag,
			String content) {
		super();
		this.seq = seq;
		this.writer = writer;
		this.pwd = pwd;
		this.email = email;
		this.title = title;
		this.writedate = writedate;
		this.readed = readed;
		this.tag = tag;
		this.content = content;
	}

	public int getSeq() {
		return seq;
	}

	public void setSeq(int seq) {
		this.seq = seq;
	}

	public String getWriter() {
		return writer;
	}

	public void setWriter(String writer) {
		this.writer = writer;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Date getWritedate() {
		return writedate;
	}

	public void setWritedate(Date writedate) {
		this.writedate = writedate;
	}

	public int getReaded() {
		return readed;
	}

	public void setReaded(int readed) {
		this.readed = readed;
	}

	public int getTag() {
		return tag;
	}

	public void setTag(int tag) {
		this.tag = tag;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Override
	public String toString() {
		return "BoardDTO [seq=" + seq + ", writer=" + writer + ", pwd=" + pwd + ", email=" + email + ", title=" + title
				+ ", writedate=" + writedate + ", readed=" + readed + ", tag=" + tag + ", content=" + content + "]";
	} 
} // class

 

* DAO interface

package days03.board;

import java.sql.SQLException;
import java.util.ArrayList;

public interface BoardDAO {
	
	// 1. ๊ธ€ ์“ฐ๊ธฐ
	int insert( BoardDTO dto ) throws SQLException; 
	
	// 2. ๊ธ€ ๋ชฉ๋ก
	ArrayList<BoardDTO> select( int currentPage, int numberPerPage  ) throws SQLException; 

	// 3. ๊ธ€ ์ƒ์„ธ๋ณด๊ธฐ
	// 3-1. ์กฐํšŒ์ˆ˜ ์ฆ๊ฐ€
	int increaseReaded(int seq)  throws SQLException; 
	// 3-2. ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€ ๋ฐ˜ํ™˜
	BoardDTO view(int seq) throws SQLException;

	// 4. ๊ธ€ ์‚ญ์ œ
	// 4-1. ๋น„๋ฐ€๋ฒˆํ˜ธ ํ™•์ธ
	String getOriginalPwd(int seq) throws SQLException;
	// 4-2. ๊ธ€ ์‚ญ์ œ.
	int delete(int seq) throws SQLException;

	// 5. ์ˆ˜์ •
	int update(BoardDTO dto) throws SQLException;

	// 6. ๊ฒ€์ƒ‰
	ArrayList<BoardDTO> search( int currentPage, int numberPerPage, int searchCondition, String searchWord) throws SQLException;

	// ์ด๋ ˆ์ฝ”๋“œ์ˆ˜ 
	int getTotalRecords() throws SQLException;
	// ์ดํŽ˜์ด์ง€์ˆ˜
	int getTotalPages(int numberPerPage) throws SQLException;

	//  ๊ฒ€์ƒ‰ํ•˜๊ธฐ - ์ด๋ ˆ์ฝ”๋“œ์ˆ˜
	int getTotalRecords(int searchCondition, String searchWord) throws SQLException;
     // ๊ฒ€์ƒ‰ํ•˜๊ธฐ - ์ดํŽ˜์ด์ง€์ˆ˜
	int getTotalPages(int numberPerPage, int searchCondition, String searchWord) throws SQLException;
	
}  // interface

* DAOImpl

public class BoardDAOImpl implements BoardDAO{

	private Connection conn = null;
	private PreparedStatement pstmt  = null;
	private ResultSet rs = null;

	public BoardDAOImpl() {}
	// 1. ์ƒ์„ฑ์ž DI( ์˜์กด์„ฑ ์ฃผ์ž… )
	public BoardDAOImpl(Connection conn) {
		this.conn = conn;
	}
	// 2. setter DI
	public void setConn(Connection conn) {
		this.conn = conn;
	}
}

* Service

public class BoardService {

	private BoardDAO dao = null;

	// 1. ์ƒ์„ฑ์ž DI
	public BoardService(BoardDAO dao) {
		super();
		this.dao = dao;
	}

	// 2. setter DI
	public void setDao(BoardDAO dao) {
		this.dao = dao;
	}
}

* Controller

public class BoardController {
	
	// ์š”์ฒญ(C) + ๋กœ์ง์ฒ˜๋ฆฌ(M) + ์ถœ๋ ฅ(V)	
	private BoardService service = null;
	private int selectedNumber = 1;
	private Scanner scanner = null;
	
	public BoardController() {
		super();
		this.scanner = new Scanner(System.in);
	}

	// 1. ์ƒ์„ฑ์ž DI(์˜์กด์„ฑ ์ฃผ์ž…)
	public BoardController(BoardService service) {
		this();
		this.service = service;
	}
}
public void boardStart() {
		
		while(true) {
			๊ฒŒ์‹œํŒ๋ฉ”๋‰ด์ถœ๋ ฅ(); // 
			๊ฒŒ์‹œํŒ๋ฉ”๋‰ด์„ ํƒ(); // ์š”์ฒญ(C) +
			๊ฒŒ์‹œํŒ๋ฉ”๋‰ด์ฒ˜๋ฆฌ();  // Model + View X
		} // while
		
	}

private void ๊ฒŒ์‹œํŒ๋ฉ”๋‰ด์ถœ๋ ฅ() {
		 String [] menus = {"์ƒˆ๊ธ€", "๋ชฉ๋ก", "๋ณด๊ธฐ", "์ˆ˜์ •", "์‚ญ์ œ", "๊ฒ€์ƒ‰", "์ข…๋ฃŒ" };
		 System.out.print("[ ๋ฉ”๋‰ด ]  ");
		 for (int i = 0; i < menus.length; i++) {
			System.out.printf("%d. %s\t", i+1, menus[i]);			
		} // for
		 System.out.println();
	}

private void ๊ฒŒ์‹œํŒ๋ฉ”๋‰ด์„ ํƒ() {
		System.out.print("> ๋ฉ”๋‰ด ์„ ํƒํ•˜์„ธ์š” ? ");
		selectedNumber = scanner.nextInt();  // 1  ์—”ํ„ฐ           13,10
		scanner.nextLine();  // 13,10 ๋ฒ„๋ฆผ.
	}

private void ๊ฒŒ์‹œํŒ๋ฉ”๋‰ด์ฒ˜๋ฆฌ() {
		 switch (  selectedNumber  ) {
			case 1:   // "์ƒˆ๊ธ€"
				์ƒˆ๊ธ€์“ฐ๊ธฐ();
				break;
			case 2:   // , "๋ชฉ๋ก"
				๋ชฉ๋ก๋ณด๊ธฐ();
				break;
			case 3:   // , "๋ณด๊ธฐ",
				์ƒ์„ธ๋ณด๊ธฐ();
				break;			
			case 4:   //  "์ˆ˜์ •"
				์ˆ˜์ •ํ•˜๊ธฐ();
				break;			
			case 5:  // , "์‚ญ์ œ
				์‚ญ์ œํ•˜๊ธฐ();
				break;	
			case 6:   // ", "๊ฒ€์ƒ‰"
				break;		
			case 7:  //  , "์ข…๋ฃŒ"
				exit();
				break;	
			default:
				System.out.println("\t\t[๊ฒฝ๊ณ ] ๋ฉ”๋‰ด ์„ ํƒ ์ž˜๋ชปํ–ˆ๋‹ค( 1~6)!!!");
				break;
			} // switch	
	}

private static void exit() {
		System.out.println("\t\t ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ !!!");
		DBConn.close();
		System.exit(-1);
}

[1. ๊ธ€์“ฐ๊ธฐ]

๊ธ€์“ฐ๊ธฐ ์š”์ฒญ

-> 1) DB insert

-> 2) ๋กœ๊ทธ ๊ธฐ๋ก

-> 3) ์ž‘์„ฑ์ž์—๊ฒŒ ํฌ์ธํŠธ 1์ฆ๊ฐ€ update

 

1), 2), 3) ๋ชจ๋‘ ์„ฑ๊ณตํ•ด์•ผ ์„ฑ๊ณตํ•˜๋Š” ์ฒ˜๋ฆฌ = ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ (transaction)

 

1) DAOImpl

@Override
public int insert(BoardDTO dto) throws SQLException {
	String sql = "INSERT INTO TBL_CSTVSBOARD "
		+ " ( seq, writer, pwd, email, title, tag, content )"
		+ " VALUES (  SEQ_TBL_CSTVSBOARD.NEXTVAL , ? , ? , ? , ? , ? , ? ) ";

	this.pstmt = this.conn.prepareStatement(sql);
	// ? , ? , ? , ? , ? , ?
	this.pstmt.setString(1,  dto.getWriter());
	this.pstmt.setString(2,  dto.getPwd());
	this.pstmt.setString(3,  dto.getEmail());
	this.pstmt.setString(4,  dto.getTitle());
	this.pstmt.setInt(5,  dto.getTag());
	this.pstmt.setString(6,  dto.getContent());

	int rowCount =  this.pstmt.executeUpdate();
	
	this.pstmt.close();

	return rowCount;
}

2) DAOImpl test

@Test
void insert_test() {
	//fail("Not yet implemented");

	Connection conn = DBConn.getConnection();
	BoardDAOImpl dao = new BoardDAOImpl(conn);

	BoardDTO dto = new BoardDTO();
	// writer, pwd, email, title, tag, content
	dto.setWriter("ํ™๊ธธ๋™");
	dto.setPwd("1234");
	dto.setEmail("hong@naver.com");
	dto.setTitle("์ฒซ ๋ฒˆ์งธ ๊ฒŒ์‹œ๊ธ€ - ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ");
	dto.setTag(0);  // ํ…์ŠคํŠธ ๋ชจ๋“œ
	dto.setContent("์ฒซ ๋ฒˆ์งธ ๊ฒŒ์‹œ๊ธ€ ๋‚ด์šฉ");

	try {
		int rowCount = dao.insert(dto);
		if( rowCount == 1 ) {
			System.out.println(" ๊ธ€์“ฐ๊ธฐ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์„ฑ๊ณต!!!");
		}else {
			System.out.println(" ๊ธ€์“ฐ๊ธฐ ์‹คํŒจ!!!");
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}		 
	DBConn.close();
}

3) Service

public int insertService( BoardDTO dto) {
	int rowCount = 0;		

	try {
		// ํŠธ๋žœ์žญ์„  ์ฒ˜๋ฆฌ ์‹œ์ž‘			T1
		// 1. ๋กœ๊ทธ ๊ธฐ๋ก ์„œ๋น„์Šค
		System.out.println("> ๊ฒŒ์‹œ๊ธ€ ์“ฐ๊ธฐ(์ถ”๊ฐ€) -> ๋กœ๊ทธ ๊ธฐ๋ก ์ž‘์—…...");
		// 2. ๊ฒŒ์‹œ๊ธ€ ์ถ”๊ฐ€
		rowCount = this.dao.insert(dto);
		// 3. ์ž‘์„ฑ์ž์˜ ํฌ์ธํŠธ 1์ฆ๊ฐ€ - update
		//    ํšŒ์›ํ…Œ์ด๋ธ” X - point ์ปฌ๋Ÿผ  X
		// this.dao.updateMemberPoint(dto.getWriter());
		System.out.println("> ๊ฒŒ์‹œ๊ธ€ ์“ฐ๊ธฐ(์ถ”๊ฐ€)  ์ž‘์„ฑ์ž ํฌ์ธํŠธ ์ฆ๊ฐ€ this. dao.update~() ..");
        
        // ์ปค๋ฐ‹			 T2
	} catch (SQLException e) {
		// ๋กค๋ฐฑ T3
		e.printStackTrace();
	}
	return rowCount;
}

4) Service ํ…Œ์ŠคํŠธ

@Test
void inserService_test() {
		
	Connection conn = DBConn.getConnection();
	BoardDAOImpl dao = new BoardDAOImpl(conn);
	BoardService service = new BoardService(dao);
		
	BoardDTO dto = new BoardDTO();
	// writer, pwd, email, title, tag, content
	dto.setWriter("ํ™๊ธธ๋™");
	dto.setPwd("1234");
	dto.setEmail("hong@naver.com");
	dto.setTitle("๋‘ ๋ฒˆ์งธ ๊ฒŒ์‹œ๊ธ€ - ์„œ๋น„์Šค ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ");
	dto.setTag(0);  // ํ…์ŠคํŠธ ๋ชจ๋“œ
	dto.setContent("๋‘ ๋ฒˆ์งธ ๊ฒŒ์‹œ๊ธ€ ๋‚ด์šฉ");
		
	int rowCount =  service.insertService(dto);
	if( rowCount == 1 ) {
		System.out.println(" ๊ธ€์“ฐ๊ธฐ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์„ฑ๊ณต!!!");
	}else {
		System.out.println(" ๊ธ€์“ฐ๊ธฐ ์‹คํŒจ!!!");
	}	
	DBConn.close();
}

5) Controller

private void ์ƒˆ๊ธ€์“ฐ๊ธฐ() {
	// ์ƒˆ ๊ฒŒ์‹œ๊ธ€ ์ •๋ณด๋ฅผ ์ฝ˜์†”์—์„œ ์ž…๋ ฅ
	System.out.print("> writer, pwd, email, title, tag, content ์ž…๋ ฅ ? ");
	//  "๊ถŒ์žฌํ˜„,1234,kkk@naver.com,์„ธ  ๋ฒˆ์งธ  ๊ฒŒ์‹œ๊ธ€,0,์„ธ  ๋ฒˆ์งธ  ๊ฒŒ์‹œ๊ธ€ ๋‚ด์šฉ"
	String [] datas = scanner.nextLine().split(",");
		
	String writer =  datas[0];
	String pwd = datas[1];
	String email = datas[2];
	String title = datas[3];
	int tag = Integer.parseInt(datas[4]);
	String content = datas[5];
		
	// [C]dto -> [S]dto-> [D]dto-> DB INSERT
	BoardDTO dto = new BoardDTO();
	dto.setWriter(writer);
	dto.setPwd(pwd);
	dto.setEmail(email);
	dto.setTitle(title);
	dto.setTag(tag);
	dto.setContent(content);
	
	int rowCount =  this.service.insertService(dto);
	
	if( rowCount == 1) {
		System.out.println("> ์ƒˆ ๊ฒŒ์‹œ๊ธ€ ์“ฐ๊ธฐ ์™„๋ฃŒ!!!");
		// ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก ํŽ˜์ด์ง€ ์ด๋™
	}		
	์ผ์‹œ์ •์ง€();
}
private tatic void ์ผ์‹œ์ •์ง€() {
	System.out.println("\t\t ์—”ํ„ฐ์น˜๋ฉด ๊ณ„์†ํ•ฉ๋‹ˆ๋‹ค.");
	try {
		System.in.read();
 		System.in.skip( System.in.available() ); // 13, 10
	} catch (IOException e) { 
		e.printStackTrace();
	} // try
}

[2. ๋ชฉ๋ก๋ณด๊ธฐ]

1) DAO Impl

@Override
public ArrayList<BoardDTO> select(int currentPage, int numberPerPage) throws SQLException {

	ArrayList<BoardDTO> list = null;
	BoardDTO dto = null; 

	int begin = (currentPage-1)*numberPerPage + 1;
	int end = begin + numberPerPage -1;

	String sql = 
			" SELECT b.* "
				+ " FROM (  "
				+ "        SELECT ROWNUM no, t.* "
				+ "        FROM (  "
				+ "                SELECT seq, writer, email, title, readed, writedate "
				+ "                FROM tbl_cstvsboard "
				+ "                ORDER BY seq DESC "
				+ "              ) t "
				+ "     ) b "
				+ " WHERE b.no BETWEEN ? AND ?";

	this.pstmt = this.conn.prepareStatement(sql);

	// BETWEEN ? AND ?"
	this.pstmt.setInt(1, begin);
	this.pstmt.setInt(2, end);

	this.rs =  this.pstmt.executeQuery();

	//		int no, seq, readed;
	//		String  writer, email, title;
	//		Date writedate;

	if ( this.rs.next()) {
		list = new ArrayList<BoardDTO>();

		do {
			dto = new BoardDTO();
			dto.setSeq(this.rs.getInt("seq"));
			dto.setReaded(this.rs.getInt("readed"));
			dto.setWriter( this.rs.getString("writer"));
			dto.setEmail( this.rs.getString("email"));
			dto.setTitle( this.rs.getString("title"));
			dto.setWritedate(this.rs.getDate("writedate"));
			list.add(dto);
		} while ( this.rs.next() );
	} // if

	this.rs.close(); // 
	this.pstmt.close();

	return  list;  // ๊ฒŒ์‹œ๊ธ€์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด  list = null ..
}

2) DAO Impl ํ…Œ์ŠคํŠธ

@Test
void select_test() {
	//fail("Not yet implemented");

	Connection conn = DBConn.getConnection();
	BoardDAOImpl dao = new BoardDAOImpl(conn);

	ArrayList<BoardDTO> list = null;

	try {
		list = dao.select(1, 15);  // 1๋ฒˆํŽ˜์ด์ง€,   15๊ฐœ ์ถœ๋ ฅ			
		// ์ถœ๋ ฅ
		Iterator<BoardDTO> ir = list.iterator();			
		while (ir.hasNext()) {
			BoardDTO dto =   ir.next();
			System.out.println( dto );
		}

	} catch (SQLException e) {
		e.printStackTrace();
	}		 
	DBConn.close();
}

3) Service

// 2. ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก ์„œ๋น„์Šค ๋ฉ”์„œ๋“œ
public ArrayList<BoardDTO> selectService( int currentPage, int numberPerPage ){
	ArrayList<BoardDTO> list = null;

	try {
		// 1. ๋กœ๊ทธ ๊ธฐ๋ก ์„œ๋น„์Šค
		System.out.println("> ๊ฒŒ์‹œ๊ธ€ ๋ชฉ๋ก -> ๋กœ๊ทธ ๊ธฐ๋ก ์ž‘์—…...");
		// 2.
		list =  this.dao.select(currentPage, numberPerPage);
	} catch (SQLException e) {
		e.printStackTrace();
	}
	return list;
}

4) Service ํ…Œ์ŠคํŠธ

5) Controller

private void ๋ชฉ๋ก๋ณด๊ธฐ() {
	// 357 INSERT
	// BoardService.selectService()/  BoardDAO.select()
	System.out.print("> ํ˜„์žฌ ํŽ˜์ด์ง€( currentPage) ์ž…๋ ฅ ? " );  
	this.currentPage = this.scanner.nextInt();
	this.scanner.nextLine(); // 13,10

	ArrayList<BoardDTO> list = this.service.selectService(this.currentPage, this.numberPerPage);

	// M[V]C  - ์ถœ๋ ฅ ๋‹ด๋‹น ๋ทฐ.
	System.out.println("\t\t\t  ๊ฒŒ์‹œํŒ");
	System.out.println("--------------------------------------------------------------");
	System.out.printf("%s\t%-40s\t%s\t%-10s\t%s\n", 
			"๊ธ€๋ฒˆํ˜ธ","๊ธ€์ œ๋ชฉ","๊ธ€์“ด์ด","์ž‘์„ฑ์ผ","์กฐํšŒ์ˆ˜");
	System.out.println("--------------------------------------------------------------");

	if (  list == null) {
		System.out.println("\t\t ๊ฒŒ์‹œ๊ธ€ ์กด์žฌ X ");
	} else {
		Iterator<BoardDTO> ir =list.iterator();
		while (ir.hasNext()) {
			BoardDTO dto =   ir.next();
			System.out.printf("%d\t%-30s  %s\t%-10s\t%d\n",
					dto.getSeq()
					, dto.getTitle()
					, dto.getWriter()
					, dto.getWritedate()
					, dto.getReaded()
					);
		}
	} // if
	System.out.println("--------------------------------------------------------------");
	// System.out.println("\t\t\t [1] 2 3 4 5 6 7 8 9 10 >");  // ํŽ˜์ด์ง•๋ธ”๋Ÿญ
	String pagingBlock = this.service.pagingService(
			   this.currentPage
			   , this.numberPerPage
			   , this.numberOfPageBlock
			);
	System.out.println(pagingBlock);
	System.out.println("--------------------------------------------------------------");
	
	์ผ์‹œ์ •์ง€();
}

[3. ์ƒ์„ธ๋ณด๊ธฐ]

1) DAO Impl

// 3-1. ์กฐํšŒ์ˆ˜ ์ฆ๊ฐ€
// ๋‹จ์œ„ ํ…Œ์ŠคํŠธ
@Override
public int increaseReaded(int seq) throws SQLException {
	String sql =   "UPDATE tbl_cstvsboard "
			+ "SET readed = readed + 1 "
			+ "WHERE seq = ? ";
	this.pstmt = this.conn.prepareStatement(sql);
	// ?
	this.pstmt.setInt(1, seq);
	int rowCount = this.pstmt.executeUpdate();		
	this.pstmt.close();		
	return rowCount;
}
// 3-2. ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€ ์ •๋ณด ๋ฐ˜ํ™˜
@Override
public BoardDTO view(int seq) throws SQLException {
	
    String sql = " SELECT seq, writer, email, title, readed, writedate, content "
			+ " FROM tbl_cstvsboard "
			+ " WHERE seq = ? ";
	this.pstmt = this.conn.prepareStatement(sql);
	// ?
	this.pstmt.setInt(1, seq);
	this.rs = this.pstmt.executeQuery()	;

	BoardDTO dto = null;

	if( rs.next() ) {

		dto = new BoardDTO();
		dto.setSeq( rs.getInt("seq") );
		dto.setWriter(rs.getString("writer"));
		dto.setEmail(rs.getString("email"));
		dto.setTitle(rs.getString("title"));
		dto.setReaded( rs.getInt("readed") );
		dto.setWritedate( rs.getDate("writedate"));
		dto.setContent(rs.getString("content"));			

	} // if

	this.pstmt.close();
	this.rs.close();

	// ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€์ด ์—†๋Š” ๊ฒฝ์šฐ๋Š” dto = null
	return dto;
}

2) DAO Impl ํ…Œ์ŠคํŠธ

@Test
void increaseReaded_test() {
	//fail("Not yet implemented");
	Connection conn = DBConn.getConnection();
	BoardDAOImpl dao = new BoardDAOImpl(conn); 

	try {
		int rowCount = dao.increaseReaded( 363 );
		System.out.println( rowCount );
	} catch (SQLException e) {
		e.printStackTrace();
	}		 
	DBConn.close();
}

3) Service

// 3. ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ธ ๋ณด๊ธฐ ์„œ๋น„์Šค ๋ฉ”์„œ๋“œ 
public BoardDTO viewService(int seq) {
	BoardDTO dto = null;

	try {
		// 1. ๋กœ๊ทธ ๊ธฐ๋ก ์„œ๋น„์Šค
		System.out.println("> ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ธ๋ณด๊ธฐ -> ๋กœ๊ทธ ๊ธฐ๋ก ์ž‘์—…...");
		// 2. ์กฐํšŒ์ˆ˜ ์ฆ๊ฐ€
		this.dao.increaseReaded(seq);
		// 3. 
		dto = this.dao. view(seq);			 
	} catch (SQLException e) {			 
		e.printStackTrace();
	}
	return dto;
}

4) Service ํ…Œ์ŠคํŠธ

5) Controller

private void ์ƒ์„ธ๋ณด๊ธฐ() {
	System.out.print("> ๊ฒŒ์‹œ๊ธ€ ๋ฒˆํ˜ธ(seq) ์ž…๋ ฅ ? " );  
	int seq = this.scanner.nextInt();
	this.scanner.nextLine(); // 13,10
		
	BoardDTO dto = this.service.viewService(seq);
	if( dto == null ) {
	    System.out.println("> ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€ ์กด์žฌ X ");
	    return ;
	}
		
	//
	System.out.println("\tใ„ฑ. ๊ธ€๋ฒˆํ˜ธ : " + seq );
	System.out.println("\tใ„ด. ์ž‘์„ฑ์ž : " + dto.getWriter() );
	System.out.println("\tใ„ท. ์กฐํšŒ์ˆ˜ : " + dto.getReaded() );
	System.out.println("\tใ„น. ๊ธ€์ œ๋ชฉ : " + dto.getTitle() );
	System.out.println("\tใ…. ๊ธ€๋‚ด์šฉ : " + dto.getContent() );
	System.out.println("\tใ…‚. ์ž‘์„ฑ์ผ : " + dto.getWritedate() );
		
	System.out.println("\t\n [์ˆ˜์ •] [์‚ญ์ œ] [๋ชฉ๋ก(home)]");
		
	์ผ์‹œ์ •์ง€();
}

[4. ์ˆ˜์ •ํ•˜๊ธฐ]

1) DAO Impl

@Override
public int update(BoardDTO dto) throws SQLException {

	String sql = "UPDATE  tbl_cstvsboard  "
			+ " SET email=?, title=?, content=? "
			+ " WHERE seq =  ? ";		

	this.pstmt = this.conn.prepareStatement(sql);

	this.pstmt.setString(1, dto.getEmail());
	this.pstmt.setString(2, dto.getTitle());
	this.pstmt.setString(3, dto.getContent());
	this.pstmt.setInt(4, dto.getSeq());

	int rowCount   = this.pstmt.executeUpdate();		

	this.pstmt.close();		
	return rowCount;
}

2) DAO Impl ํ…Œ์ŠคํŠธ

3) Service

// seq, email, title, content
public int updateService(BoardDTO dto) {		
	int rowCount = 0;

	try {
		// 1. ๋กœ๊ทธ ๊ธฐ๋ก
		// 2.
		// 3. 
		rowCount =  this.dao.update( dto );

	} catch (SQLException e) { 
		e.printStackTrace();
	}
	return rowCount;
}

 

4) Service ํ…Œ์ŠคํŠธ

5) Controller

private void ์ˆ˜์ •ํ•˜๊ธฐ() {
	System.out.print("> ์ˆ˜์ •ํ•  ๊ธ€๋ฒˆํ˜ธ(seq), ์ด๋ฉ”์ผ, ์ œ๋ชฉ, ๋‚ด์šฉ ์ž…๋ ฅ ? ");
	int seq = this.scanner.nextInt();
	// ์ˆ˜์ •ํ•  ๊ธ€ ๋ฒˆํ˜ธ์˜ ๊ฒŒ์‹œ๊ธ€ ์ •๋ณด ์ถœ๋ ฅ..
	BoardDTO dto = this.service.viewService(seq);
	if( dto == null ) {
	    System.out.println("> ํ•ด๋‹น ๊ฒŒ์‹œ๊ธ€ ์กด์žฌ X ");
	    return ;
	} 
	System.out.println("\tใ„ฑ. ๊ธ€๋ฒˆํ˜ธ : " + seq );
	System.out.println("\tใ„ด. ์ž‘์„ฑ์ž : " + dto.getWriter() );
	System.out.println("\tใ„ท. ์กฐํšŒ์ˆ˜ : " + dto.getReaded() );
	System.out.println("\tใ„น. ๊ธ€์ œ๋ชฉ : " + dto.getTitle() );
	System.out.println("\tใ…. ๊ธ€๋‚ด์šฉ : " + dto.getContent() );
	// ์ˆ˜์ •์ผ, ์ž‘์„ฑ์ผ
	System.out.println("\tใ…‚. ์ž‘์„ฑ์ผ : " + dto.getWritedate() );		
	System.out.println("\t\n [์ˆ˜์ •] [์‚ญ์ œ] [๋ชฉ๋ก(home)]");
	
	// ์ด๋ฉ”์ผ/์ œ๋ชฉ/๋‚ด์šฉ ๋งŒ ์ˆ˜์ •.
	String email = this.scanner.next();
	String title = this.scanner.next();
	String content = this.scanner.next();
		
	this.scanner.nextLine(); // 13.10
	
	// this.service.updateService( seq, email , title, cotent);
	
	dto = new BoardDTO();
	dto.setSeq(seq);
	dto.setEmail(email);
	dto.setTitle(title);
	dto.setContent(content);
	int rowCount = this.service.updateService( dto);
	
	if( rowCount == 1 ) {
		System.out.printf("> ๊ธ€๋ฒˆํ˜ธ( %d ) ๊ฒŒ์‹œ๊ธ€ ์ˆ˜์ • ์™„๋ฃŒ!!!\n ", seq);
	}		
	์ผ์‹œ์ •์ง€();
}

[5. ์‚ญ์ œํ•˜๊ธฐ]

1) DAO Impl

// 4. ์‚ญ์ œ
@Override
public String getOriginalPwd(int seq) throws SQLException {
	String pwd = null;
	String sql = "SELECT pwd "
			+ " FROM tbl_cstvsboard "
			+ " WHERE seq =  ? ";

	this.pstmt = this.conn.prepareStatement(sql); 
	this.pstmt.setInt(1, seq);
	this.rs = this.pstmt.executeQuery()	;		
	if( rs.next() ) {
		pwd = rs.getString("pwd");
	}		
	this.pstmt.close();
	this.rs.close();
	return pwd;
}

@Override
public int delete(int seq) throws SQLException {      
	String sql = "DELETE FROM  tbl_cstvsboard  " 
			+ " WHERE seq =  ? ";		
	this.pstmt = this.conn.prepareStatement(sql); 
	this.pstmt.setInt(1, seq);
	int rowCount   = this.pstmt.executeUpdate();		
	this.pstmt.close();		
	return rowCount;
}

2) DAO Impl ํ…Œ์ŠคํŠธ

3) Service

public int deleteService(int seq, String pwd) {
	int result = 0;

	try {
		// ์ €์žฅ ํ”„๋กœ์‹œ์ €
		// 1. 
		String originalPwd = this.dao.getOriginalPwd(seq);
		// 
		if(  originalPwd.equals( pwd ) ) {
			// 2.
			result =  this.dao.delete( seq );
		}else {
			result = -1;
		}
	} catch (SQLException e) { 
		e.printStackTrace();
	}
	return result;
}

4) Service ํ…Œ์ŠคํŠธ

5) Controller

private void ์‚ญ์ œํ•˜๊ธฐ() {
	// 1. ์‚ญ์ œํ•  ๊ธ€๋ฒˆํ˜ธ(seq)  ์ž…๋ ฅ 
	// 2. ๊ฒŒ์‹œ๊ธ€ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ
	System.out.print("> ์‚ญ์ œํ•  ๊ธ€๋ฒˆํ˜ธ(seq), ๋น„๋ฐ€๋ฒˆํ˜ธ(pwd) ์ž…๋ ฅ ? ");
	int seq = this.scanner.nextInt();
	String pwd = this.scanner.next();		
	this.scanner.nextLine(); // 13,10
		
	//   -1(๋น„๋ฐ€๋ฒˆํ˜ธ ํ‹€๋ฆฐ ๊ฒฝ์šฐ )    1(์‚ญ์ œ ์„ฑ๊ณต )   0(์‚ญ์ œ ์‹คํŒจ )
	int result = this.service.deleteService(seq, pwd);
		
	if(result == -1 ) {
		System.out.println("> ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ํ‹€๋ฆฝ๋‹ˆ๋‹ค. ");
	}else if( result == 1 ){
		System.out.println("> ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ ์™„๋ฃŒ!!!");
	}else if( result == 0 ) {
		System.out.println("> ๊ฒŒ์‹œ๊ธ€ ์‚ญ์ œ ์‹คํŒจ!!!");
	}
	์ผ์‹œ์ •์ง€();
}

[6. ๊ฒ€์ƒ‰ํ•˜๊ธฐ]

1) DAO Impl

// ํ•œ ํŽ˜์ด์ง€ : 10 ์ถœ๋ ฅ  numberPerPagge
// ํ˜„์žฌ  ํŽ˜์ด์ง€ : 1       currentPage
// ๊ฒ€์ƒ‰๋œ ๊ฒฐ๊ณผ : 35 ํ–‰ -> 1๋ฒˆ ํŽ˜์ด์ง€ 10๊ฐœ ์ถœ๋ ฅ

@Override
public ArrayList<BoardDTO> search( int currentPage, int numberPerPage, int searchCondition, String searchWord) throws SQLException {

	ArrayList<BoardDTO> list = null;
	BoardDTO dto = null; 

	int begin = ( currentPage-1)*numberPerPage + 1;
	int end = begin + numberPerPage -1;

	String sql = 
			" SELECT b.* "
				+ " FROM (  "
				+ "        SELECT ROWNUM no, t.* "
				+ "        FROM (  "
				+ "                SELECT seq, writer, email, title, readed, writedate "
				+ "                FROM tbl_cstvsboard ";

	switch (searchCondition) {
	case 1: // ์ œ๋ชฉ
		sql += " WHERE REGEXP_LIKE( title , ? , 'i' ) ";
		break;
	case 2:  // ๋‚ด์šฉ
		sql += " WHERE REGEXP_LIKE( content , ? , 'i' ) ";
		break;
	case 3: // ์ž‘์„ฑ์ž
		sql += " WHERE REGEXP_LIKE( writer , ? , 'i' ) ";
		break;
	case 4:  // ์ œ๋ชฉ + ๋‚ด์šฉ
		sql += " WHERE REGEXP_LIKE( title , ? , 'i' )  OR REGEXP_LIKE( content , ? , 'i' )  ";
		break;									
	}		
	sql +=  "                ORDER BY seq DESC "
			+ "              ) t "
			+ "     ) b "
			+ " WHERE b.no BETWEEN ? AND ?";
	this.pstmt = this.conn.prepareStatement(sql);

	this.pstmt.setString(1,  searchWord );
	if ( searchCondition == 4) {
		this.pstmt.setString(2,  searchWord );
		this.pstmt.setInt(3, begin);
		this.pstmt.setInt(4, end);
	} else {
		// BETWEEN ? AND ?"
		this.pstmt.setInt(2, begin);
		this.pstmt.setInt(3, end);
	}

	this.rs =  this.pstmt.executeQuery();

	//		int no, seq, readed;
	//		String  writer, email, title;
	//		Date writedate;

	if ( this.rs.next()) {
		list = new ArrayList<BoardDTO>();

		do {
			dto = new BoardDTO();
			dto.setSeq(this.rs.getInt("seq"));
			dto.setReaded(this.rs.getInt("readed"));
			dto.setWriter( this.rs.getString("writer"));
			dto.setEmail( this.rs.getString("email"));

			if( searchCondition == 1 ) {  // ์ œ๋ชฉ ๊ฒ€์ƒ‰
				String title =	this.rs.getString("title").replace(searchWord, "["+searchWord +"]");
				dto.setTitle( title );
			} else {
				dto.setTitle( this.rs.getString("title"));
			}

			dto.setWritedate(this.rs.getDate("writedate"));
			list.add(dto);
		} while ( this.rs.next() );
	} // if

	this.rs.close(); // 
	this.pstmt.close();

	return  list;  // ๊ฒŒ์‹œ๊ธ€์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด  list = null ..
}

2) DAO Impl ํ…Œ์ŠคํŠธ

3) Service

public ArrayList<BoardDTO> searchService( int  currentPage, int numberPerPage,  int searchCondition, String searchWord) {
		
	ArrayList<BoardDTO> list = null;
		
	try {
		// 1. ๋กœ๊ทธ ๊ธฐ๋ก
		// 2.
		// 3. 
		list =  this.dao.search(  currentPage, numberPerPage,  searchCondition,  searchWord);
	} catch (SQLException e) { 
		e.printStackTrace();
	}
	return list;
}

4) Service ํ…Œ์ŠคํŠธ

5) Controller

private int currentPage = 1;  // ๋ชฉ๋ก ๋ณด๊ณ  ์žˆ๋Š” ํ˜„์žฌ ํŽ˜์ด์ง€.
private int numberPerPage = 10; // ํ•œ ํŽ˜์ด์ง€์— ์ถœ๋ ฅํ•  ๊ฒŒ์‹œ๊ธ€ ์ˆ˜
private int numberOfPageBlock = 10;   // 1 2 3 4 5 6 7 8 9 10

private void ๊ฒ€์ƒ‰ํ•˜๊ธฐ() {
	// 1. ์ œ๋ชฉ 1 , ์ž‘์„ฑ์ž 2,  ๋‚ด์šฉ 3 , ์ œ๋ชฉ + ๋‚ด์šฉ  4 ๊ฒ€์ƒ‰์กฐ๊ฑด ์ž…๋ ฅ
	// 2. ๊ฒ€์ƒ‰์–ด ์ž…๋ ฅ
		
	System.out.print("> ๊ฒ€์ƒ‰๋œ ํ˜„์žฌ ํŽ˜์ด์ง€( currentPage) ์ž…๋ ฅ ? " );  
	this.currentPage = this.scanner.nextInt(); 
	int searchCondition;  // ๊ฒ€์ƒ‰์กฐ๊ฑด
	System.out.print("> ๊ฒ€์ƒ‰ ์กฐ๊ฑด : ์ œ๋ชฉ(1) , ๋‚ด์šฉ(2), ์ž‘์„ฑ์ž(3), ์ œ๋ชฉ+๋‚ด์šฉ(4) ์„ ํƒ  ? ");
	searchCondition = this.scanner.nextInt();
	this.scanner.nextLine();  // 13,10 
	System.out.print("> ๊ฒ€์ƒ‰์–ด ์ž…๋ ฅ ? ");
	String searchWord = this.scanner.nextLine();		 

	ArrayList<BoardDTO> list = this.service.searchService(
			 currentPage, numberPerPage	, searchCondition, searchWord);
	
    //  ๋ชฉ๋ก๋ณด๊ธฐ()  ์ฝ”๋”ฉ ๋ณต์‚ฌ ๋ถ™์ด๊ธฐ
	System.out.println("\t\t\t  ๊ฒŒ์‹œํŒ");
	System.out.println("--------------------------------------------------------------");
	System.out.printf("%s\t%-40s\t%s\t%-10s\t%s\n", 
			"๊ธ€๋ฒˆํ˜ธ","๊ธ€์ œ๋ชฉ","๊ธ€์“ด์ด","์ž‘์„ฑ์ผ","์กฐํšŒ์ˆ˜");
	System.out.println("--------------------------------------------------------------");
	
    if (  list == null) {
		System.out.println("\t\t ๊ฒ€์ƒ‰๋œ ๊ฒŒ์‹œ๊ธ€ ์กด์žฌ X ");
	} else {
		Iterator<BoardDTO> ir =list.iterator();
		while (ir.hasNext()) {
			BoardDTO dto =   ir.next();
			System.out.printf("%d\t%-30s  %s\t%-10s\t%d\n",
					dto.getSeq()
					, dto.getTitle()
					, dto.getWriter()
					, dto.getWritedate()
					, dto.getReaded()
					);
		}
	} // if
    
	System.out.println("--------------------------------------------------------------");
	// System.out.println("\t\t\t [1] 2 3 4 5 6 7 8 9 10 >");  // ํŽ˜์ด์ง•๋ธ”๋Ÿญ
	// ๋ชฉ๋ก๋ณด๊ธฐ์˜ ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ ๋ถ€๋ถ„์„ ์ฐธ์กฐํ•˜๋ฉด ์ˆ˜์ •...
	String pagingBlock = this.service.pagingService(
    	this.currentPage
    	, this.numberPerPage
    	, this.numberOfPageBlock
    	, searchCondition   // ๊ฒ€์ƒ‰์กฐ๊ฑด
    	, searchWord           // ๊ฒ€์ƒ‰์–ด
	);
	System.out.println(pagingBlock);
	System.out.println("--------------------------------------------------------------");
	์ผ์‹œ์ •์ง€();
}

[7. ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ1 - ๋ชฉ๋ก๋ณด๊ธฐ]

1) ์ด ๋ ˆ์ฝ”๋“œ ์ˆ˜ ํŒŒ์•…
2) ํ•œ ํŽ˜์ด์ง€์— ๋ช‡ ๊ฐœ ๋ ˆ์ฝ”๋“œ ์ˆ˜ 
3) ์ด ํŽ˜์ด์ง€ = ์ด ๋ ˆ์ฝ”๋“œ / ํ•œ ํŽ˜์ด์ง€ ๋ ˆ์ฝ”๋“œ ์ˆ˜ (์˜ฌ๋ฆผ)
4) ํ˜„์žฌ ํŽ˜์ด์ง€ 
5) ํŽ˜์ด์ง€ ๋ธ”๋Ÿญ ์ˆ˜ (1~10๊นŒ์ง€)
6) ์‹œ์ž‘ ๋ธ”๋Ÿญ (1)
7) ๋ ๋ธ”๋Ÿญ (10)
8) prev < ์ด์ „, next > ๋‹ค์Œ ํ‘œ์‹œ ์—ฌ๋ถ€

1) DAO Impl

@Override
public int getTotalRecords() throws SQLException {
	int totalRecords = 0;

	String sql = "SELECT COUNT(*) "
			+ " FROM tbl_cstvsboard ";	 

	this.pstmt = this.conn.prepareStatement(sql); 

	this.rs = this.pstmt.executeQuery()	;		
	if( rs.next() ) {
		totalRecords = rs.getInt( 1 );
	}		
	this.pstmt.close();
	this.rs.close();

	return totalRecords;
}
@Override
public int getTotalPages(int numberPerPage) throws SQLException {
	int totalPages = 0;

	String sql = "SELECT CEIL(COUNT(*)/?) "
			+ " FROM tbl_cstvsboard ";	 

	this.pstmt = this.conn.prepareStatement(sql); 
	this.pstmt.setInt(1, numberPerPage) ;
	this.rs = this.pstmt.executeQuery()	;		
	if( rs.next() ) {
		totalPages = rs.getInt( 1 );
	}		
	this.pstmt.close();
	this.rs.close();

	return totalPages;
}

2) DAO Impl ํ…Œ์ŠคํŠธ

@Test
void getTotalRecords_test() {
		 
	Connection conn = DBConn.getConnection();
	BoardDAOImpl dao = new BoardDAOImpl(conn); 
		
	try {
		 // int totalRecords = dao.getTotalRecords();		   
		 // System.out.println( totalRecords );
			
		int totalPages = dao.getTotalPages( 10  );
		 System.out.println( totalPages );
			
	} catch (SQLException e) {
		e.printStackTrace();
	}		 
	DBConn.close();
}

3) Service

//  "\t\t\t [1] 2 3 4 5 6 7 8 9 10 >"
public String pagingService(int currentPage, int numberPerPage, int numberOfPageBlock) {
		
	String pagingBlock = "\t\t\t";
		
	try {
		int totalRecords = this.dao.getTotalRecords()  ;
		// int totalPages =   totalRecords / numberPerPage + 1 ;		
		int totalPages =   this.dao.getTotalPages( numberPerPage )  ;
		int begin = ( currentPage - 1)/numberOfPageBlock * numberOfPageBlock + 1;
		int end    = begin + numberOfPageBlock - 1;
		if( end > totalPages )  end = totalPages;
		  
		boolean prev =   begin == 1 ?  false : true;
		boolean next  =  end == totalPages ? false: true;
		
		if(prev)  pagingBlock +=" < " ;
		for (int j = begin; j <= end; j++) {
			 pagingBlock += String.format(  currentPage == j ? " [%d]" : " %d ", j) ;
		}
		if( next )   pagingBlock +=" > " ;
	} catch( SQLException e) {
		e.printStackTrace();
	}	
	return pagingBlock;
}

4) Service ํ…Œ์ŠคํŠธ

5) Controller

private void ๋ชฉ๋ก๋ณด๊ธฐ() {
	// 357 INSERT
	// BoardService.selectService()/  BoardDAO.select()
	System.out.print("> ํ˜„์žฌ ํŽ˜์ด์ง€( currentPage) ์ž…๋ ฅ ? " );  
	this.currentPage = this.scanner.nextInt();
	this.scanner.nextLine(); // 13,10

	ArrayList<BoardDTO> list = this.service.selectService(this.currentPage, this.numberPerPage);

	// M[V]C  - ์ถœ๋ ฅ ๋‹ด๋‹น ๋ทฐ.
	System.out.println("\t\t\t  ๊ฒŒ์‹œํŒ");
	System.out.println("--------------------------------------------------------------");
	System.out.printf("%s\t%-40s\t%s\t%-10s\t%s\n", 
			"๊ธ€๋ฒˆํ˜ธ","๊ธ€์ œ๋ชฉ","๊ธ€์“ด์ด","์ž‘์„ฑ์ผ","์กฐํšŒ์ˆ˜");
	System.out.println("--------------------------------------------------------------");

	if (  list == null) {
		System.out.println("\t\t ๊ฒŒ์‹œ๊ธ€ ์กด์žฌ X ");
	} else {
		Iterator<BoardDTO> ir =list.iterator();
		while (ir.hasNext()) {
			BoardDTO dto =   ir.next();
			System.out.printf("%d\t%-30s  %s\t%-10s\t%d\n",
					dto.getSeq()
					, dto.getTitle()
					, dto.getWriter()
					, dto.getWritedate()
					, dto.getReaded()
					);
		}// if

	System.out.println("--------------------------------------------------------------");
	// System.out.println("\t\t\t [1] 2 3 4 5 6 7 8 9 10 >");  // ํŽ˜์ด์ง•๋ธ”๋Ÿญ
	String pagingBlock = this.service.pagingService(
			   this.currentPage
			   , this.numberPerPage
			   , this.numberOfPageBlock
			);
	System.out.println(pagingBlock);
	System.out.println("--------------------------------------------------------------");
	
	์ผ์‹œ์ •์ง€();
}

 

[7. ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ2 - ๊ฒ€์ƒ‰๊ฒฐ๊ณผ]

1) ์ด ๋ ˆ์ฝ”๋“œ ์ˆ˜ ํŒŒ์•…
2) ํ•œ ํŽ˜์ด์ง€์— ๋ช‡ ๊ฐœ ๋ ˆ์ฝ”๋“œ ์ˆ˜ 
3) ์ด ํŽ˜์ด์ง€ = ์ด ๋ ˆ์ฝ”๋“œ / ํ•œ ํŽ˜์ด์ง€ ๋ ˆ์ฝ”๋“œ ์ˆ˜ (์˜ฌ๋ฆผ)
4) ํ˜„์žฌ ํŽ˜์ด์ง€ 
5) ํŽ˜์ด์ง€ ๋ธ”๋Ÿญ ์ˆ˜ (1~10๊นŒ์ง€)
6) ์‹œ์ž‘ ๋ธ”๋Ÿญ (1)
7) ๋ ๋ธ”๋Ÿญ (10)
8) prev < ์ด์ „, next > ๋‹ค์Œ ํ‘œ์‹œ ์—ฌ๋ถ€

1) DAO Impl

// ๊ฒ€์ƒ‰ํ•˜๊ธฐ
@Override
public int getTotalRecords(int searchCondition, String searchWord) throws SQLException {
	int totalRecords = 0;

	String sql = "SELECT COUNT(*) "
			+ " FROM tbl_cstvsboard ";	 
	switch (searchCondition) {
	case 1: // ์ œ๋ชฉ
		sql += " WHERE REGEXP_LIKE( title , ? , 'i' ) ";
		break;
	case 2:  // ๋‚ด์šฉ
		sql += " WHERE REGEXP_LIKE( content , ? , 'i' ) ";
		break;
	case 3: // ์ž‘์„ฑ์ž
		sql += " WHERE REGEXP_LIKE( writer , ? , 'i' ) ";
		break;
	case 4:  // ์ œ๋ชฉ + ๋‚ด์šฉ
		sql += " WHERE REGEXP_LIKE( title , ? , 'i' )  OR REGEXP_LIKE( content , ? , 'i' )  ";
		break;									
	}		

	this.pstmt = this.conn.prepareStatement(sql); 		
	this.pstmt.setString(1, searchWord);
	if ( searchCondition == 4 ) {
		this.pstmt.setString(2, searchWord);
	}		 
	this.rs = this.pstmt.executeQuery()	;		
	if( rs.next() ) {
		totalRecords = rs.getInt( 1 );
	}		
	this.pstmt.close();
	this.rs.close();
    
	return totalRecords;
}
// ๋‹จ์œ„ ํ…Œ์ŠคํŠธ
@Override
public int getTotalPages(int numberPerPage,int searchCondition, String searchWord) throws SQLException {
	int totalPages = 0;

	String sql = "SELECT CEIL(COUNT(*)/?) "
			+ " FROM tbl_cstvsboard ";	 
	switch (searchCondition) {
	case 1: // ์ œ๋ชฉ
		sql += " WHERE REGEXP_LIKE( title , ? , 'i' ) ";
		break;
	case 2:  // ๋‚ด์šฉ
		sql += " WHERE REGEXP_LIKE( content , ? , 'i' ) ";
		break;
	case 3: // ์ž‘์„ฑ์ž
		sql += " WHERE REGEXP_LIKE( writer , ? , 'i' ) ";
		break;
	case 4:  // ์ œ๋ชฉ + ๋‚ด์šฉ
		sql += " WHERE REGEXP_LIKE( title , ? , 'i' )  OR REGEXP_LIKE( content , ? , 'i' )  ";
		break;									
	}	

	this.pstmt = this.conn.prepareStatement(sql); 
	this.pstmt.setInt(1, numberPerPage) ;
	this.pstmt.setString(2,  searchWord);
	if( searchCondition == 4 ) this.pstmt.setString(3,  searchWord);   

	this.rs = this.pstmt.executeQuery()	;		
	if( rs.next() ) {
		totalPages = rs.getInt( 1 );
	}		
	this.pstmt.close();
	this.rs.close();
    
	return totalPages;
}

2) DAO Impl ํ…Œ์ŠคํŠธ

3) Service

// ๊ฒ€์ƒ‰ํ•˜๊ธฐ - ํŽ˜์ด์ง•์ฒ˜๋ฆฌ ๋ฉ”์„œ๋“œ
public String pagingService(int currentPage, int numberPerPage, int numberOfPageBlock
		, int searchCondition,			String searchWord) {
	String pagingBlock = "\t\t\t";
		
	try {
		int totalRecords = this.dao.getTotalRecords(searchCondition, searchWord)  ;
		// int totalPages =   totalRecords / numberPerPage + 1 ;		
		int totalPages =   this.dao.getTotalPages( numberPerPage , searchCondition, searchWord)  ;
		int begin = ( currentPage - 1)/numberOfPageBlock * numberOfPageBlock + 1;
		int end    = begin + numberOfPageBlock - 1;
		if( end > totalPages )  end = totalPages;
				  
		boolean prev =   begin == 1 ?  false : true;
		boolean next  =  end == totalPages ? false: true;
				
		if(prev)  pagingBlock +=" < " ;
		for (int j = begin; j <= end; j++) {
			 pagingBlock += String.format(  currentPage == j ? " [%d]" : " %d ", j) ;
		}
		if( next )   pagingBlock +=" > " ;
	} catch( SQLException e) {
		e.printStackTrace();
	}		
	return pagingBlock;
}

4) Service ํ…Œ์ŠคํŠธ

5) Controller

private void ๊ฒ€์ƒ‰ํ•˜๊ธฐ() {
	// 1. ์ œ๋ชฉ 1 , ์ž‘์„ฑ์ž 2,  ๋‚ด์šฉ 3 , ์ œ๋ชฉ + ๋‚ด์šฉ  4            ๊ฒ€์ƒ‰์กฐ๊ฑด ์ž…๋ ฅ
	// 2. ๊ฒ€์ƒ‰์–ด ์ž…๋ ฅ
		
	System.out.print("> ๊ฒ€์ƒ‰๋œ ํ˜„์žฌ ํŽ˜์ด์ง€( currentPage) ์ž…๋ ฅ ? " );  
	this.currentPage = this.scanner.nextInt(); 
	int searchCondition;  // ๊ฒ€์ƒ‰์กฐ๊ฑด
	System.out.print("> ๊ฒ€์ƒ‰ ์กฐ๊ฑด : ์ œ๋ชฉ(1) , ๋‚ด์šฉ(2), ์ž‘์„ฑ์ž(3), ์ œ๋ชฉ+๋‚ด์šฉ(4) ์„ ํƒ  ? ");
	searchCondition = this.scanner.nextInt();
	this.scanner.nextLine();  // 13,10 
	System.out.print("> ๊ฒ€์ƒ‰์–ด ์ž…๋ ฅ ? ");
	String searchWord = this.scanner.nextLine();		 

	ArrayList<BoardDTO> list = this.service.searchService(
			 currentPage, numberPerPage	, searchCondition, searchWord);

	//  ๋ชฉ๋ก๋ณด๊ธฐ()  ์ฝ”๋”ฉ ๋ณต์‚ฌ ๋ถ™์ด๊ธฐ
	System.out.println("\t\t\t  ๊ฒŒ์‹œํŒ");
	System.out.println("--------------------------------------------------------------");
	System.out.printf("%s\t%-40s\t%s\t%-10s\t%s\n", 
			"๊ธ€๋ฒˆํ˜ธ","๊ธ€์ œ๋ชฉ","๊ธ€์“ด์ด","์ž‘์„ฑ์ผ","์กฐํšŒ์ˆ˜");
	System.out.println("--------------------------------------------------------------");

	if (  list == null) {
		System.out.println("\t\t ๊ฒ€์ƒ‰๋œ ๊ฒŒ์‹œ๊ธ€ ์กด์žฌ X ");
	} else {
		Iterator<BoardDTO> ir =list.iterator();
		while (ir.hasNext()) {
			BoardDTO dto =   ir.next();
			System.out.printf("%d\t%-30s  %s\t%-10s\t%d\n",
					dto.getSeq()
					, dto.getTitle()
					, dto.getWriter()
					, dto.getWritedate()
					, dto.getReaded()
					);
		}
	} // if

	System.out.println("--------------------------------------------------------------");
	// System.out.println("\t\t\t [1] 2 3 4 5 6 7 8 9 10 >");  // ํŽ˜์ด์ง•๋ธ”๋Ÿญ
	// ๋ชฉ๋ก๋ณด๊ธฐ์˜ ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ ๋ถ€๋ถ„์„ ์ฐธ์กฐํ•˜๋ฉด ์ˆ˜์ •...
	String pagingBlock = this.service.pagingService(
			   this.currentPage
			   , this.numberPerPage
			   , this.numberOfPageBlock
			   , searchCondition   // ๊ฒ€์ƒ‰์กฐ๊ฑด
			   , searchWord           // ๊ฒ€์ƒ‰์–ด
			);
	System.out.println(pagingBlock);
	System.out.println("--------------------------------------------------------------");
	
	์ผ์‹œ์ •์ง€();
}

๊ณ„์†์—ฌ๋ถ€ํ™•์ธ Controller

// days02.Ex02_02.java
private static char _continue = 'y';
private static void ๊ณ„์†์—ฌ๋ถ€ํ™•์ธ() {
	System.out.print("> ๊ณ„์† ํ• ๊ฑฐ๋ƒ ? ");
	try {
		_continue =  (char)System.in.read();
		System.in.skip( System.in.available()  ); // 13, 10 ์ œ๊ฑฐ
	} catch (IOException e) { 
		e.printStackTrace();
	}
}

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ