I'm having an issue with if-statements that are encapsulated in while loops, in summary the if-statements are not recognising whether the stated condition is true or not (according to my trouble shooting steps) but the condition should definitely be met.
Here is a list of what works (therefore they should be left out):
- The DB Connection.
- The initial SQL select statement
- The while loop retrieves all database coloumn and row valus as excpected
- The Try/Catch block and its exception handling.
- Executing SQL statements inside the while loop but before any if-statement.
My most recent trouble shooting step is what led me to identify where the issue is occurring. The two variables "ValueInBox" AND "ProductName" always has the expected value which means that any of the first three if-statemnet should be executed as their condition is true! But it doesn't produce an excpected output :(
How do I know you may ask?!?!? if look at my last if statement, this is the only one that executes, and it prints out the value of the variables on the consol window as shown in the screen shot below:
As you see from the screen shot that according to the value of the variables, those first three if-statements should execute, but nooo no matter how much time I try to reason with these if-statements they go on strike and demand a pay rise :(
Im sure i missed out on something realy stupid and small, Thanks!
public void actionPerformed(ActionEvent arg0) {
try {
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://h:/AgentsDataBase.accdb");
Statement s = conn.createStatement();
String stmnt = "SELECT * FROM WishListProducts WHERE ProductStatus = Yes";
ResultSet rs = s.executeQuery(stmnt);
String ValueInBox = AddRemoveWishlistProductTF.getText();
//stmnt = "DELETE FROM WishListProducts WHERE ProductName ='Penguin'";
//s.executeUpdate(stmnt);
while (rs.next()) {
String ProductName = rs.getString("ProductName");
//stmnt = "DELETE FROM WishListProducts WHERE ProductName ='Penguin'";
//s.executeUpdate(stmnt);
if (ProductName == ValueInBox ) { // if it exists within the DB
stmnt = "DELETE FROM WishListProducts WHERE ProductName ='Penguin'";
s.executeUpdate(stmnt);
}
else if(ProductName == "Penguin") {
stmnt = "DELETE FROM WishListProducts WHERE ProductName ='Penguin'";
s.executeUpdate(stmnt);
}
else if(ValueInBox == "Penguin") {
stmnt = "DELETE FROM WishListProducts WHERE ProductName ='Penguin'";
s.executeUpdate(stmnt);
}
//System.out.println("val in box" + ValueInBox);
//System.out.println("Val in DB" + ProductName );}
else if (ProductName != ValueInBox) { // if it does not exist
System.out.println("val in box" + ValueInBox);
System.out.println("Val in DB" + ProductName )
}
}
s.close();
}
catch(Exception e) {
e.printStackTrace();
}
}