BookVO
package jdbcExam; public class BookVO { private int bookId; private String bookTitle; private int page; private String author; private String content; private String redDate; public int getBookId() { return bookId; } public void setBookId(int bookId) { this.bookId = bookId; } public String getBookTitle() { return bookTitle; } public void setBookTitle(String bookTitle) { this.bookTitle = bookTitle; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getRedDate() { return redDate; } public void setRedDate(String redDate) { this.redDate = redDate; } @Override public String toString() { return "BookVO [bookId=" + bookId + ", bookTitle=" + bookTitle + ", page=" + page + ", author=" + author + ", content=" + content + ", redDate=" + redDate + "]"; }
} |
BookDAO
package jdbcExam; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; public class BookDAO { public List<BookVO> getBookList(){ List<BookVO> bookList = new ArrayList<>(); //1.선언 Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try{ //3. 접속 conn = DBUtil.getConnection(); //4. 쿼리작성 String sql = "select * from book order by book_id"; ps = conn.prepareStatement(sql); //5. 실행 rs = ps.executeQuery(); //6. 결과값을 꺼낸다.
while(rs.next()){ BookVO resultVO = new BookVO();//값을 저장할 가방을 생성 resultVO.setBookId(rs.getInt(1)); resultVO.setBookTitle(rs.getString("book_title")); resultVO.setPage(rs.getInt(3)); resultVO.setAuthor(rs.getString(4)); resultVO.setContent(rs.getString(5)); resultVO.setRedDate(rs.getString(6));
bookList.add(resultVO); }
}catch(Exception e){ e.printStackTrace(); }finally { //2.닫는다. DBUtil.close(conn, ps, rs); } return bookList; }
public BookVO getBook(int bookId){ BookVO resultVO = null; //1.선언 Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try{ //3. 접속 conn = DBUtil.getConnection(); //4. 쿼리작성 String sql = "select * from book where book_id=?"; ps = conn.prepareStatement(sql); ps.setInt(1, bookId); //5. 실행 rs = ps.executeQuery(); //6. 결과값을 꺼낸다. if(rs.next()){ resultVO = new BookVO(); //값을 저장할 가방을 생성 resultVO.setBookId(rs.getInt(1)); resultVO.setBookTitle(rs.getString("book_title")); resultVO.setPage(rs.getInt(3)); resultVO.setAuthor(rs.getString(4)); resultVO.setContent(rs.getString(5)); resultVO.setRedDate(rs.getString(6)); }
}catch(Exception e){ e.printStackTrace(); }finally { //2.닫는다. DBUtil.close(conn, ps, rs); } return resultVO; } public int addBook(BookVO book) { int insertCount = 0; Connection conn = null; PreparedStatement ps = null; try { conn = DBUtil.getConnection(); // 5. 쿼리작성 String sql = "insert into book values(book_seq.nextval,?,?,?,?,sysdate)"; ps = conn.prepareStatement(sql); ps.setString(1, book.getBookTitle()); ps.setInt(2, book.getPage()); ps.setString(3, book.getAuthor()); ps.setString(4, book.getContent()); insertCount = ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { // 2. 닫는다. DBUtil.close(conn, ps); } return insertCount; } public void delBook(int bookId) { // 1. 선언 (필요한 객체 선언) Connection conn = null; PreparedStatement ps = null; try { conn = DBUtil.getConnection(); // 5. 쿼리작성 String sql = "delete book where book_id=?"; ps = conn.prepareStatement(sql); ps.setInt(1, bookId); // 6. 실행 ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { // 2. 닫는다. DBUtil.close(conn, ps); } } } |
'프로그래밍 > 자바' 카테고리의 다른 글
member (0) | 2016.01.19 |
---|---|
BookDAOTest (0) | 2016.01.14 |
DBUtil 이용하기 (0) | 2016.01.14 |
BookDAO (0) | 2016.01.14 |
수업중 기본명령 (0) | 2016.01.13 |