Summary: When called from servlets, DatabaseHelper
methods ignore the SQL/query part, in this case, INSERT
and SELECT
queries (Database isn't affected). Rest of the method logic works, even that based on successful execution of these queries i.e. the if
statements. However, method calls from DatabaseHelper.main()
(contains no program logic other than testing this issue) works just fine and the queries work (Database is affected).
Specifics: I have a simple 'ShoppingCart' project using NetBeans 11.1.
I am having issues with loading available products data or saving the orders to the database. Method calls from ShoppingCartServlet
and CheckoutServlet
servlets to the DatabaseHelper.java
method (given below) don't seem to work as expected (seem to skip db related code).
However, calls to DatabaseHelper.loadProductsData()
and DatabaseHelper.saveOrdersData()
from within the DatabaseHelper
work.
I created DatabaseHelper.main()
for calling from within and ran DatabaseHelper.java
individually in NetBeans and it ran perfectly.
However the servlets seem to ignore the db related parts.
ShoppingCartServlet
gets returned an emptyproductsList
ArrayList whereasDatabaseHelper.main()
gets a filled ArrayList. If i manually fill the ArrayList ( in the finally block ofDatabaseHelper.loadProductsData()
), then the method returns a filled list to the servlet just likeDatabaseHelper.main()
.CheckoutServlet
gets a Boolean true returned by callingDatabaseHelper.saveOrdersData()
as expected, just likeDatabaseHelper.main()
however it doesn't actually save the orders to the database unlikeDatabaseHelper.main()
which does save.
DatabaseHelper.loadProductsData():
try {
String query_loadProductsData = "SELECT * FROM ProductsData";
String connectionURL = "";
connectionURL = "jdbc:ucanaccess://D:/OneDrive/VU/Records/7/CS506/Assignments/3/ShoppingCart/web/assets/bc160400944.accdb";
Connection conn = DriverManager.getConnection(connectionURL);
Statement stmt = conn.createStatement();
ResultSet result_set = stmt.executeQuery(query_loadProductsData);
conn.close();
Boolean empty = true;
// populate ArrayList
while (result_set.next()) {
id = result_set.getInt("ProductID");
name = result_set.getString("ProductName");
price = result_set.getFloat("Price");
quantity = result_set.getInt("Quantity");
productsList.add(new Product(id, name, (float) price, quantity));
empty = false;
}
if( empty ) {
System.out.println("failed to executeQuery");
}
} catch (SQLException sql_ex) {
System.out.println("SQL Exception has occured. Details given below: \n" + sql_ex);
} finally {
if (productsList.isEmpty()) {
productsList.add(new Product(1, "Flour" ,(float) 18.00 ,20));
productsList.add(new Product(2, "Oil" ,(float) 19.00 ,50));
productsList.add(new Product(3, "Ghee" ,(float) 10.00, 0));
productsList.add(new Product(4, "Sauce" ,(float) 22.00 ,40));
productsList.add(new Product(5, "Sugar" ,(float) 21.35 ,35));
productsList.add(new Product(6, "Salt" ,(float) 25.00 ,26));
productsList.add(new Product(7, "Daal" ,(float) 30.00 ,38));
productsList.add(new Product(8, "Rice" ,(float) 40.00 ,92));
productsList.add(new Product(9, "Pasta" ,(float) 97.00 ,63));
productsList.add(new Product(10, "Fruits" ,(float) 35.00 ,72));
productsList.add(new Product(11, "Dry Fruits" ,(float) 31.00 ,70));
productsList.add(new Product(12, "Tea" ,(float) 21.00 ,12));
productsList.add(new Product(13, "Coffee" ,(float) 38.00, 9));
productsList.add(new Product(14, "Milk" ,(float) 10.00 ,10));
productsList.add(new Product(15, "Snacks" ,(float) 23.25 ,13));
productsList.add(new Product(16, "Dessert" ,(float) 15.50 ,22));
productsList.add(new Product(17, "Vegetables" ,(float) 17.45 ,52));
productsList.add(new Product(18, "Chicken" ,(float) 39.00 ,98));
productsList.add(new Product(19, "Beef" ,(float) 62.50, 0));
productsList.add(new Product(20, "Frozen Food" ,(float) 19.20 ,30));
productsList.add(new Product(21, "Sea Food" ,(float) 81.00 ,29));
productsList.add(new Product(22, "Sweet" ,(float) 10.00 ,29));
productsList.add(new Product(23, "Cake" ,(float) 21.00, 0));
productsList.add(new Product(24, "Yogurt" ,(float) 26.00, 0));
productsList.add(new Product(25, "Pickle" ,(float) 30.00 ,41));
}
return productsList;
}
}
DatabaseHelper.saveOrdersData():
public static Boolean saveOrdersData() {
try {
String query_saveOrdersData = "INSERT INTO OrdersData (NoItems, SubTotal, Shipping, GrandTotal, Address)";
query_saveOrdersData += "VALUES (15, 15, 15, 30, 'address'); ";
String connectionURL = "";
connectionURL = "jdbc:ucanaccess://D:/OneDrive/VU/Records/7/CS506/Assignments/3/ShoppingCart/web/assets/bc160400944.accdb";
Connection conn = DriverManager.getConnection(connectionURL);
Statement stmt = conn.createStatement();
int affected_rows = stmt.executeUpdate(query_saveOrdersData);
conn.close();
if(affected_rows>0) {
System.out.println("Order data has been saved.");
}
return true;
} catch (SQLException sql_ex) {
System.out.println("SQL Exception has occured. Details given below: \n" + sql_ex);
return true;
}
}
PS: If you are testing this, you might want to change the db conn URL accordingly. I went with absolute URL. Couldn't get the right relative one. Also, I am very new to Java and this is my first servlet based program. Please keep it simple.