|
20 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21 | 21 | * THE SOFTWARE.
|
22 | 22 | */
|
| 23 | + |
23 | 24 | package com.iluwatar.dao;
|
24 | 25 |
|
| 26 | +import java.sql.Connection; |
| 27 | +import java.sql.SQLException; |
| 28 | +import java.sql.Statement; |
25 | 29 | import java.util.ArrayList;
|
26 | 30 | import java.util.List;
|
| 31 | +import java.util.stream.Stream; |
| 32 | + |
| 33 | +import javax.sql.DataSource; |
27 | 34 |
|
28 | 35 | import org.apache.log4j.Logger;
|
| 36 | +import org.h2.jdbcx.JdbcDataSource; |
29 | 37 |
|
30 | 38 | /**
|
31 |
| - * |
32 | 39 | * Data Access Object (DAO) is an object that provides an abstract interface to some type of
|
33 | 40 | * database or other persistence mechanism. By mapping application calls to the persistence layer,
|
34 | 41 | * DAO provide some specific data operations without exposing details of the database. This
|
35 | 42 | * isolation supports the Single responsibility principle. It separates what data accesses the
|
36 | 43 | * application needs, in terms of domain-specific objects and data types (the public interface of
|
37 | 44 | * the DAO), from how these needs can be satisfied with a specific DBMS.
|
38 |
| - * <p> |
39 |
| - * With the DAO pattern, we can use various method calls to retrieve/add/delete/update data without |
40 |
| - * directly interacting with the data. The below example demonstrates basic CRUD operations: select, |
41 |
| - * add, update, and delete. |
| 45 | + * |
| 46 | + * <p>With the DAO pattern, we can use various method calls to retrieve/add/delete/update data |
| 47 | + * without directly interacting with the data source. The below example demonstrates basic CRUD |
| 48 | + * operations: select, add, update, and delete. |
| 49 | + * |
42 | 50 | *
|
43 | 51 | */
|
44 | 52 | public class App {
|
45 |
| - |
| 53 | + private static final String DB_URL = "jdbc:h2:~/dao:customerdb"; |
46 | 54 | private static Logger log = Logger.getLogger(App.class);
|
47 |
| - |
| 55 | + |
48 | 56 | /**
|
49 | 57 | * Program entry point.
|
50 | 58 | *
|
51 | 59 | * @param args command line args.
|
| 60 | + * @throws Exception if any error occurs. |
52 | 61 | */
|
53 |
| - public static void main(final String[] args) { |
54 |
| - final CustomerDao customerDao = new CustomerDaoImpl(generateSampleCustomers()); |
55 |
| - log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); |
56 |
| - log.info("customerDao.getCusterById(2): " + customerDao.getCustomerById(2)); |
| 62 | + public static void main(final String[] args) throws Exception { |
| 63 | + final CustomerDao inMemoryDao = new InMemoryCustomerDao(); |
| 64 | + performOperationsUsing(inMemoryDao); |
| 65 | + |
| 66 | + final DataSource dataSource = createDataSource(); |
| 67 | + createSchema(dataSource); |
| 68 | + final CustomerDao dbDao = new DbCustomerDao(dataSource); |
| 69 | + performOperationsUsing(dbDao); |
| 70 | + deleteSchema(dataSource); |
| 71 | + } |
| 72 | + |
| 73 | + private static void deleteSchema(DataSource dataSource) throws SQLException { |
| 74 | + try (Connection connection = dataSource.getConnection(); |
| 75 | + Statement statement = connection.createStatement()) { |
| 76 | + statement.execute(CustomerSchemaSql.DELETE_SCHEMA_SQL); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static void createSchema(DataSource dataSource) throws SQLException { |
| 81 | + try (Connection connection = dataSource.getConnection(); |
| 82 | + Statement statement = connection.createStatement()) { |
| 83 | + statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private static DataSource createDataSource() { |
| 88 | + JdbcDataSource dataSource = new JdbcDataSource(); |
| 89 | + dataSource.setURL(DB_URL); |
| 90 | + return dataSource; |
| 91 | + } |
| 92 | + |
| 93 | + private static void performOperationsUsing(final CustomerDao customerDao) throws Exception { |
| 94 | + addCustomers(customerDao); |
| 95 | + log.info("customerDao.getAllCustomers(): "); |
| 96 | + try (Stream<Customer> customerStream = customerDao.getAll()) { |
| 97 | + customerStream.forEach((customer) -> log.info(customer)); |
| 98 | + } |
| 99 | + log.info("customerDao.getCustomerById(2): " + customerDao.getById(2)); |
57 | 100 | final Customer customer = new Customer(4, "Dan", "Danson");
|
58 |
| - customerDao.addCustomer(customer); |
59 |
| - log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); |
| 101 | + customerDao.add(customer); |
| 102 | + log.info("customerDao.getAllCustomers(): " + customerDao.getAll()); |
60 | 103 | customer.setFirstName("Daniel");
|
61 | 104 | customer.setLastName("Danielson");
|
62 |
| - customerDao.updateCustomer(customer); |
63 |
| - log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); |
64 |
| - customerDao.deleteCustomer(customer); |
65 |
| - log.info("customerDao.getAllCustomers(): " + customerDao.getAllCustomers()); |
| 105 | + customerDao.update(customer); |
| 106 | + log.info("customerDao.getAllCustomers(): "); |
| 107 | + try (Stream<Customer> customerStream = customerDao.getAll()) { |
| 108 | + customerStream.forEach((cust) -> log.info(cust)); |
| 109 | + } |
| 110 | + customerDao.delete(customer); |
| 111 | + log.info("customerDao.getAllCustomers(): " + customerDao.getAll()); |
| 112 | + } |
| 113 | + |
| 114 | + private static void addCustomers(CustomerDao customerDao) throws Exception { |
| 115 | + for (Customer customer : generateSampleCustomers()) { |
| 116 | + customerDao.add(customer); |
| 117 | + } |
66 | 118 | }
|
67 | 119 |
|
68 | 120 | /**
|
|
0 commit comments