private void addLibrary() { try { Connection conn = getConnection(); try { LibraryBranchDAO libDAO = new LibraryBranchDAO(conn); LibraryBranch toAdd = new LibraryBranch(); System.out.println("What is the new branch's name?"); String libName = getInputString(); System.out.println("What is the Publisher's address?"); String libAddress = getInputString(); toAdd.setBranchName(libName); toAdd.setAddress(libAddress); libDAO.create(toAdd); conn.commit(); conn.close(); } catch (Exception e) { conn.rollback(); conn.close(); e.printStackTrace(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
private void editBranch(LibraryBranch branch) { try { Connection conn = getConnection(); System.out.println("Enter Branch's new name:"); String name = getInputString(); System.out.println("Enter Publisher's new address:"); String address = getInputString(); try { LibraryBranchDAO libDAO = new LibraryBranchDAO(conn); branch.setBranchName(name); branch.setAddress(address); libDAO.update(branch); conn.commit(); conn.close(); } catch (Exception e) { conn.rollback(); conn.close(); } } catch (Exception e) { // TODO Auto-generated catch block System.err.println("Error while connecting to database"); e.printStackTrace(); } }
private LibraryBranch showAllBranches() { LibraryBranch branch = null; try { Connection conn = getConnection(); LibraryBranchDAO libDAO = new LibraryBranchDAO(conn); List<LibraryBranch> allBranches = libDAO.readAll(); ArrayList<String> actions = new ArrayList<String>(); actions.add("Cancel"); int choice = getChoiceNumber(allBranches, actions); if (choice > 0 && choice <= allBranches.size()) { branch = allBranches.get(choice - 1); } conn.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return branch; }
private void deleteBranch(LibraryBranch branch) { try { Connection conn = getConnection(); try { LibraryBranchDAO libDAO = new LibraryBranchDAO(conn); BookLoanDAO loanDAO = new BookLoanDAO(conn); List<BookLoan> branchLoans = (List<BookLoan>) loanDAO.read( "SELECT * FROM tbl_book_loans WHERE branchId = ? AND dateIn IS NULL", new Object[] {branch.getBranchId()}); if (branchLoans.size() > 0) { ArrayList<String> answers = new ArrayList<String>(); answers.add("No, nevermind"); answers.add("Yes, delete this branch"); System.out.println( "This branch has " + branchLoans.size() + " books that are not returned"); System.out.println("Are you sure you still want to delete? These books will be lost"); displayOptions(answers); int in = getInputInt(1, 2); if (in == 1) { return; } } libDAO.delete(branch); conn.commit(); conn.close(); } catch (Exception e) { conn.rollback(); conn.close(); } } catch (Exception e) { // TODO Auto-generated catch block System.err.println("Error while connecting to database"); e.printStackTrace(); } }