From 8684c18bbc289da4f6bda56a00a76ea5cd917390 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Thu, 13 Aug 2015 12:51:46 +0200 Subject: [PATCH] =?utf8?q?Continued=20with=20project:=20-=20added=20thrown?= =?utf8?q?=20exceptions=20-=20Very=20rude=20implementation=20of=20getResul?= =?utf8?q?tFromSet()=20-=20getProducts()=20and=20getCategories()=20now=20o?= =?utf8?q?nly=20throw=20ServletException=20-=20The=20above=20change=20allo?= =?utf8?q?ws=20to=20remove=20the=20try-catch=20block=20Signed-off-by:Rolan?= =?utf8?q?d=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../application/PizzaApplication.java | 12 ++--- .../application/PizzaServiceApplication.java | 47 +++++++----------- .../frontend/category/CategoryFrontend.java | 4 +- .../PizzaCategoryDatabaseFrontend.java | 48 +++++++++++++++---- .../product/PizzaProductDatabaseFrontend.java | 48 +++++++++++++++---- .../frontend/product/ProductFrontend.java | 4 +- 6 files changed, 108 insertions(+), 55 deletions(-) diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java index 89f46456..22901cab 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java @@ -16,13 +16,11 @@ */ package org.mxchange.pizzaapplication.application; -import java.io.IOException; import java.util.Iterator; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.mxchange.jcore.application.Application; -import org.mxchange.jcore.exceptions.BadTokenException; import org.mxchange.pizzaapplication.category.Category; import org.mxchange.pizzaapplication.product.Product; @@ -204,19 +202,17 @@ public interface PizzaApplication extends Application { * Some "getter" for a an array of all products * * @return All products - * @throws java.io.IOException If an IO error occurs - * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found + * @throws javax.servlet.ServletException If anything went wrong */ - public Iterator getProducts () throws IOException, BadTokenException; + public Iterator getProducts () throws ServletException; /** * Some "getter" for a an array of all categories * * @return All categories - * @throws java.io.IOException If an IO error occurs - * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found + * @throws javax.servlet.ServletException If anything went wrong */ - public Iterator getCategories () throws IOException, BadTokenException; + public Iterator getCategories () throws ServletException; /** * Checks if given Product instance is available and returns a printable diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java index 4c4335c9..1603c70f 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java @@ -164,14 +164,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P // Init/declare total price and iterator int totalAmount = 0; - Iterator iterator; - - try { - // Get iterator - iterator = this.getProducts(); - } catch (final IOException | BadTokenException ex) { - throw new ServletException(ex); - } + Iterator iterator = this.getProducts(); // "Walk" over all products while (iterator.hasNext()) { @@ -225,12 +218,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P float totalPrice = 0.00f; // Get iterator - Iterator iterator; - try { - iterator = this.getProducts(); - } catch (final IOException | BadTokenException ex) { - throw new ServletException(ex); - } + Iterator iterator = this.getProducts(); // "Walk" over all products while (iterator.hasNext()) { @@ -532,9 +520,13 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * @return All products */ @Override - public Iterator getProducts () throws IOException, BadTokenException { - // Ask frontend for a list of products - return this.productFrontend.getProducts(); + public Iterator getProducts () throws ServletException { + try { + // Ask frontend for a list of products + return this.productFrontend.getProducts(); + } catch (final IOException | BadTokenException | SQLException ex) { + throw new ServletException(ex); + } } /** @@ -543,9 +535,13 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * @return All categories */ @Override - public Iterator getCategories () throws IOException, BadTokenException { - // Ask frontend for a list of categories - return this.categoryFrontend.getCategories(); + public Iterator getCategories () throws ServletException { + try { + // Ask frontend for a list of categories + return this.categoryFrontend.getCategories(); + } catch (final IOException | BadTokenException | SQLException ex) { + throw new ServletException(ex); + } } /** @@ -756,14 +752,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); // Init iterator - Iterator iterator; - - try { - // Get iterator - iterator = this.getProducts(); - } catch (final IOException | BadTokenException ex) { - throw new ServletException(ex); - } + Iterator iterator = this.getProducts(); // "Walk" over all products while (iterator.hasNext()) { @@ -1122,7 +1111,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P try { // Get iterator iterator = this.getProducts(); - } catch (final IOException | BadTokenException ex) { + } catch (final ServletException ex) { this.abortProgramWithException(ex); } diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java index baa6e14f..831e9aec 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java @@ -17,6 +17,7 @@ package org.mxchange.pizzaapplication.database.frontend.category; import java.io.IOException; +import java.sql.SQLException; import java.util.Iterator; import org.mxchange.jcore.database.frontend.DatabaseFrontend; import org.mxchange.jcore.exceptions.BadTokenException; @@ -35,6 +36,7 @@ public interface CategoryFrontend extends DatabaseFrontend { * @return Iterator on all categories * @throws java.io.IOException If any IO error occurs * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-) + * @throws java.sql.SQLException If any SQL error occurs */ - public Iterator getCategories () throws IOException, BadTokenException; + public Iterator getCategories () throws IOException, BadTokenException, SQLException; } diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java index 64ee9609..65b3612f 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java @@ -17,12 +17,14 @@ package org.mxchange.pizzaapplication.database.frontend.category; import java.io.IOException; +import java.sql.ResultSet; import java.sql.SQLException; import java.text.MessageFormat; import java.util.Iterator; import org.mxchange.jcore.criteria.searchable.SearchCriteria; import org.mxchange.jcore.criteria.searchable.SearchableCritera; import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend; +import org.mxchange.jcore.database.result.DatabaseResult; import org.mxchange.jcore.database.result.Result; import org.mxchange.jcore.database.storage.Storeable; import org.mxchange.jcore.exceptions.BadTokenException; @@ -69,16 +71,9 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen this.getLogger().trace("EXIT!"); //NOI18N } - /** - * An iterator on all products - * - * @return Iterator on all products - * @throws java.io.IOException If any IO error occurs - * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-) - */ @Override @SuppressWarnings ("unchecked") - public Iterator getCategories () throws IOException, BadTokenException { + public Iterator getCategories () throws IOException, BadTokenException, SQLException { // Trace message this.getLogger().trace("CALLED!"); //NOI18N @@ -101,6 +96,43 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen return (Iterator) iterator; } + /** + * Gets a Result back from given ResultSet instance + * + * @param resultSet ResultSet instance from SQL driver + * @return A typorized Result instance + * @throws java.sql.SQLException If any SQL error occurs + */ + @Override + public Result getResultFromSet (final ResultSet resultSet) throws SQLException { + // Trace message + this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet)); + + // Init result instance + Result result = new DatabaseResult(); + + // Reset result set before first row + resultSet.beforeFirst(); + + // "Walk" through all entries + while (resultSet.next()) { + // Unwrap whole object + Category category = resultSet.unwrap(Category.class); + + // Debug log + this.getLogger().debug(MessageFormat.format("category={0}", category)); + + // Add it to result + result.add(category); + } + + // Trace message + this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result)); + + // Return result + return result; + } + /** * Parses given line from database backend into a Storeable instance. Please * note that not all backends need this. diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java index 1fc6fe5f..5f5820a2 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java @@ -17,12 +17,14 @@ package org.mxchange.pizzaapplication.database.frontend.product; import java.io.IOException; +import java.sql.ResultSet; import java.sql.SQLException; import java.text.MessageFormat; import java.util.Iterator; import org.mxchange.jcore.criteria.searchable.SearchCriteria; import org.mxchange.jcore.criteria.searchable.SearchableCritera; import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend; +import org.mxchange.jcore.database.result.DatabaseResult; import org.mxchange.jcore.database.result.Result; import org.mxchange.jcore.database.storage.Storeable; import org.mxchange.jcore.exceptions.BadTokenException; @@ -71,16 +73,9 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement this.getLogger().trace("EXIT!"); //NOI18N } - /** - * An iterator on all products - * - * @return Iterator on all products - * @throws org.mxchange.jcore.exceptions.BadTokenException - * @throws java.io.IOException If any IO error occurs - */ @Override @SuppressWarnings ("unchecked") - public Iterator getProducts () throws IOException, BadTokenException { + public Iterator getProducts () throws IOException, BadTokenException, SQLException { // Trace message this.getLogger().trace("CALLED!"); //NOI18N @@ -106,6 +101,43 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement return (Iterator) iterator; } + /** + * Gets a Result back from given ResultSet instance + * + * @param resultSet ResultSet instance from SQL driver + * @return A typorized Result instance + * @throws java.sql.SQLException If any SQL error occurs + */ + @Override + public Result getResultFromSet (final ResultSet resultSet) throws SQLException { + // Trace message + this.getLogger().trace(MessageFormat.format("resultSet={0} - CALLED!", resultSet)); + + // Init result instance + Result result = new DatabaseResult(); + + // Reset result set before first row + resultSet.beforeFirst(); + + // "Walk" through all entries + while (resultSet.next()) { + // Unwrap whole object + Product product = resultSet.unwrap(Product.class); + + // Debug log + this.getLogger().debug(MessageFormat.format("product={0}", product)); + + // Add it to result + result.add(product); + } + + // Trace message + this.getLogger().trace(MessageFormat.format("result({0})={1} - EXIT!", result.size(), result)); + + // Return result + return result; + } + /** * Parses given line from database backend into a Storeable instance. Please * note that not all backends need this. diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java index c7f8b72a..0ceadaa6 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java @@ -17,6 +17,7 @@ package org.mxchange.pizzaapplication.database.frontend.product; import java.io.IOException; +import java.sql.SQLException; import java.util.Iterator; import org.mxchange.jcore.database.frontend.DatabaseFrontend; import org.mxchange.jcore.exceptions.BadTokenException; @@ -35,6 +36,7 @@ public interface ProductFrontend extends DatabaseFrontend { * @return Iterator on all products * @throws java.io.IOException If any IO error occurs * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-) + * @throws java.sql.SQLException If any SQL errors occur */ - public Iterator getProducts () throws IOException, BadTokenException; + public Iterator getProducts () throws IOException, BadTokenException, SQLException; } -- 2.39.5