Servlet Error

This is pretty easy,
I can't get the boolean value allOk to "work" for lack of a better term.
It is a scoping issue and also pertains to my relative inexperience with java
please help.

        private boolean validateChanges(Connection cn, HttpServletRequest request)
        throws ServletException, IOException {
               
               try
               {      
                     boolean allOk=true;
                    String user = (String)request.getRemoteUser();
                    String query = "SELECT user_pass FROM users WHERE user_name = \"" + request.getRemoteUser() + "\";";
                    Statement statement = cn.createStatement();
                ResultSet resultSet = statement.executeQuery(query);
                    resultSet.moveToCurrentRow();
                    String pass = resultSet.getString(1);
                    
                    if (!request.getParameter("userName").equals(user)) {
                          allOk=false;
                    }
                    if (!request.getParameter("password").equals(pass)) {
                        allOk=false;
                    }
                    if (request.getParameter("password1").equals("q")) {
                          allOk=false;
                    }
                    if (!request.getParameter("password1").equals(request.getParameter("password2"))) {
                          allOk=false;
                    }
                          
              }
          catch(SQLException sqle) {
                  System.err.println("Error connecting: " + sqle);
          }
          catch(Exception ex) {
                  System.err.println("Error with input: " + ex);
          }
          
             return allOk;
      
        }
benk-master-flashAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Change

>>
try
             {    
                  boolean allOk=true;
>>

to



boolean allOk=true;
try
             {    
0
Nick_72Commented:
You might have an exeption thrown, so you'll need to add a finally block to make sure it's always returned:

         catch(SQLException sqle) {
                System.err.println("Error connecting: " + sqle);
                allOK = false;  <--- add this
         }
         catch(Exception ex) {
                System.err.println("Error with input: " + ex);
                allOK = false;  <--- add this
         }
         finally {
                return allOK;  <----- this code is always run no matter what
         }
       }

/Nick
0
Nick_72Commented:
and of course, use CEHJ's suggestion also :)
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

benk-master-flashAuthor Commented:
followed both of your suggestions and incorporated both your adjustments plus one of my own
with the following code allOk always evaluates to true and thus sends me to the /index2.jsp page
how can make sure to execute the block of code that validates the changes to the password

if (validateChanges(connection,request)) {
                          commitUpdate(connection,request);
                          connection.close();
                          address = "/index2.jsp";
                    } else {
                          address = "/ChangePassword.jsp";                                
                    }
                    RequestDispatcher dispatcher = request.getRequestDispatcher(address);
                    dispatcher.forward(request,response);

private boolean validateChanges(Connection cn, HttpServletRequest request)
        throws ServletException, IOException {
            boolean allOk=true;
               try
               {      
                    String user = (String)request.getRemoteUser();
                    String query = "SELECT user_pass FROM users WHERE user_name = \"" + request.getRemoteUser() + "\";";
                    Statement statement = cn.createStatement();
                ResultSet resultSet = statement.executeQuery(query);
                    resultSet.moveToCurrentRow();
                    String pass = resultSet.getString(1);
                    /*
                    if (!request.getParameter("userName").equals(user)) {
                          allOk=false;
                    }
                    if (!request.getParameter("password").equals(pass)) {
                        allOk=false;
                    }
                    if (request.getParameter("password1").equals("q")) {
                          allOk=false;
                    }
                    if (!request.getParameter("password1").equals(request.getParameter("password2"))) {
                          allOk=false;
                    }
                    */
                    allOk=false;            
              }
          catch(SQLException sqle) {
                  System.err.println("Error connecting: " + sqle);
          }
          catch(Exception ex) {
                  System.err.println("Error with input: " + ex);
          }
          finally {
              return allOk;
                }
       }
0
CEHJCommented:
I would set allOk to false first then do:

String query = "SELECT user_pass FROM users WHERE user_name = \"" + request.getRemoteUser() + "\";";
Statement statement = cn.createStatement();
ResultSet resultSet = statement.executeQuery(query);
String pass = resultSet.getString(1);
if (resultSet.next()) {
      allOk = request.getParameter("userName").equals(user) &&
            request.getParameter("password").equals(pass) &&
            request.getParameter("password1").equals("q") &&
            request.getParameter("password1").equals(request.getParameter("password2"));
}      

(that would be the whole of the try block)      
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
You don't need terminating semicolons on your single SQL statements btw. There's an error in my last post:

>>String pass = resultSet.getString(1);
>>if (resultSet.next()) {

should be

if (resultSet.next()) {
     String pass = resultSet.getString(1);


0
objectsCommented:
looks like your sql query is failing.
check the logs for any errors.
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.