程序不会进入while循环

时间:2014-07-08 09:48:05

标签: java jdbc

我正在创建一个Web应用程序。我正在使用netbeans 7.4和MySQL 6.1作为我的数据库。我想更改用户的密码。保存在数据库中的密码结构为md5。因此,我必须首先将当前密码的用户输入转换为md5,并检查数据库是否有任何匹配。现在,我在ChangePassword.jsp中有一个表单结构。在表单中有当前,新密码和重新键入密码的三个字段。然后通过servlet,我正在操纵这些信息。

与数据库的连接很好,我已经可以设法将当前密码加密到md5。但是,当我在程序中添加while(rs.next())循环以循环查询的所有结果时,它不起作用。所以它不会转发到所需的jsp。有关如何做到这一点的任何建议?任何帮助都感激不尽。提前致谢。这是我的代码。

try{
            ServletContext context = getServletContext();

            String currentPassword = request.getParameter("currentPassword");
            String newPassword = request.getParameter("newPassword");
            String tryNewPassword = request.getParameter("retypePassword");
            String pswrd = "";

            //For Password Encryption into md5
            String md5 = null;
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(currentPassword.getBytes(), 0, currentPassword.length());
            md5 = new BigInteger(1, digest.digest()).toString(16);

            //Database Connection
            Class.forName(context.getAttribute("dbdriver").toString());
            String db = context.getAttribute("connstr").toString();
            Connection conn = DriverManager.getConnection(db);
            Statement stmt = conn.createStatement();
            stmt = conn.createStatement();

            //Check Database Connection
            if (conn != null) {
                JOptionPane.showMessageDialog(null, "Connected!");
            } else {
                JOptionPane.showMessageDialog(null, "Not Connected!");
            }

            String query = "SELECT password FROM accounts WHERE password='"+md5+"'";
            ResultSet rs = stmt.executeQuery(query);


            JOptionPane.showMessageDialog(null, "Entering while loop");
            while(rs.next()){
            if(md5CurrentPasswrd.equals(rs.getString(1))){
                if(newPassword == tryNewPassword){
                    digest.update(newPassword.getBytes(), 0, newPassword.length());
                    md5NewPasswrd = new BigInteger(1, digest.digest()).toString(16);
                    String queryUpdate = "UPDATE accounts SET password='"+md5NewPasswrd+"' /n"
                            + " WHERE password='"+md5CurrentPasswrd+"'";
                    PreparedStatement prepStmt = conn.prepareStatement(queryUpdate);
                    prepStmt.executeUpdate();

                    response.sendRedirect("/Project1/PasswordSaved.jsp");
                }else{
                    response.sendRedirect("/Project1/PasswordNotSaved.jsp");
                }
            }else{
               response.sendRedirect("/Project1/PasswordNotSaved.jsp");
            }
            pswrd = rs.getString(1);
            JOptionPane.showMessageDialog(null, "Running while loop");
        }

            JOptionPane.showMessageDialog(null, "End of while loop");

        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(ChangePassword.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ChangePassword.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ChangePassword.class.getName()).log(Level.SEVERE, null, ex);
        }

0 个答案:

没有答案