Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- import java.sql.*;
- import java.util.ArrayList;
- public class AuthorDAO {
- private static final String SQL_INSERT_AUTHOR =
- "INSERT INTO author (first_name, last_name)" + " VALUES(?, ?);";
- private static final String SQL_SELECT_BY_AUTHOR_ID =
- "SELECT * FROM author" + " WHERE id = ?;";
- private static final String SQL_SELECT_ALL_AUTHORS = "SELECT * FROM author;";
- private static final String SQL_DELETE_AUTHOR =
- "DELETE FROM author" + " WHERE id = ?;";
- private static final String SQL_UPDATE_AUTHOR =
- "UPDATE author" + " SET first_name = ?, last_name = ?"
- + " WHERE id = ?;";
- private static final Logger log = LogManager
- .getLogger(AuthorDAO.class.getName());
- public void insertAuthor(String firstName, String lastName) {
- log.traceEntry(firstName + " " + lastName);
- try (Connection connection = ConnectionManager
- .getConnection(); PreparedStatement statement = connection
- .prepareStatement(SQL_INSERT_AUTHOR)) {
- log.trace("Connection and Statement opening");
- statement.setString(1, firstName);
- statement.setString(2, lastName);
- statement.executeUpdate();
- log.trace("Connection and Statement closing");
- } catch (SQLException e) {
- log.catching(e);
- }
- log.traceExit();
- }
- public Author findAuthorById(int authorId) {
- log.traceEntry("" + authorId);
- Author author = new Author();
- ResultSet result;
- try (Connection connection = ConnectionManager
- .getConnection(); PreparedStatement statement = connection
- .prepareStatement(SQL_SELECT_BY_AUTHOR_ID)) {
- log.trace("Connection and Statement opening");
- statement.setInt(1, authorId);
- result = statement.executeQuery();
- while (result.next()) {
- int id = result.getInt("id");
- String firstName = result.getString("first_name");
- String lastName = result.getString("last_name");
- author.setId(id);
- author.setFirstName(firstName);
- author.setLastName(lastName);
- }
- log.trace("Connection and Statement closing");
- } catch (SQLException e) {
- log.catching(e);
- }
- return log.traceExit(author);
- }
- public ArrayList<Author> findAllAuthors() {
- ArrayList<Author> list = new ArrayList<>();
- Author author;
- ResultSet result;
- try (Connection connection = ConnectionManager
- .getConnection(); Statement statement = connection
- .createStatement()) {
- log.trace("Connection and Statement opening");
- result = statement.executeQuery(SQL_SELECT_ALL_AUTHORS);
- while (result.next()) {
- int id = result.getInt("id");
- String firstName = result.getString("first_name");
- String lastName = result.getString("last_name");
- author = new Author();
- author.setId(id);
- author.setFirstName(firstName);
- author.setLastName(lastName);
- list.add(author);
- }
- log.trace("Connection and Statement closing");
- } catch (SQLException e) {
- log.catching(e);
- }
- return log.traceExit(list);
- }
- public void deleteAuthor(int authorId) {
- log.traceEntry("" + authorId);
- try (Connection connection = ConnectionManager
- .getConnection(); PreparedStatement statement = connection
- .prepareStatement(SQL_DELETE_AUTHOR)) {
- log.trace("Connection and Statement opening");
- statement.setInt(1, authorId);
- statement.executeUpdate();
- log.trace("Connection and Statement closing");
- } catch (SQLException e) {
- log.catching(e);
- }
- log.traceExit();
- }
- public void updateAuthor(int id, String firstName, String lastName) {
- log.traceEntry(id + " " + firstName + " " + lastName);
- try (Connection connection = ConnectionManager
- .getConnection(); PreparedStatement statement = connection
- .prepareStatement(SQL_UPDATE_AUTHOR)) {
- log.trace("Connection and Statement opening");
- statement.setInt(1, id);
- statement.setString(2, firstName);
- statement.setString(3, lastName);
- statement.executeUpdate();
- log.trace("Connection and Statement closing");
- } catch (SQLException e) {
- log.catching(e);
- }
- log.traceExit();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement