1+ /**
2+ * The MIT License
3+ * Copyright (c) 2014 Ilkka Seppälä
4+ *
5+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6+ * of this software and associated documentation files (the "Software"), to deal
7+ * in the Software without restriction, including without limitation the rights
8+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+ * copies of the Software, and to permit persons to whom the Software is
10+ * furnished to do so, subject to the following conditions:
11+ *
12+ * The above copyright notice and this permission notice shall be included in
13+ * all copies or substantial portions of the Software.
14+ *
15+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+ * THE SOFTWARE.
22+ */
123package com .iluwatar .dao ;
224
325import java .sql .Connection ;
4- import java .sql .DriverManager ;
526import java .sql .PreparedStatement ;
627import java .sql .ResultSet ;
728import java .sql .SQLException ;
1132import java .util .stream .Stream ;
1233import java .util .stream .StreamSupport ;
1334
35+ import javax .sql .DataSource ;
36+
37+ /**
38+ *
39+ *
40+ */
1441public class DBCustomerDao implements CustomerDao {
1542
16- private String dbUrl ;
43+ private final DataSource dataSource ;
1744
18- public DBCustomerDao (String dbUrl ) {
19- this .dbUrl = dbUrl ;
45+ public DBCustomerDao (DataSource dataSource ) {
46+ this .dataSource = dataSource ;
2047 }
2148
2249 @ Override
23- public Stream <Customer > getAll () {
50+ public Stream <Customer > getAll () throws Exception {
2451
2552 Connection connection ;
2653 try {
@@ -41,14 +68,16 @@ public boolean tryAdvance(Consumer<? super Customer> action) {
4168 e .printStackTrace ();
4269 return false ;
4370 }
44-
4571 }}, false ).onClose (() -> mutedClose (connection ));
4672 } catch (SQLException e ) {
47- e .printStackTrace ();
48- return null ;
73+ throw new Exception (e .getMessage (), e );
4974 }
5075 }
5176
77+ private Connection getConnection () throws SQLException {
78+ return dataSource .getConnection ();
79+ }
80+
5281 private void mutedClose (Connection connection ) {
5382 try {
5483 connection .close ();
@@ -64,22 +93,23 @@ private Customer createCustomer(ResultSet resultSet) throws SQLException {
6493 }
6594
6695 @ Override
67- public Customer getById (int id ) {
96+ public Customer getById (int id ) throws Exception {
6897 try (Connection connection = getConnection ();
6998 PreparedStatement statement = connection .prepareStatement ("SELECT * FROM CUSTOMERS WHERE ID = ?" )) {
7099 statement .setInt (1 , id );
71100 ResultSet resultSet = statement .executeQuery ();
72101 if (resultSet .next ()) {
73102 return createCustomer (resultSet );
103+ } else {
104+ return null ;
74105 }
75106 } catch (SQLException ex ) {
76- ex .printStackTrace ( );
107+ throw new Exception ( ex .getMessage (), ex );
77108 }
78- return null ;
79109 }
80110
81111 @ Override
82- public boolean add (Customer customer ) {
112+ public boolean add (Customer customer ) throws Exception {
83113 if (getById (customer .getId ()) != null ) {
84114 return false ;
85115 }
@@ -92,39 +122,31 @@ public boolean add(Customer customer) {
92122 statement .execute ();
93123 return true ;
94124 } catch (SQLException ex ) {
95- ex .printStackTrace ();
96- return false ;
125+ throw new Exception (ex .getMessage (), ex );
97126 }
98127 }
99128
100129 @ Override
101- public boolean update (Customer customer ) {
130+ public boolean update (Customer customer ) throws Exception {
102131 try (Connection connection = getConnection ();
103132 PreparedStatement statement = connection .prepareStatement ("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?" )) {
104133 statement .setString (1 , customer .getFirstName ());
105134 statement .setString (2 , customer .getLastName ());
106135 statement .setInt (3 , customer .getId ());
107136 return statement .executeUpdate () > 0 ;
108137 } catch (SQLException ex ) {
109- ex .printStackTrace ();
110- return false ;
138+ throw new Exception (ex .getMessage (), ex );
111139 }
112140 }
113141
114142 @ Override
115- public boolean delete (Customer customer ) {
143+ public boolean delete (Customer customer ) throws Exception {
116144 try (Connection connection = getConnection ();
117145 PreparedStatement statement = connection .prepareStatement ("DELETE FROM CUSTOMERS WHERE ID = ?" )) {
118146 statement .setInt (1 , customer .getId ());
119147 return statement .executeUpdate () > 0 ;
120148 } catch (SQLException ex ) {
121- ex .printStackTrace ();
122- return false ;
149+ throw new Exception (ex .getMessage (), ex );
123150 }
124151 }
125-
126- private Connection getConnection () throws SQLException {
127- return DriverManager .getConnection (dbUrl );
128- }
129-
130152}
0 commit comments