package db.demo2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
import java.util.Scanner;

public class BookService {

	Scanner scanner = new Scanner(System.in);
	
	// 모든 책 정보를 조회하는 기능
	public void selectAllBooks() throws Exception {
		/*
		 * 1. SQL 정의
		 * 2. 드라이브 메모리 로딩
		 * 3. Connection 획득
		 * 4. PreparedStatement 획득
		 * (5. ?에 값 설정)
		 * 6. 실행
		 * (7. ResultSet 처리)
		 * 8. 자원 해제 
		 */

		String sql = "select book_no, book_title, book_author, book_publisher, book_price, book_pubdate, book_create_date from books";
		Class.forName("oracle.jdbc.OracleDriver");
		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "zxcv1234");
		PreparedStatement pstmt = conn.prepareStatement(sql);
		
		ResultSet rs = pstmt.executeQuery();
		
		while (rs.next()) {
			int no = rs.getInt("book_no");
			String title = rs.getString("book_title");
			String author = rs.getString("book_author");
			String publisher = rs.getString("book_publisher");
			int price = rs.getInt("book_price");
			String pubdate = rs.getString("book_pubdate");
			Date createdate = rs.getDate("book_create_date");
			
			System.out.println(no + ", " + author + ", " + publisher + ", " + price + ", " + pubdate + ", " + createdate);
		}
		rs.close();
		pstmt.close();
		conn.close();
	}
	
	// 제목을 전달받아서 그 제목에 해당하는 책 정보를 조회하는 기능
	public void selectBooksByTitle(String keyword) throws Exception {
		String sql = "select book_no, book_title, book_author, book_publisher, book_price, book_pubdate, book_create_date from books where book_title like '%' || ? || '%' ";
		
		Class.forName("oracle.jdbc.OracleDriver");
		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "zxcv1234");
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, keyword);
		ResultSet rs = pstmt.executeQuery();
		
		while (rs.next()) {
			int no = rs.getInt("book_no");
			String title = rs.getString("book_title");
			String author = rs.getString("book_author");
			String publisher = rs.getString("book_publisher");
			int price = rs.getInt("book_price");
			String pubdate = rs.getString("book_pubdate");
			Date createdate = rs.getDate("book_create_date");
			
			System.out.println(no + ", " + title + ", " + author + ", " + publisher + ", " + price + ", " + pubdate + ", " + createdate);
		}
		rs.close();
		pstmt.close();
		conn.close();
		
		
	}
	
	// 책 정보를 전달받아서 db에 저장하는 기능
	public void insertNewBook(int no, String title, String author, String publisher, int price, String pubdate) throws Exception {
		String sql = "insert into books (book_no, book_title, book_author, book_publisher, book_price, book_pubdate) "
				+ " values (?, ?, ?, ?, ?, ?)";
		Class.forName("oracle.jdbc.OracleDriver");
		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "zxcv1234");
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, no);
		pstmt.setString(2, title);
		pstmt.setString(3, author);
		pstmt.setString(4, publisher);
		pstmt.setInt(5, price);
		pstmt.setString(6, pubdate);
		int rs = pstmt.executeUpdate();
		System.out.println(rs + "개의 행이 저장되었습니다.");
		
		pstmt.close();
		conn.close();
		
	}
	
	// 삭제할 책 번호를 전달받아서 DB에서 책 정보를 삭제하는 기능
	public void deleteBookByNo(int no) throws Exception {
		String sql = "delete from books where book_no = ?";
		Class.forName("oracle.jdbc.OracleDriver");
		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "zxcv1234");
		PreparedStatement pstmt = conn.prepareStatement(sql);
		pstmt.setInt(1, no);
		int rs = pstmt.executeUpdate();
		System.out.println(rs + "개의 행이 삭제되었습니다.");
		pstmt.close();
		conn.close();
		
	}
	
	// 책 정보를 전달받아서 변경하는 기능
	public void updateBook(int no, String title, String author, String publisher, int price, String pubdate) throws Exception {
		String sql = "update books set book_title = ? , book_author = ?, book_publisher = ?, book_price = ?, book_pubdate = ? where book_no = ?";
		
		Class.forName("oracle.jdbc.OracleDriver");
		Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "zxcv1234");
		PreparedStatement pstmt = conn.prepareStatement(sql);
		
		pstmt.setString(1, title);
		pstmt.setString(2, author);
		pstmt.setString(3, publisher);
		pstmt.setInt(4, price);
		pstmt.setString(5, pubdate);
		pstmt.setInt(6, no);
		
		int rs = pstmt.executeUpdate();
		System.out.println(rs + "개의 행이 변경되었습니다.");
		
		pstmt.close();
		conn.close();
		
	}
	
}

 

package db.demo2;

import java.util.Scanner;

public class BookApp {

	public static void main(String[] args) throws Exception	{
		
		Scanner scanner = new Scanner(System.in);
		BookService service = new BookService();
		
		while (true) {
			System.out.println("1. 전체 조회 2. 제목 검색 3. 책 등록 4. 책 삭제 5. 책 정보 변경 0. 종료");
			
			System.out.println("메뉴 선택> ");
			int selectNo = scanner.nextInt();
			
			if (selectNo == 1) {
				// 모든 책 정보를 조회해서 출력한다.
				service.selectAllBooks();
				
			} else if (selectNo == 2) {
				// 제목을 입력받아서 제목을 포함하고 있는 책 정보를 조회해서 출력한다.
				System.out.println("조회할 책 제목 입력> ");
				String keyword = scanner.next();
				service.selectBooksByTitle(keyword);
			
			} else if (selectNo == 3) {
				// 번호, 제목, 저자, 출판사, 가격, 출판일을 입력받아서 DB에 저장한다.
				System.out.println("저장할 책 번호 입력>");
				int no = scanner.nextInt();
				System.out.println("저장할 책 제목 입력>");
				String title = scanner.next();
				System.out.println("저장할 책 저자 입력>");
				String author = scanner.next();
				System.out.println("저장할 책 출판사 입력>");
				String publisher= scanner.next();
				System.out.println("저장할 책 가격 입력>");
				int price = scanner.nextInt();
				System.out.println("저장할 책 출판일 입력>");
				String pubdate = scanner.next();

				service.insertNewBook(no, title, author, publisher, price, pubdate);

			} else if (selectNo == 4) {
				// 책 번호를 입력받아서 책 정보를 삭제한다.
				System.out.println("삭제할 책 번호 입력> ");
				int no = scanner.nextInt();
				service.deleteBookByNo(no);
				
			} else if (selectNo == 5) {
				// 수정할 책 번호, 변경할 정보(제목, 저자, 출판사, 가격, 출판일)을 입력받아서
				// 책 번호에 해당하는 책 정보를 변경한다.
				
				System.out.println("변경할 책 번호 입력> ");
				int no = scanner.nextInt();
				System.out.println("변경할 책 제목 입력> ");
				String title = scanner.next();
				System.out.println("변경할 책 저자 입력> ");
				String author = scanner.next();
				System.out.println("변경할 책 출판사 입력> ");
				String publisher = scanner.next();
				System.out.println("변경할 책 가격 입력> ");
				int price = scanner.nextInt();
				System.out.println("변경할 책 출판일 입력> ");
				String pubdate = scanner.next();
				
				service.updateBook(no, title, author, publisher, price, pubdate);
				
			} else if (selectNo == 0) {
				System.out.println("종료");
				break;
			} 
		}
	}
}

'자바 > jdbc' 카테고리의 다른 글

utils  (0) 2019.06.10
demo3  (0) 2019.06.10
demo1  (0) 2019.06.10

+ Recent posts