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?
 
CEHJConnect With a Mentor Commented:
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Nick_72Commented:
and of course, use CEHJ's suggestion also :)
0
 
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:
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
All Courses

From novice to tech pro — start learning today.