/**
  * *********************************AUTHOR
  * SECTION******************************************************
  */
 public void createAuthor(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("Author Name cannot be empty or more than 45 Chars");
     } else {
       AuthorDAO adao = new AuthorDAO(conn);
       adao.create(author);
       conn.commit();
     }
   } catch (Exception e) {
     e.printStackTrace();
     conn.rollback();
   } finally {
     conn.close();
   }
 }
Esempio n. 2
0
  private void addAuthor() {
    System.out.println("Enter new author's name: [N/A to cancel]");
    String name = getInputString();
    if (!name.equals("N/A")) {
      Author author = new Author();
      author.setAuthorName(name);
      try {
        Connection conn = getConnection();
        try {
          AuthorDAO authorDAO = new AuthorDAO(conn);
          authorDAO.create(author);
          conn.commit();
          conn.close();
        } catch (Exception e) {
          conn.rollback();
        }

      } catch (Exception e) {
        System.err.println("Error while connecting to Database");
        e.printStackTrace();
      }
    }
  }