보면서 코드 작성하려고 하나로 합친것


import java.util.Date;

public class Notice {
	private int id ;
	private String title ;
	private String wirterid ;
	private Date regDate;
	private String content ;
	private int hit ;
	private String files;
	
	
	public Notice() {
		
	}

	public Notice(int id, String title, String wirterid, Date regDate, String content, int hit, String files) {
		this.id = id;
		this.title = title;
		this.wirterid = wirterid;
		this.regDate = regDate;
		this.content = content;
		this.hit = hit;
		this.files = files;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getWirterid() {
		return wirterid;
	}
	public void setWirterid(String wirterid) {
		this.wirterid = wirterid;
	}
	public Date getRegDate() {
		return regDate;
	}
	public void setRegDate(Date regDate) {
		this.regDate = regDate;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public int getHit() {
		return hit;
	}
	public void setHit(int hit) {
		this.hit = hit;
	}

	public String getFiles() {
		return files;
	}

	public void setFiles(String files) {
		this.files = files;
	}	
}

Main

import java.sql.SQLException;

import com.lec.app.console.NoticeConsole;

public class program5 {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		NoticeConsole console = new NoticeConsole();
		//int page;
		
		EXIT:
		while(true) {
			console.printNoticeList();
			int menu =console.inputNoticeMenu();
			
				
			
			switch(menu) {
			case 1: //상세 조회
				break;
			case 2:  //이전
				console.movePrevList();
				//page--;
				break;
				
			case 3: // 다음
				console.moveNextList();
				break;
				//page++;
			case 4: //글쓰기
				break;
			case 5: //검색
				console.inputSearchWord();
				break;
			case 6:
				System.out.println("Bye~~~~");
				break EXIT;
			default:
				System.out.println("메뉴는 1~4번까지 입력하실수있습니다.");
				break;
			}
		}
	}
}

NoticeConsole

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

import com.lec.app.entity.Notice;
import com.lec.app.service.NoticeService;

public class NoticeConsole {

	private NoticeService service;
	private int page;
	private String searchFiled;
	private String searchWord;
	
	public NoticeConsole() {
		service = new NoticeService();
		page= 1;
		searchFiled="TITLE";
		searchWord= "";
	}
	

	public void printNoticeList() throws ClassNotFoundException, SQLException {
		List<Notice> list = service.getList(page, searchFiled, searchWord );
		int count = service.getCount();
		int lastPage = count/10;
		lastPage = count%10 == 0?lastPage: lastPage +1;
		
		
		System.out.println("────────────────────");
		System.out.printf("<공지사항> 총 %d 게시글\n",count); 
		System.out.println("────────────────────");
		
		for(Notice n:list) {
		System.out.printf("%d. %s / %s / %s\n",
											n.getId(), n.getTitle(), n.getWirterid(),n.getRegDate());
		}
		System.out.println("────────────────────");
		System.out.printf("                %d/%d  pages\n",page,lastPage); 

	}

	public int inputNoticeMenu() {
		Scanner scan = new Scanner(System.in);
		
		System.out.printf("1.상세조회/ 2.이전 / 3.다음/ 4.글쓰기/ 5.검색/ 6.종료  >");
		String menu_ = scan.nextLine();
		int menu = Integer.parseInt(menu_);
		
		
		return menu;
	}


	public void movePrevList() {
		if(page == 1) {
			System.out.println("=================");
			System.out.println("이전 페이지가 없습니다.");
			System.out.println("=================");

			return;
		}
		page --;
	}


	public void moveNextList() throws ClassNotFoundException, SQLException {
		int count = service.getCount();
		int lastPage = count/10;
		lastPage = count%10 == 0?lastPage: lastPage +1;
		if(page == lastPage) {
			System.out.println("=================");
			System.out.println("다음 페이지가 없습니다.");
			System.out.println("=================");

			return;
		}
		page ++;
	}
	
	public void inputSearchWord() {
		Scanner scan = new Scanner(System.in);
		System.out.println("검색 범주(title/content/writerId) 중에 하나를 입력하세요.");
		System.out.print(">");
		searchFiled = scan.nextLine();
		System.out.print("검색어 >");
		searchWord =scan.nextLine();
		
	}
	
}

NoticeService

public class NoticeService {

	private String url = "jdbc:oracle:thin:@localhost:1521/xe";
	private String uid= "lec";
	private String pwd = "0000";
	private String driver = "oracle.jdbc.driver.OracleDriver";
	
	
	public List<Notice> getList(int page, String field, String query) throws ClassNotFoundException, SQLException {
		
		int start = 1+(page-1)*10;					//1, 11, 21, 31 ...
		int end = 10*page; //10 , 20 , 30, 40 .... 
		
		String sql = "SELECT * FROM NOTICE_VIEW WHERE "+field+" LIKE ? AND NUM BETWEEN ? AND ?";
			
		Class.forName(driver);
		Connection con = DriverManager.getConnection(url,uid,pwd);
		PreparedStatement st = con.prepareStatement(sql);
		st.setString(1, "%"+query+"%");
		st.setInt(2, start);
		st.setInt(3, end);
		ResultSet rs = st.executeQuery();
		
		List<Notice> list = new ArrayList<Notice>();
		
		
		while(rs.next()) {
			int id = rs.getInt("ID");
			String title = rs.getString("title");
			String wirterid = rs.getString("WRITER_ID");
			Date regDate=rs.getDate("REGDATE");
			String content = rs.getString("CONTENT");
			int hit =rs.getInt("HIT");
			String files = rs.getString("FILES");
			
			Notice notice = new Notice(
					id,
					title,
					wirterid,
					regDate,
					content,
					hit,
					files
					);
			
			list.add(notice);
			
		}
		rs.close();
		st.close();
		con.close();
		return list;
	}
	

	// Scalar ( 단일값을 얻는 함수)
	public int getCount() throws SQLException, ClassNotFoundException {
		int count = 0;
		String sql = "SELECT COUNT(ID) COUNT FROM NOTICE";
		
		Class.forName(driver);
		Connection con = DriverManager.getConnection(url,uid,pwd);
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery(sql);
		
		if(rs.next())
			count = rs.getInt("COUNT");
			
		rs.close();
		st.close();
		con.close();
		
		return count;
	}


	public int insert(Notice notice) throws ClassNotFoundException, SQLException {
		String title = notice.getTitle();
		String writerId =notice.getWirterid();
		String content =notice.getContent();
		String files = notice.getFiles();
		
		String url = "jdbc:oracle:thin:@localhost:1521/xe";
		String sql = "INSERT INTO NOTICE(TITLE,WRITER_ID, CONTENT, FILES) VALUES ( ?,?,?,?)";
		
			
		Class.forName(driver);
		Connection con = DriverManager.getConnection(url,uid,pwd);		
		PreparedStatement st = con.prepareStatement(sql);
		st.setString(1, title);
		st.setString(2, writerId);
		st.setString(3, content);
		st.setString(4, files);
		
		int result = st.executeUpdate();
		
		st.close();
		con.close();
		return result;
	}
	
	public int update(Notice notice) throws ClassNotFoundException, SQLException {
		String title = notice.getTitle();
		String content =notice.getContent();
		String files =notice.getFiles();
		int id = notice.getId();
		
		String url = "jdbc:oracle:thin:@localhost:1521/xe";
		String sql = "" + 
				"  UPDATE NOTICE " + 
				"SET" + 
				"    TITLE = ?," + 
				"    CONTENT =?," + 
				"    FILES=?" + 
				"WHERE ID = ?";
		
			
		Class.forName(driver);
		Connection con = DriverManager.getConnection(url,uid,pwd);
		PreparedStatement st = con.prepareStatement(sql);
		st.setString(1, title);
		st.setString(2, content);
		st.setString(3, files);
		st.setInt(4, id);
		
		int result = st.executeUpdate();
		
		st.close();
		con.close();
		return result;
	}
	
	public int delete(int id) throws ClassNotFoundException, SQLException {

		String url = "jdbc:oracle:thin:@localhost:1521/xe";
		String sql = "DELETE NOTICE WHERE ID = ?";
		
			
		Class.forName(driver);
		Connection con = DriverManager.getConnection(url,uid,pwd);
		PreparedStatement st = con.prepareStatement(sql);
		st.setInt(1, id);
		
		int result = st.executeUpdate();
		
		st.close();
		con.close();
		return result;
	}

}

댓글남기기