2323
2424package com .iluwatar .dao ;
2525
26- import org .slf4j .Logger ;
27- import org .slf4j .LoggerFactory ;
28-
2926import java .sql .Connection ;
3027import java .sql .PreparedStatement ;
3128import java .sql .ResultSet ;
3633import java .util .function .Consumer ;
3734import java .util .stream .Stream ;
3835import java .util .stream .StreamSupport ;
39-
4036import javax .sql .DataSource ;
37+ import org .slf4j .Logger ;
38+ import org .slf4j .LoggerFactory ;
4139
4240/**
4341 * An implementation of {@link CustomerDao} that persists customers in RDBMS.
44- *
4542 */
4643public class DbCustomerDao implements CustomerDao {
4744
@@ -50,29 +47,32 @@ public class DbCustomerDao implements CustomerDao {
5047 private final DataSource dataSource ;
5148
5249 /**
53- * Creates an instance of {@link DbCustomerDao} which uses provided <code>dataSource</code>
54- * to store and retrieve customer information.
55- *
50+ * Creates an instance of {@link DbCustomerDao} which uses provided <code>dataSource</code> to
51+ * store and retrieve customer information.
52+ *
5653 * @param dataSource a non-null dataSource.
5754 */
5855 public DbCustomerDao (DataSource dataSource ) {
5956 this .dataSource = dataSource ;
6057 }
6158
6259 /**
63- * @return a lazily populated stream of customers. Note the stream returned must be closed to
64- * free all the acquired resources. The stream keeps an open connection to the database till
65- * it is complete or is closed manually.
60+ * Get all customers as Java Stream.
61+ *
62+ * @return a lazily populated stream of customers. Note the stream returned must be closed to free
63+ * all the acquired resources. The stream keeps an open connection to the database till it is
64+ * complete or is closed manually.
6665 */
6766 @ Override
6867 public Stream <Customer > getAll () throws Exception {
6968
7069 Connection connection ;
7170 try {
7271 connection = getConnection ();
73- PreparedStatement statement = connection .prepareStatement ("SELECT * FROM CUSTOMERS" ); // NOSONAR
72+ PreparedStatement statement =
73+ connection .prepareStatement ("SELECT * FROM CUSTOMERS" ); // NOSONAR
7474 ResultSet resultSet = statement .executeQuery (); // NOSONAR
75- return StreamSupport .stream (new Spliterators .AbstractSpliterator <Customer >(Long .MAX_VALUE ,
75+ return StreamSupport .stream (new Spliterators .AbstractSpliterator <Customer >(Long .MAX_VALUE ,
7676 Spliterator .ORDERED ) {
7777
7878 @ Override
@@ -108,8 +108,8 @@ private void mutedClose(Connection connection, PreparedStatement statement, Resu
108108 }
109109
110110 private Customer createCustomer (ResultSet resultSet ) throws SQLException {
111- return new Customer (resultSet .getInt ("ID" ),
112- resultSet .getString ("FNAME" ),
111+ return new Customer (resultSet .getInt ("ID" ),
112+ resultSet .getString ("FNAME" ),
113113 resultSet .getString ("LNAME" ));
114114 }
115115
@@ -122,8 +122,8 @@ public Optional<Customer> getById(int id) throws Exception {
122122 ResultSet resultSet = null ;
123123
124124 try (Connection connection = getConnection ();
125- PreparedStatement statement =
126- connection .prepareStatement ("SELECT * FROM CUSTOMERS WHERE ID = ?" )) {
125+ PreparedStatement statement =
126+ connection .prepareStatement ("SELECT * FROM CUSTOMERS WHERE ID = ?" )) {
127127
128128 statement .setInt (1 , id );
129129 resultSet = statement .executeQuery ();
@@ -151,8 +151,8 @@ public boolean add(Customer customer) throws Exception {
151151 }
152152
153153 try (Connection connection = getConnection ();
154- PreparedStatement statement =
155- connection .prepareStatement ("INSERT INTO CUSTOMERS VALUES (?,?,?)" )) {
154+ PreparedStatement statement =
155+ connection .prepareStatement ("INSERT INTO CUSTOMERS VALUES (?,?,?)" )) {
156156 statement .setInt (1 , customer .getId ());
157157 statement .setString (2 , customer .getFirstName ());
158158 statement .setString (3 , customer .getLastName ());
@@ -169,8 +169,9 @@ public boolean add(Customer customer) throws Exception {
169169 @ Override
170170 public boolean update (Customer customer ) throws Exception {
171171 try (Connection connection = getConnection ();
172- PreparedStatement statement =
173- connection .prepareStatement ("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?" )) {
172+ PreparedStatement statement =
173+ connection
174+ .prepareStatement ("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?" )) {
174175 statement .setString (1 , customer .getFirstName ());
175176 statement .setString (2 , customer .getLastName ());
176177 statement .setInt (3 , customer .getId ());
@@ -186,8 +187,8 @@ public boolean update(Customer customer) throws Exception {
186187 @ Override
187188 public boolean delete (Customer customer ) throws Exception {
188189 try (Connection connection = getConnection ();
189- PreparedStatement statement =
190- connection .prepareStatement ("DELETE FROM CUSTOMERS WHERE ID = ?" )) {
190+ PreparedStatement statement =
191+ connection .prepareStatement ("DELETE FROM CUSTOMERS WHERE ID = ?" )) {
191192 statement .setInt (1 , customer .getId ());
192193 return statement .executeUpdate () > 0 ;
193194 } catch (SQLException ex ) {
0 commit comments