View Javadoc
1   package ejava.examples.daoex.dao;
2   
3   import java.lang.reflect.Field;
4   import java.sql.Connection;
5   import java.sql.PreparedStatement;
6   import java.sql.ResultSet;
7   import java.sql.SQLException;
8   import java.sql.Statement;
9   import java.util.List;
10  
11  import javax.persistence.PersistenceException;
12  
13  import ejava.examples.daoex.bo.Book;
14  
15  public class JDBCBookDAOImpl implements BookDAO {
16  	private Connection connection;
17  	
18  	public void setConnection(Connection connection) {
19  		this.connection = connection;
20  	}
21  
22  	@Override
23  	public Book create(Book book) throws PersistenceException {
24  		try (PreparedStatement insertStatement=getInsertPreparedStatement(connection, book);
25  		     PreparedStatement idStatement=getIdentityStatement(connection)){
26  			insertStatement.execute();
27  			
28              try (ResultSet rs = idStatement.executeQuery()) {
29                  if (rs.next()) {
30                      Field id = Book.class.getDeclaredField("id");
31                      id.setAccessible(true);
32                      id.set(book, rs.getLong(1));
33                  } else {
34                      throw new PersistenceException("no identity returned from database");
35                  }                
36              } catch (NoSuchFieldException ex) {
37                  throw new PersistenceException("Error locating id field", ex);
38              } catch (IllegalAccessException ex) {
39                  throw new PersistenceException("Access error setting id", ex);
40              }
41  			
42  			return book;
43  		} catch (SQLException ex) { 
44  			throw new PersistenceException("SQL error creating book", ex);
45  		}
46  	}
47  	
48  	private PreparedStatement getInsertPreparedStatement(Connection c, Book book) throws SQLException {
49          PreparedStatement statement=connection.prepareStatement(
50                  "insert into JPADAO_BOOK (ID, DESCRIPTION, PAGES, TITLE) " +
51                  "values (null, ?, ?, ?)");
52          statement.setString(1, book.getDescription());
53          statement.setInt(2, book.getPages());
54          statement.setString(3, book.getTitle());
55          return statement;
56  	}
57  	
58  	private PreparedStatement getIdentityStatement(Connection c) throws SQLException {
59          PreparedStatement statement = connection.prepareStatement("call identity()");
60          return statement;
61  	}
62  
63  	@Override
64  	public Book update(Book book) throws PersistenceException {
65  		// TODO Auto-generated method stub
66  		return null;
67  	}
68  
69  	@Override
70  	public Book get(long id) throws PersistenceException {
71  		// TODO Auto-generated method stub
72  		return null;
73  	}
74  
75  	@Override
76  	public void remove(Book book) throws PersistenceException {
77  		// TODO Auto-generated method stub
78  
79  	}
80  
81  	@Override
82  	public List<Book> findAll(int start, int count) throws PersistenceException {
83  		// TODO Auto-generated method stub
84  		return null;
85  	}
86  
87  }