简体   繁体   中英

JDBC try block does not execute

so the problem is that Try block code does not execute, what i can i do ? and where is the problem ?

public class verif {
    public static String checki(String pseudo, String mdp) {
        boolean check = false;
        String psDB = "", passDB = "";
        Connection con = null;
        PreparedStatement stmt;
        ResultSet res;
        try {

            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel", "naila", "pass");
            stmt = con.prepareStatement("select pseudo,mdp from recp");
            res = stmt.executeQuery();
            while (res.next()) {
                psDB = res.getString("pseudo");
                passDB = res.getString("mdp");
            }

            boolean us = psDB.equals(pseudo);
            boolean ps = passDB.equals(mdp);
            if (us && ps)
                check = true;
            else
                check = false;
        } catch (ClassNotFoundException | SQLException e) {
        }
        return psDB;
    }
}

and this is the servlet :

 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String pseudo=request.getParameter("username");
    String mdp = request.getParameter("password");
    PrintWriter p = response.getWriter();
    p.print("pseudo est "+verif.checki(pseudo, mdp));
  }
Connection con = null;
Connection co=DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel" ,"naila","pass"); 

You have this 2 connections declared but in your prepared statement you use:

stmt = con.prepareStatement("select pseudo,mdp from recp.hotel");

Just remove the con and use co and that should do the trick

Also, when you execute the prepared statement you get a cursor. and that cursor needs to be moved to the next position. For example, if you think that multiple records will be returned the do:

while(rs.next()){  
  psDB=res.getString("pseudo");
  passDB=res.getString("mdp");
}

You just forgot to call next() on the ResultSet res to point the cursor of ResultSet to the first record (if exists any). If you are not calling next method then that cursor will point just before the first result record and then the res.getString(#) will not return anything.

Just do this:

.
.
.
res = stmt.executeQuery();
while(res.next()){
    psDB = res.getString(1).toString();
    passDB = res.getString(2).toString();
}
.
.
.

Another hint is calling res.getString(#) will return values of type String and no need to call .toString() on them again. So it's better to change it like this:

.
.
.
res = stmt.executeQuery();
while(res.next()){
    psDB = res.getString(1);
    passDB = res.getString(2);
}
.
.
.

And it's a good practice to make the result of your sql as narrow as possible to only one record in this case (checking username and password for login purpose etc.). This means only select the password for the username which is passed to your checki method:

.
.
.
stmt = con.prepareStatement("select pseudo, mdp from recp where pseudo = ? ");
stmt.setString(1, pseudo);
res = stmt.executeQuery();
while(res.next()){
    psDB = res.getString(1);
    passDB = res.getString(2);
}
.
.
.

If you have any other errors after this, you may check that how many records do you have in your table and check to see if the record you are selecting in your code is there or not.

After that it may be because of some exception in your code which is caught by the catch clause but can not make any signal to inform you since you do nothing in the catch clause. Change your catch clause like this to see if any exception occurs or not:

}catch (ClassNotFoundException | SQLException e) {
    e.printStackTrace();
}

Your code should be look like this.

public class verif {
    public static boolean checki(String pseudo, String mdp) {
        boolean check = false;
        String psDB = "", passDB = "";
        Connection con = null;
        PreparedStatement stmt;
        ResultSet res;
        try {

            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hotel", "naila", "pass");
            stmt = con.prepareStatement("select pseudo,mdp from recp where pseudo=? AND mdp=?");
            stmt.setString(pseudo);
            stmt.setString(mdp);

            res = stmt.executeQuery();
           /* Not  Needed
         while (res.next()) {
                psDB = res.getString("pseudo");
                passDB = res.getString("mdp");
            }

            boolean us = psDB.equals(pseudo);
            boolean ps = passDB.equals(mdp);*/
            if (res.next())
                check = true;
            else
                check = false;
        } catch (Exception e) {
              e.printStackTrace();
        }
        return check;
    }
}

And Servlet Side code should be like this.

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{   
    String pseudo=request.getParameter("username");
    String mdp = request.getParameter("password");
    PrintWriter p = response.getWriter();
    if(verif.checki(pseudo, mdp)){
        // Put your Business Logic (Create session and redirect to Main Page)
        p.print("Successfully Logged in");
    }
    else{
        // Redirect to LogIn page
        p.print("Invalid Credential ");
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM