2 분 소요

사용자 인터페이스 붙이기(공지사항)


package com.newlecture.app.console;

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

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

public class Noticeconsole {
	
	

	private NoticeService service;
    private int page;
	
	public Noticeconsole() {
	   service = new NoticeService();
	}
	
	public void printNoticeList() throws ClassNotFoundException, SQLException {
		List<Notice> list = service.getList();
		//데이터필요
	
	System.out.println("----------------------------");
	System.out.printf("<공지사항> 총%d게시글\n",12);
	System.out.println("----------------------------");
	
	for(Notice n :list) {
	System.out.printf("%d. %s / %s / %s\n",
						n.getId(),
						n.getTitle(),
						n.getWriterld(),
						n.getRegDate());
	}
	System.out.println("----------------------------");
	System.out.printf("       %d%d pages\n", 1, 2);
	
	}
	public int inputNoticeMenu() {
		Scanner scan = new Scanner(System.in);
			System.out.printf("1.상세조회/2.이전/3.다음/4.글쓰기>");
			String menu_ = scan.nextLine();// 정수가 아닌 다른 것이 눌렸을 경우 에러가 발생한다 다음입력에 영향을 줄 수 있다
			// 메뉴를 임시변수에다가 담는다 
			int menu = Integer.parseInt(menu_); // 메뉴를 정수형으로 바꾼다
					
		
		return 0;
	}

}




 

package ex1;

import java.sql.SQLException;

import com.newlecture.app.console.Noticeconsole;

public class Program5 {
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		Noticeconsole console = new Noticeconsole();
		
		//종료메뉴를 클릭할때까지는 계속돈다
		EXIT:
			while(true) {
			console.printNoticeList();
			int menu = console.inputNoticeMenu();
			
			switch(menu) {
			case 1: //상세조회
				break;
			case 2: // 이전
				break;
			case 3: //다음 
				break;
			case 4: //글쓰기
				break;
			case 5 : //종료
				System.out.println("bye~");
				break EXIT;
			default: ////만약 범위를 벗어난 것을 입력하면?
				System.out.println("<사용방법> 메뉴는 1~4까지만 입력 할 수 있습니다");
					break;
			}
		
		
		}
	}

}



- 쿼리 서버

```JAVJA

package com.newlecture.app.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.newlecture.app.entity.Notice;

public class NoticeService {
	String url ="jdbc:oracle:thin:@localhost:1521/orcl";   
	private String uid ="eunji";
	private String pwd="1234";

	
	public List<Notice> getList() throws ClassNotFoundException, SQLException{
		
		String sql = "SELECT*FROM NOTICE";
				
		Class.forName("oracle.jdbc.driver.OracleDriver");  
		Connection con = DriverManager.getConnection(url,uid,pwd); 
		java.sql.Statement st =con.createStatement();
		ResultSet rs = st.executeQuery(sql);
		
		List<Notice> list = new ArrayList<Notice>();
		
 
		// notice라는 그릇이 필요하다

		while(rs.next()) {
			int id = rs.getInt("ID");
			String title = rs.getString("TITLE");
			String writerld = rs.getString("WRITER_ID");
			String content = rs.getString("CONTENT");
			Date regDate=rs.getDate("REGDATE");
			int hit = rs.getInt("hit");
			String files = rs.getString("FILES");
			
			Notice notice = new Notice( // 반환하는 녀석들~
									id,
									title,
									writerld,
									content,
									regDate,
									hit,
									files
									);
			
			//notice를 add해서 추가하게되면  목록에 하나하나씩 출력하게된다
			list.add(notice);
		}

	
		
		rs.close(); 
		st.close();
		con.close();
		return list;
	}
	


	public int insert(Notice notice) throws ClassNotFoundException, SQLException {
		
		String title = notice.getTitle();
		   String writerId  =notice.getWriterld();
		   String content = notice.getContent();
		   String files = notice.getFiles();

		   
		      
   
		   String url ="jdbc:oracle:thin:@localhost:1521/orcl";   
			String sql = "INSERT INTO notice ("
					+ "    title,"
					+ "    writer_id,"
					+ "    content,"
					+ "    files"
					+ ") VALUES (?,?,?,?)";
					
					
			Class.forName("oracle.jdbc.driver.OracleDriver"); 
			Connection con = DriverManager.getConnection(url,uid,pwd); 
//			sql.Statement st =con.createStatement();
			//st.execute query,update를 실행 할때 넘겼는데 
			PreparedStatement st=con.prepareStatement(sql); //prepare 미리 준비해서 실행하라 statement자체가 쿼리문을 가지고 있다
			st.setString(1, title);
			st.setString(2, writerId);
			st.setString(3, content);
			st.setString(4, files);
			
		//prepare쓸때는 sql을 쓰면 안된다
			int result = st.executeUpdate();
//			System.out.println(result); 출력되는 문장은 지워도 된다
			
			st.close();
			con.close();
		
		
		return result;
	}
	
	
	public int update(Notice notice) throws SQLException, ClassNotFoundException {
		
		String title = notice.getTitle();
		String content = notice.getContent();
		String files =notice.getFiles();
		int id = notice.getId();


		String sql = "UPDATE notice "
				+ "SET"  // 덧셈으 하고 나면은 결과가 notice set이 되므로 결과가 
				+ "    title = ?,"
				+ "    content=?,"
				+ "    files = ?"
				+ "WHERE id =?";
		
		
				
		Class.forName("oracle.jdbc.driver.OracleDriver"); 
		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);
		
	//prepare쓸때는 sql을 쓰면 안된다
		int result = st.executeUpdate();
		

		
		st.close();
		con.close();
		
		return result;
	}
	
	public int delete(int id) throws SQLException, ClassNotFoundException {
		
//		int id = 3; 인자로 받기 때문에 지워준다


	    String url ="jdbc:oracle:thin:@localhost:1521/orcl";   
		String sql = "DELETE NOTICE WHERE ID=?";
		
				
		Class.forName("oracle.jdbc.driver.OracleDriver"); 
		Connection con = DriverManager.getConnection(url,uid,pwd); 
//		sql.Statement st =con.createStatement();
		//st.execute query,update를 실행 할때 넘겼는데 
		PreparedStatement st=con.prepareStatement(sql); //prepare 미리 준비해서 실행하라 statement자체가 쿼리문을 가지고 있다
		
		st.setInt(1, id);
	//prepare쓸때는 sql을 쓰면 안된다
		int result = st.executeUpdate();
	
		

		
		st.close();
		con.close();
		
		return result;
	}
 
}
 

	
	

카테고리:

업데이트:

댓글남기기