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
+ */
1
23
package com .iluwatar .dao ;
2
24
3
25
import java .sql .Connection ;
4
- import java .sql .DriverManager ;
5
26
import java .sql .PreparedStatement ;
6
27
import java .sql .ResultSet ;
7
28
import java .sql .SQLException ;
11
32
import java .util .stream .Stream ;
12
33
import java .util .stream .StreamSupport ;
13
34
35
+ import javax .sql .DataSource ;
36
+
37
+ /**
38
+ *
39
+ *
40
+ */
14
41
public class DBCustomerDao implements CustomerDao {
15
42
16
- private String dbUrl ;
43
+ private final DataSource dataSource ;
17
44
18
- public DBCustomerDao (String dbUrl ) {
19
- this .dbUrl = dbUrl ;
45
+ public DBCustomerDao (DataSource dataSource ) {
46
+ this .dataSource = dataSource ;
20
47
}
21
48
22
49
@ Override
23
- public Stream <Customer > getAll () {
50
+ public Stream <Customer > getAll () throws Exception {
24
51
25
52
Connection connection ;
26
53
try {
@@ -41,14 +68,16 @@ public boolean tryAdvance(Consumer<? super Customer> action) {
41
68
e .printStackTrace ();
42
69
return false ;
43
70
}
44
-
45
71
}}, false ).onClose (() -> mutedClose (connection ));
46
72
} catch (SQLException e ) {
47
- e .printStackTrace ();
48
- return null ;
73
+ throw new Exception (e .getMessage (), e );
49
74
}
50
75
}
51
76
77
+ private Connection getConnection () throws SQLException {
78
+ return dataSource .getConnection ();
79
+ }
80
+
52
81
private void mutedClose (Connection connection ) {
53
82
try {
54
83
connection .close ();
@@ -64,22 +93,23 @@ private Customer createCustomer(ResultSet resultSet) throws SQLException {
64
93
}
65
94
66
95
@ Override
67
- public Customer getById (int id ) {
96
+ public Customer getById (int id ) throws Exception {
68
97
try (Connection connection = getConnection ();
69
98
PreparedStatement statement = connection .prepareStatement ("SELECT * FROM CUSTOMERS WHERE ID = ?" )) {
70
99
statement .setInt (1 , id );
71
100
ResultSet resultSet = statement .executeQuery ();
72
101
if (resultSet .next ()) {
73
102
return createCustomer (resultSet );
103
+ } else {
104
+ return null ;
74
105
}
75
106
} catch (SQLException ex ) {
76
- ex .printStackTrace ( );
107
+ throw new Exception ( ex .getMessage (), ex );
77
108
}
78
- return null ;
79
109
}
80
110
81
111
@ Override
82
- public boolean add (Customer customer ) {
112
+ public boolean add (Customer customer ) throws Exception {
83
113
if (getById (customer .getId ()) != null ) {
84
114
return false ;
85
115
}
@@ -92,39 +122,31 @@ public boolean add(Customer customer) {
92
122
statement .execute ();
93
123
return true ;
94
124
} catch (SQLException ex ) {
95
- ex .printStackTrace ();
96
- return false ;
125
+ throw new Exception (ex .getMessage (), ex );
97
126
}
98
127
}
99
128
100
129
@ Override
101
- public boolean update (Customer customer ) {
130
+ public boolean update (Customer customer ) throws Exception {
102
131
try (Connection connection = getConnection ();
103
132
PreparedStatement statement = connection .prepareStatement ("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?" )) {
104
133
statement .setString (1 , customer .getFirstName ());
105
134
statement .setString (2 , customer .getLastName ());
106
135
statement .setInt (3 , customer .getId ());
107
136
return statement .executeUpdate () > 0 ;
108
137
} catch (SQLException ex ) {
109
- ex .printStackTrace ();
110
- return false ;
138
+ throw new Exception (ex .getMessage (), ex );
111
139
}
112
140
}
113
141
114
142
@ Override
115
- public boolean delete (Customer customer ) {
143
+ public boolean delete (Customer customer ) throws Exception {
116
144
try (Connection connection = getConnection ();
117
145
PreparedStatement statement = connection .prepareStatement ("DELETE FROM CUSTOMERS WHERE ID = ?" )) {
118
146
statement .setInt (1 , customer .getId ());
119
147
return statement .executeUpdate () > 0 ;
120
148
} catch (SQLException ex ) {
121
- ex .printStackTrace ();
122
- return false ;
149
+ throw new Exception (ex .getMessage (), ex );
123
150
}
124
151
}
125
-
126
- private Connection getConnection () throws SQLException {
127
- return DriverManager .getConnection (dbUrl );
128
- }
129
-
130
152
}
0 commit comments