Advertisement
Guest User

Untitled

a guest
May 24th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.91 KB | None | 0 0
  1. import org.apache.logging.log4j.LogManager;
  2. import org.apache.logging.log4j.Logger;
  3.  
  4. import java.sql.*;
  5. import java.util.ArrayList;
  6.  
  7. public class AuthorDAO {
  8.  
  9.     private static final String SQL_INSERT_AUTHOR =
  10.             "INSERT INTO author (first_name, last_name)" + " VALUES(?, ?);";
  11.     private static final String SQL_SELECT_BY_AUTHOR_ID =
  12.             "SELECT * FROM author" + " WHERE id = ?;";
  13.     private static final String SQL_SELECT_ALL_AUTHORS = "SELECT * FROM author;";
  14.     private static final String SQL_DELETE_AUTHOR =
  15.             "DELETE FROM author" + " WHERE id = ?;";
  16.     private static final String SQL_UPDATE_AUTHOR =
  17.             "UPDATE author" + " SET first_name = ?, last_name = ?"
  18.                     + " WHERE id = ?;";
  19.  
  20.     private static final Logger log = LogManager
  21.             .getLogger(AuthorDAO.class.getName());
  22.  
  23.     public void insertAuthor(String firstName, String lastName) {
  24.         log.traceEntry(firstName + " " + lastName);
  25.         try (Connection connection = ConnectionManager
  26.                 .getConnection(); PreparedStatement statement = connection
  27.                 .prepareStatement(SQL_INSERT_AUTHOR)) {
  28.             log.trace("Connection and Statement opening");
  29.  
  30.             statement.setString(1, firstName);
  31.             statement.setString(2, lastName);
  32.             statement.executeUpdate();
  33.  
  34.             log.trace("Connection and Statement closing");
  35.         } catch (SQLException e) {
  36.             log.catching(e);
  37.         }
  38.         log.traceExit();
  39.     }
  40.  
  41.     public Author findAuthorById(int authorId) {
  42.         log.traceEntry("" + authorId);
  43.         Author author = new Author();
  44.         ResultSet result;
  45.         try (Connection connection = ConnectionManager
  46.                 .getConnection(); PreparedStatement statement = connection
  47.                 .prepareStatement(SQL_SELECT_BY_AUTHOR_ID)) {
  48.             log.trace("Connection and Statement opening");
  49.  
  50.             statement.setInt(1, authorId);
  51.             result = statement.executeQuery();
  52.  
  53.             while (result.next()) {
  54.                 int id = result.getInt("id");
  55.                 String firstName = result.getString("first_name");
  56.                 String lastName = result.getString("last_name");
  57.  
  58.                 author.setId(id);
  59.                 author.setFirstName(firstName);
  60.                 author.setLastName(lastName);
  61.             }
  62.  
  63.             log.trace("Connection and Statement closing");
  64.         } catch (SQLException e) {
  65.             log.catching(e);
  66.         }
  67.         return log.traceExit(author);
  68.     }
  69.  
  70.     public ArrayList<Author> findAllAuthors() {
  71.         ArrayList<Author> list = new ArrayList<>();
  72.         Author author;
  73.         ResultSet result;
  74.         try (Connection connection = ConnectionManager
  75.                 .getConnection(); Statement statement = connection
  76.                 .createStatement()) {
  77.             log.trace("Connection and Statement opening");
  78.  
  79.             result = statement.executeQuery(SQL_SELECT_ALL_AUTHORS);
  80.  
  81.             while (result.next()) {
  82.                 int id = result.getInt("id");
  83.                 String firstName = result.getString("first_name");
  84.                 String lastName = result.getString("last_name");
  85.  
  86.                 author = new Author();
  87.                 author.setId(id);
  88.                 author.setFirstName(firstName);
  89.                 author.setLastName(lastName);
  90.                 list.add(author);
  91.             }
  92.  
  93.             log.trace("Connection and Statement closing");
  94.         } catch (SQLException e) {
  95.             log.catching(e);
  96.         }
  97.         return log.traceExit(list);
  98.     }
  99.  
  100.     public void deleteAuthor(int authorId) {
  101.         log.traceEntry("" + authorId);
  102.         try (Connection connection = ConnectionManager
  103.                 .getConnection(); PreparedStatement statement = connection
  104.                 .prepareStatement(SQL_DELETE_AUTHOR)) {
  105.             log.trace("Connection and Statement opening");
  106.  
  107.             statement.setInt(1, authorId);
  108.             statement.executeUpdate();
  109.  
  110.             log.trace("Connection and Statement closing");
  111.         } catch (SQLException e) {
  112.             log.catching(e);
  113.         }
  114.         log.traceExit();
  115.     }
  116.  
  117.     public void updateAuthor(int id, String firstName, String lastName) {
  118.         log.traceEntry(id + " " + firstName + " " + lastName);
  119.         try (Connection connection = ConnectionManager
  120.                 .getConnection(); PreparedStatement statement = connection
  121.                 .prepareStatement(SQL_UPDATE_AUTHOR)) {
  122.             log.trace("Connection and Statement opening");
  123.  
  124.             statement.setInt(1, id);
  125.             statement.setString(2, firstName);
  126.             statement.setString(3, lastName);
  127.             statement.executeUpdate();
  128.  
  129.             log.trace("Connection and Statement closing");
  130.         } catch (SQLException e) {
  131.             log.catching(e);
  132.         }
  133.         log.traceExit();
  134.     }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement