Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,6 @@ using prepared statement.
- [Accessing Databases Through JDBC with Java](https://app.pluralsight.com/projects/accessing-databases-through-jdbc-in-java)
- [Design_Patterns_In_Java_CRUD](https://github.com/naman14310/Design_Patterns_In_Java_CRUD)
- [Database Normalization](http://extreme-java.blogspot.com/2014/05/database-normalization.html)
- [Database Interaction with DAO and DTO Design Patterns](https://dzone.com/articles/database-interaction-dao-and)
- [Database Interaction with DAO and DTO Design Patterns](https://dzone.com/articles/database-interaction-dao-and)
- [`try`-with-resources](https://en.wikipedia.org/wiki/Java_syntax#try-with-resources_statements)
- [tryResourceClose](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)
53 changes: 52 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,36 @@
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<version>23.1.0</version>
<scope>compile</scope>
</dependency>

<!--LOMBOK-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
</dependencies>

<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.19.0</version>
<configuration>
<rulesets>
<!-- A rule set, that comes bundled with PMD -->
<ruleset>pmd-ruleset.xml</ruleset>

</rulesets>
</configuration>
</plugin>
</plugins>
</reporting>

<build>
<finalName>orders</finalName>
<plugins>
Expand Down Expand Up @@ -97,6 +122,32 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.19.0</version> <!-- or use version from pluginManagement -->
<configuration>
<!-- failOnViolation is actually true by default, but can be disabled -->
<failOnViolation>true</failOnViolation>
<!-- printFailingErrors is pretty useful -->
<printFailingErrors>true</printFailingErrors>

<rulesets>
<!-- A rule set, that comes bundled with PMD -->
<ruleset>pmd-ruleset.xml</ruleset>

</rulesets>
</configuration>
<executions>
<execution>
<id>pmd-check</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
17 changes: 8 additions & 9 deletions src/main/java/com/example/order/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -32,11 +33,10 @@ public class Main {
*
* @param args Command and arguments to be interpreted by the application
*/
public static void main(String[] args) {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String command = "";
public static void main(String[] args) throws SQLException, IOException {

try {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
String command = "";
while (!Commands.EXIT.getCmd().equalsIgnoreCase(command)) {
System.out.print(INITIAL_PROMPT);
String line = reader.readLine();
Expand All @@ -63,7 +63,7 @@ public static void main(String[] args) {
*
* @param args Arguments given to the application
*/
private static void processCommand(String[] args) {
private static void processCommand(String[] args) throws SQLException, IOException {
String error = validateArgs(args);

if (isEmpty(error)) {
Expand Down Expand Up @@ -120,10 +120,9 @@ private static void processCommand(String[] args) {
*/
private static @NotNull OrderDto askForOrderDetails() {
OrderDto orderDTO = new OrderDto();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

boolean invalidInput;
try {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
boolean invalidInput;
/* Ask for customer ID */
do {
System.out.print("Customer ID: ");
Expand All @@ -141,7 +140,7 @@ private static void processCommand(String[] args) {
do {
OrderDetailDto orderDetailDTO = new OrderDetailDto();

// Ask for product ID
/* Ask for product ID */
do {
System.out.print("Product ID: ");
String line = reader.readLine();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/example/order/dao/DeleteOrderDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.example.order.util.ExceptionHandler;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
Expand Down Expand Up @@ -32,7 +33,7 @@ public DeleteOrderDao(Database database) {
* @param paramsDto Object with the parameters for the operation
* @return Number of orders deleted
*/
public int deleteOrdersById(@NotNull ParamsDto paramsDto) {
public int deleteOrdersById(@NotNull ParamsDto paramsDto) throws IOException {
int numberResults = 0;

try (Connection con = database.getConnection();
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/example/order/dao/GetOrderDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.example.order.util.ExceptionHandler;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand All @@ -16,7 +17,7 @@
* DAO to get an order
*/
public class GetOrderDao {
private final String query = "SELECT * FROM orders o WHERE o.order_id = ?";
private final static String query = "SELECT * FROM orders o WHERE o.order_id = ?";
private final Database database;

/**
Expand Down Expand Up @@ -53,8 +54,8 @@ public OrderDto getOrderById(ParamsDto paramsDto) {
orderDto.setStatus(rs.getString(4));
}

} catch (SQLException ex) {
ExceptionHandler.handleException(ex);
} catch (SQLException | IOException ex) {
ExceptionHandler.handleException((SQLException) ex);
}

return orderDto;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/example/order/dao/InsertOrderDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.example.order.util.OrderStatus;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.sql.*;
import java.time.LocalDateTime;

Expand Down Expand Up @@ -38,7 +39,7 @@ public InsertOrderDao(Database database) {
* @param orderDto Object with the information to insert
* @return The ID of the order inserted
*/
public long insertOrder(OrderDto orderDto) {
public long insertOrder(OrderDto orderDto) throws IOException {
long orderId = -1;

try (Connection con = database.getConnection();
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/example/order/dao/TotalOrderDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import com.example.order.util.ExceptionHandler;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.*;

/**
* DAO to get the total of all the paid orders of a customer
*/
public class TotalOrderDao {
private final String query = "{call GET_PAID_ORDER_TOTAL_FROM_CUSTOMER(?)}";
private final static String query = "{call GET_PAID_ORDER_TOTAL_FROM_CUSTOMER(?)}";
private final Database database;

/**
Expand All @@ -30,7 +31,7 @@ public TotalOrderDao(Database database) {
* @param paramsDto Object with the arguments of the operation
* @return Total of all paid orders
*/
public BigDecimal getTotalAllPaidOrders(ParamsDto paramsDto) {
public BigDecimal getTotalAllPaidOrders(ParamsDto paramsDto) throws SQLException, IOException {
BigDecimal result = null;

try (Connection con = database.getConnection();
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/example/order/dao/UpdateOrderDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.example.order.util.ExceptionHandler;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
Expand All @@ -13,7 +14,6 @@
* DAO to update an order
*/
public class UpdateOrderDao {
private final String query = "UPDATE orders o SET o.order_status = ? WHERE o.order_id = ?";
private final Database database;

/**
Expand All @@ -31,7 +31,7 @@ public UpdateOrderDao(Database database) {
* @param paramsDto Object with the parameters for the operation
* @return Number of affected rows
*/
public int updateOrderStatus(ParamsDto paramsDto) {
public int updateOrderStatus(ParamsDto paramsDto) throws IOException {
int numberResults = 0;

try (Connection con = database.getConnection();
Expand All @@ -55,6 +55,7 @@ public int updateOrderStatus(ParamsDto paramsDto) {
* @throws SQLException In case of an error
*/
private @NotNull PreparedStatement createPreparedStatement(@NotNull Connection con, @NotNull ParamsDto paramsDto) throws SQLException {
String query = "UPDATE orders o SET o.order_status = ? WHERE o.order_id = ?";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1, paramsDto.getStatus());
ps.setLong(2, paramsDto.getOrderId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.example.order.dto.ParamsDto;
import com.example.order.util.Database;

import java.io.IOException;

/**
* Service class to delete one or more orders
*/
Expand All @@ -16,7 +18,7 @@ public class DeleteOrderService implements OrderService {
* @param paramsDTO Object with the parameters to execute the service
*/
@Override
public String execute(ParamsDto paramsDTO) {
public String execute(ParamsDto paramsDTO) throws IOException {
String result;
int rowsAffected = deleteOrderDao.deleteOrdersById(paramsDTO);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.example.order.util.Database;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

/**
* Service class to insert an order
*/
Expand All @@ -17,7 +19,7 @@ public class InsertOrderService implements OrderService {
* @param paramsDTO Object with the parameters to execute the service
*/
@Override
public String execute(@NotNull ParamsDto paramsDTO) {
public String execute(@NotNull ParamsDto paramsDTO) throws IOException {
String result;
long orderId = insertOrderDao.insertOrder(paramsDTO.getOrder());

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/example/order/service/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import com.example.order.dto.ParamsDto;

import java.io.IOException;
import java.sql.SQLException;

/**
* Interface for service classes
*/
public interface OrderService {
String execute(ParamsDto paramsDTO);
String execute(ParamsDto paramsDTO) throws IOException, SQLException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.example.order.dto.ParamsDto;
import com.example.order.util.Database;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;

/**
* Service class to get the total of the orders of a customer
Expand All @@ -18,7 +20,7 @@ public class TotalOrderService implements OrderService {
* @param paramsDTO Object with the parameters to execute the service
*/
@Override
public String execute(ParamsDto paramsDTO) {
public String execute(ParamsDto paramsDTO) throws IOException, SQLException {
String result;
BigDecimal total = totalOrderDao.getTotalAllPaidOrders(paramsDTO);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.example.order.dto.ParamsDto;
import com.example.order.util.Database;

import java.io.IOException;

/**
* Service class to update an order
*/
Expand All @@ -16,7 +18,7 @@ public class UpdateOrderService implements OrderService {
* @param paramsDTO Object with the parameters to execute the service
*/
@Override
public String execute(ParamsDto paramsDTO) {
public String execute(ParamsDto paramsDTO) throws IOException {
String result;
int rowsAffected = updateOrderDao.updateOrderStatus(paramsDTO);

Expand Down
Loading