private void editPublisher(Publisher publisher) { try { Connection conn = getConnection(); System.out.println("Enter Publisher's new name:"); String name = getInputString(); System.out.println("Enter Publisher's new address:"); String address = getInputString(); System.out.println("Enter Publisher's new phone:"); String phone = getInputString(); try { PublisherDAO pubDAO = new PublisherDAO(conn); publisher.setPublisherName(name); publisher.setPublisherAddress(address); publisher.setPublisherPhone(phone); pubDAO.update(publisher); 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(); } }
// gather the necessary information to add a publisher private void addPublisher() { try { Connection conn = getConnection(); try { PublisherDAO pubDAO = new PublisherDAO(conn); Publisher toAdd = new Publisher(); System.out.println("What is the Publisher's name?"); String pubName = getInputString(); System.out.println("What is the Publisher's address?"); String pubAddress = getInputString(); System.out.println("What is the Publisher's phone number?"); String pubPhone = getInputString(); toAdd.setPublisherName(pubName); toAdd.setPublisherAddress(pubAddress); toAdd.setPublisherPhone(pubPhone); pubDAO.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 AddPublisher() throws SQLException { boolean exit_Add = false; String name, Address, phone; Set<Publisher> s = as.listpublisher3(); Publisher p = new Publisher(); do { System.out.println("Enter the Publisher Name:"); name = sc.nextLine(); System.out.println("Enter the Publisher Address:"); Address = sc.nextLine(); System.out.println("Enter the Publisher Phone:"); phone = sc.nextLine(); p.setPublisherName(name); p.setPublisherAddress(Address); p.setPublisherPhone(phone); if (name != null && name.length() > 0 && name.length() <= 45) { if (!s.contains(p)) { try { as.createPublisher(p); System.out.println("Operation successful!"); exit_Add = true; } catch (Exception e) { // TODO Auto-generated catch block System.out.println(e.getMessage()); } } else { System.out.println("Error: This publisher already exists."); } } else { System.out.println(); System.out.println("Error: Name cannot be empty or more than 45 characters"); } if (!exit_Add) { System.out.println(); System.out.println(); System.out.println("Press ' q ' to return or any other key to continue"); String option = (sc.nextLine()); if (option.length() > 0) { switch (option.charAt(0)) { case 'q': exit_Add = true; break; default: exit_Add = false; break; } } } } while (!exit_Add); }
private void Updatepublisher2(int id) throws SQLException { boolean exit_Add = false; String name, Address, phone; Set<Publisher> s = as.listpublisher3(); Publisher p = new Publisher(); do { System.out.println("Enter the new Publisher Name:"); name = sc.nextLine(); System.out.println("Enter the new Publisher Address:"); Address = sc.nextLine(); System.out.println("Enter the new Publisher Phone:"); phone = sc.nextLine(); p.setPublisherId(id); p.setPublisherName(name); p.setPublisherAddress(Address); p.setPublisherPhone(phone); if (name != null && name.length() > 0 && name.length() <= 45) { if (!s.contains(p)) { as.updatePublisher(p); System.out.println("Update successful"); exit_Add = true; } else { // If the information entered are identical to the original System.out.println("Update successful"); } } else { System.out.println(); System.out.println("Error: Name cannot be empty or more than 45 caracter"); } if (!exit_Add) { System.out.println(); System.out.println(); System.out.println("Press ' q ' to return or any other key to continue"); String option = (sc.nextLine()); if (option.length() > 0) { switch (option.charAt(0)) { case 'q': exit_Add = true; break; default: exit_Add = false; break; } } } } while (!exit_Add); }