private void addToLibrary(Book book) { int position = alphabet.indexOf(book.getTitle().charAt(0)); addToStorage(booksByTitle[position], book); position = alphabet.indexOf(book.getAuthor().charAt(0)); addToStorage(booksByAuthor[position], book); addToStorage(booksByGenre[book.getGenre().getId()], book); }
// 도서 등록 public int InsertData(Book bean) { System.out.println(bean.toString()); String sql = "insert into books(bookcode, name, volume, writer, publisher, pubdate, genre, image, bookstat, bookstory) "; sql += " values(bookseq.nextval, ?, ?, ?, ?, to_date(?, 'yyyy/MM/dd'), ?, ?, default, ?) "; PreparedStatement pstmt = null; int cnt = -99999; try { if (conn == null) { super.conn = super.getConn(); } conn.setAutoCommit(false); pstmt = super.conn.prepareStatement(sql); pstmt.setString(1, bean.getName()); pstmt.setInt(2, bean.getVolume()); pstmt.setString(3, bean.getWriter()); pstmt.setString(4, bean.getPublisher()); pstmt.setString(5, bean.getPubdate()); pstmt.setString(6, bean.getGenre()); // 책 이미지 이름은 책 제목과 책 볼륨의 문자열 조합이다. String image = bean.getName() + " " + bean.getVolume() + ".jpg"; pstmt.setString(7, image); // pstmt.setString(8, bean.getBookstat()); pstmt.setString(8, bean.getBookstory()); cnt = pstmt.executeUpdate(); conn.commit(); } catch (Exception e) { SQLException err = (SQLException) e; cnt = -err.getErrorCode(); e.printStackTrace(); try { conn.rollback(); } catch (Exception e2) { e2.printStackTrace(); } } finally { try { if (pstmt != null) { pstmt.close(); } super.closeConn(); } catch (Exception e2) { e2.printStackTrace(); } } return cnt; }
// 책 한건의 정보를 편집하기 위함 public int UpdateData(Book bean) { // System.out.println( "업뎃" + bean.toString() ); String sql = "update books set "; sql += " name=?, volume=?, writer=?, publisher=?, pubdate=to_date(?, 'yyyy/MM/dd'), "; sql += " genre=?, bookstory=? "; sql += " where bookcode = ? "; PreparedStatement pstmt = null; int cnt = -99999; try { if (conn == null) { super.conn = super.getConn(); } conn.setAutoCommit(false); pstmt = super.conn.prepareStatement(sql); pstmt.setString(1, bean.getName()); pstmt.setInt(2, bean.getVolume()); pstmt.setString(3, bean.getWriter()); pstmt.setString(4, bean.getPublisher()); pstmt.setString(5, bean.getPubdate()); pstmt.setString(6, bean.getGenre()); pstmt.setString(7, bean.getBookstory()); pstmt.setInt(8, bean.getBookcode()); cnt = pstmt.executeUpdate(); conn.commit(); } catch (Exception e) { SQLException err = (SQLException) e; cnt = -err.getErrorCode(); e.printStackTrace(); try { conn.rollback(); } catch (Exception e2) { e2.printStackTrace(); } } finally { try { if (pstmt != null) { pstmt.close(); } super.closeConn(); } catch (Exception e2) { e2.printStackTrace(); } } return cnt; }