JDBCBookDAOImpl.java

  1. package ejava.examples.daoex.dao;

  2. import java.lang.reflect.Field;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.List;

  9. import javax.persistence.PersistenceException;

  10. import ejava.examples.daoex.bo.Book;

  11. public class JDBCBookDAOImpl implements BookDAO {
  12.     private Connection connection;
  13.    
  14.     public void setConnection(Connection connection) {
  15.         this.connection = connection;
  16.     }

  17.     @Override
  18.     public Book create(Book book) throws PersistenceException {
  19.         try (PreparedStatement insertStatement=getInsertPreparedStatement(connection, book);
  20.              PreparedStatement idStatement=getIdentityStatement(connection)){
  21.             insertStatement.execute();
  22.            
  23.             try (ResultSet rs = idStatement.executeQuery()) {
  24.                 if (rs.next()) {
  25.                     Field id = Book.class.getDeclaredField("id");
  26.                     id.setAccessible(true);
  27.                     id.set(book, rs.getLong(1));
  28.                 } else {
  29.                     throw new PersistenceException("no identity returned from database");
  30.                 }                
  31.             } catch (NoSuchFieldException ex) {
  32.                 throw new PersistenceException("Error locating id field", ex);
  33.             } catch (IllegalAccessException ex) {
  34.                 throw new PersistenceException("Access error setting id", ex);
  35.             }
  36.            
  37.             return book;
  38.         } catch (SQLException ex) {
  39.             throw new PersistenceException("SQL error creating book", ex);
  40.         }
  41.     }
  42.    
  43.     private PreparedStatement getInsertPreparedStatement(Connection c, Book book) throws SQLException {
  44.         PreparedStatement statement=connection.prepareStatement(
  45.                 "insert into JPADAO_BOOK (ID, DESCRIPTION, PAGES, TITLE) " +
  46.                 "values (null, ?, ?, ?)");
  47.         statement.setString(1, book.getDescription());
  48.         statement.setInt(2, book.getPages());
  49.         statement.setString(3, book.getTitle());
  50.         return statement;
  51.     }
  52.    
  53.     private PreparedStatement getIdentityStatement(Connection c) throws SQLException {
  54.         PreparedStatement statement = connection.prepareStatement("call identity()");
  55.         return statement;
  56.     }

  57.     @Override
  58.     public Book update(Book book) throws PersistenceException {
  59.         // TODO Auto-generated method stub
  60.         return null;
  61.     }

  62.     @Override
  63.     public Book get(long id) throws PersistenceException {
  64.         // TODO Auto-generated method stub
  65.         return null;
  66.     }

  67.     @Override
  68.     public void remove(Book book) throws PersistenceException {
  69.         // TODO Auto-generated method stub

  70.     }

  71.     @Override
  72.     public List<Book> findAll(int start, int count) throws PersistenceException {
  73.         // TODO Auto-generated method stub
  74.         return null;
  75.     }

  76. }