Advertisement
Guest User

BookDAOImpl.java

a guest
Mar 18th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.11 KB | None | 0 0
  1. package com.apress.books.dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. import com.apress.books.model.Author;
  13. import com.apress.books.model.Book;
  14. import com.apress.books.model.Category;
  15.  
  16. public class BookDAOImpl {
  17.     static {
  18.         try {
  19.             Class.forName("com.mysql.jdbc.Driver");
  20.         } catch (ClassNotFoundException ex) {
  21.         }
  22.     }
  23.    
  24.     private Connection getConnection() throws SQLException {
  25.         return DriverManager.getConnection("jdbc:mysql://localhost/books", "user=root&password=password");
  26.     }
  27.    
  28.     private void closeConnection(Connection connection) {
  29.         if (connection == null)
  30.             return;
  31.         try {
  32.             connection.close();
  33.         } catch (SQLException ex) {
  34.         }
  35.     }
  36.    
  37.     public List<Book> findAllBooks() {
  38.         List<Book> result = new ArrayList<>();
  39.         List<Author> authorList = new ArrayList<>();
  40.        
  41.         String sql = "select * from book inner join author on book.id = author.book_id";
  42.        
  43.         Connection connection = null;
  44.         try {
  45.             connection = getConnection();
  46.             PreparedStatement statement = connection.prepareStatement(sql);
  47.             ResultSet resultSet = statement.executeQuery();
  48.             while (resultSet.next()) {
  49.                 com.apress.books.model.Book book = new Book();
  50.                 Author author = new Author();
  51.                 book.setId(resultSet.getLong("book_title"));
  52.                 book.setBookTitle(resultSet.getString("book_title"));
  53.                 book.setCategoryId(resultSet.getLong("category_id"));
  54.                 author.setBookId(resultSet.getLong("book_id"));
  55.                 author.setFirstName(resultSet.getString("first_name"));
  56.                 author.setLastName(resultSet.getString("last_name"));
  57.                 authorList.add(author);
  58.                 book.setAuthors(authorList);
  59.                 book.setPublisherName(resultSet.getString("publisher"));
  60.                 result.add(book);
  61.             }
  62.         } catch (SQLException ex) {
  63.             ex.printStackTrace();
  64.         } finally {
  65.             closeConnection(connection);
  66.         }
  67.         return result;
  68.     }
  69.    
  70.     public List<Book> searchBooksByKeyword(String keyWord) {
  71.         List<Book> result = new ArrayList<>();
  72.         List<com.apress.books.model.Author> authorList = new ArrayList<>();
  73.        
  74.         String sql = "select * from book inner join author on book.id = author.book_id"
  75.                 + " where book_title like %"
  76.                 + keyWord.trim()
  77.                 + "%'"
  78.                 + " or last_name like '%" + keyWord.trim() + "%'";
  79.        
  80.         Connection connection = null;
  81.         try {
  82.             connection = getConnection();
  83.             PreparedStatement statement = connection.prepareStatement(sql);
  84.             ResultSet resultSet = statement.executeQuery();
  85.             while (resultSet.next()) {
  86.                 com.apress.books.model.Book book = new Book();
  87.                 com.apress.books.model.Author author = new Author();
  88.                 book.setId(resultSet.getLong("id"));
  89.                 book.setBookTitle(resultSet.getString("book_title"));
  90.                 book.setPublisherName(resultSet.getString("publisher"));
  91.                 author.setFirstName(resultSet.getString("first_name"));
  92.                 author.setLastName(resultSet.getString("last_name"));
  93.                 author.setBookId(resultSet.getLong("book_id"));
  94.                 authorList.add(author);
  95.                 book.setAuthors(authorList);
  96.                 result.add(book);
  97.             }
  98.         } catch (SQLException ex) {
  99.             ex.printStackTrace();
  100.         } finally {
  101.             closeConnection(connection);
  102.         }
  103.        
  104.         return result;
  105.     }
  106.    
  107.     public List<Category> findAllCategories() {
  108.         List<com.apress.books.model.Category> result = new ArrayList<>();
  109.         String sql = "select * from category";
  110.        
  111.         Connection connection = null;
  112.         try {
  113.             connection = getConnection();
  114.             PreparedStatement statement = connection.prepareStatement(sql);
  115.             ResultSet resultSet = statement.executeQuery();
  116.             while (resultSet.next()) {
  117.                 com.apress.books.model.Category category = new Category();
  118.                 category.setId(resultSet.getLong("id"));
  119.                 category.setCategoryDescription(resultSet
  120.                         .getString("category_description"));
  121.                 result.add(category);
  122.             }
  123.         } catch (SQLException ex) {
  124.             ex.printStackTrace();
  125.         } finally {
  126.             closeConnection(connection);
  127.         }
  128.         return result;
  129.     }
  130.    
  131.     public void insert(Book book) {
  132.     }
  133.    
  134.     public void update(Book book) {
  135.     }
  136.    
  137.     public void delete(Long bookId) {
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement