예제 #1
0
  private void deleteAuthor(Author author) {
    try {
      Connection conn = getConnection();
      try {
        AuthorDAO authorDAO = new AuthorDAO(conn);
        BookDAO bookDAO = new BookDAO(conn);
        List<Book> authorBooks =
            (List<Book>)
                bookDAO.read(
                    "SELECT * FROM tbl_book WHERE tbl_book.bookId IN (SELECT tbl_book_authors.bookId FROM tbl_book_authors WHERE authorId = ?)",
                    new Object[] {author.getAuthorId()});
        if (authorBooks.size() > 0) {
          ArrayList<String> answers = new ArrayList<String>();
          answers.add("No, nevermind");
          answers.add("Yes, delete this author");
          System.out.println(
              author + " has (co)authored " + authorBooks.size() + " books in our records");
          System.out.println(
              "Are you sure you still want to delete? Some books may remain with no author");
          displayOptions(answers);
          int in = getInputInt(1, 2);
          if (in == 1) {
            return;
          }
        }
        System.out.println("here");

        authorDAO.delete(author);
        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();
    }
  }
 /** *************************************************************************************** */
 public void DeleteAuthor(Author author) throws Exception {
   ConnectionUtil c = new ConnectionUtil();
   Connection conn = c.createConnection();
   try {
     if (author == null
         || author.getAuthorName() == null
         || author.getAuthorName().length() == 0
         || author.getAuthorName().length() > 45) {
       throw new Exception("The Author cannot be null");
     } else {
       AuthorDAO adao = new AuthorDAO(conn);
       adao.delete(author);
       conn.commit();
     }
   } catch (Exception e) {
     e.printStackTrace();
     conn.rollback();
   } finally {
     conn.close();
   }
 }