Incorporated review changes - 1) Created sql file for central schema 2)

Changed getById return type to Optional
This commit is contained in:
Narendra Pathai
2016-03-28 11:55:22 +05:30
parent f32a3892a3
commit ddbc61b140
7 changed files with 52 additions and 32 deletions

View File

@@ -73,15 +73,14 @@ public class App {
private static void deleteSchema(DataSource dataSource) throws SQLException {
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
statement.execute("DROP TABLE CUSTOMERS");
statement.execute(CustomerSchemaSql.DELETE_SCHEMA_SQL);
}
}
private static void createSchema(DataSource dataSource) throws SQLException {
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
statement.execute("CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
+ "LNAME VARCHAR(100))");
statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL);
}
}

View File

@@ -23,6 +23,7 @@
package com.iluwatar.dao;
import java.util.Optional;
import java.util.stream.Stream;
/**
@@ -51,10 +52,11 @@ public interface CustomerDao {
/**
* @param id unique identifier of the customer.
* @return customer with unique identifier <code>id</code> if found, null otherwise.
* @return an optional with customer if a customer with unique identifier <code>id</code>
* exists, empty optional otherwise.
* @throws Exception if any error occurs.
*/
Customer getById(int id) throws Exception;
Optional<Customer> getById(int id) throws Exception;
/**
* @param customer the customer to be added.

View File

@@ -0,0 +1,9 @@
package com.iluwatar.dao;
public interface CustomerSchemaSql {
String CREATE_SCHEMA_SQL = "CREATE TABLE CUSTOMERS (ID NUMBER, FNAME VARCHAR(100), "
+ "LNAME VARCHAR(100))";
String DELETE_SCHEMA_SQL = "DROP TABLE CUSTOMERS";
}

View File

@@ -27,6 +27,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Optional;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
@@ -109,7 +110,7 @@ public class DbCustomerDao implements CustomerDao {
* {@inheritDoc}
*/
@Override
public Customer getById(int id) throws Exception {
public Optional<Customer> getById(int id) throws Exception {
try (Connection connection = getConnection();
PreparedStatement statement =
connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
@@ -117,9 +118,9 @@ public class DbCustomerDao implements CustomerDao {
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
return createCustomer(resultSet);
return Optional.of(createCustomer(resultSet));
} else {
return null;
return Optional.empty();
}
} catch (SQLException ex) {
throw new Exception(ex.getMessage(), ex);
@@ -131,7 +132,7 @@ public class DbCustomerDao implements CustomerDao {
*/
@Override
public boolean add(Customer customer) throws Exception {
if (getById(customer.getId()) != null) {
if (getById(customer.getId()).isPresent()) {
return false;
}

View File

@@ -25,6 +25,7 @@ package com.iluwatar.dao;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
/**
@@ -46,13 +47,13 @@ public class InMemoryCustomerDao implements CustomerDao {
}
@Override
public Customer getById(final int id) {
return idToCustomer.get(id);
public Optional<Customer> getById(final int id) {
return Optional.ofNullable(idToCustomer.get(id));
}
@Override
public boolean add(final Customer customer) {
if (getById(customer.getId()) != null) {
if (getById(customer.getId()).isPresent()) {
return false;
}