尝试在Java Swing中登录3次

尝试在Java Swing中登录3次,java,swing,Java,Swing,我的问题是,我的某些代码在第一次尝试后没有递减或递增,它递减1,但在另一次尝试后它没有递减,请帮助我这是一个项目。这对我有很大帮助。这是登录尝试。如果密码正确,它将转到另一个面板,但如果密码错误,它将不会转到,并警告3次。如果尝试次数为3次。该帐户将在数据库中停用 我已经试着把声明放进去了,但都没用 字符串x=BI.getText; 字符串x2=WD.getText; 字符串x3=FT.getText; char[]pass=passtft.getPassword; 字符串密码=新的字符串密码;

我的问题是,我的某些代码在第一次尝试后没有递减或递增,它递减1,但在另一次尝试后它没有递减,请帮助我这是一个项目。这对我有很大帮助。这是登录尝试。如果密码正确,它将转到另一个面板,但如果密码错误,它将不会转到,并警告3次。如果尝试次数为3次。该帐户将在数据库中停用

我已经试着把声明放进去了,但都没用

字符串x=BI.getText; 字符串x2=WD.getText; 字符串x3=FT.getText; char[]pass=passtft.getPassword; 字符串密码=新的字符串密码; 布尔容器=false; System.out.printlnacs; int x5=3; 如果isclick.equalsx{ 试一试{ Connection con=DriverManager.getconnectiondbur、userdb、passdb; 字符串查询=从accountnumber='+accs+'的帐户中选择*; 语句st=con.createStatement; 结果集rs=st.executeQueryquery; 而rs.next{ String pin=rs.getStringpin; 字符串saltpin=rs.getStringsaltpin; 布尔验证=PasswordUtils.verifyUserPasswordpassword,pin,saltpin; 如果验证==true{ 容器=真; }否则{ 容器=假; x5-; } } rs.close; 圣克洛斯; con.close; }捕获异常e1{ System.out.printlne1; } 如果容器==true{ PassPanel.setVisiblefalse; balancePanel.setVisibletrue; }否则{ JOptionPane.showMessageDialognull,Pin错误!; passtft.setText; 数量=; } System.out.printlnx5; 如果x5==0{ 试一试{ 连接cay=DriverManager.getconnectiondbur、userdb、passdb; 字符串qr=更新帐户集状态='+deactivated+',其中accountnumber='+accs+'; 语句rst=cay.createStatement; rst.executeUpdateqr; 克洛斯礁; rst.close; }捕获异常e2{ } JOptionPane.showMessageDialognull,您的卡被阻止!您已达到最大限制!请联系我们的客户服务!; } } 我希望减少,如果尝试次数为3,它将更新到数据库

建议:

关于良好的变量和对象命名的好处,以及x this、x that或x其他任何不属于该领域的东西,总有一些值得一提的地方。当然,您可以为您的对象指定任何您喜欢的名称,只要它们遵循那些用石头写的Java规则,但是为了您和其他人将来的理智起见,明智的做法是对您的命名进行一定程度的描述。它确实有助于更轻松地遵循代码,甚至在第一次运行代码之前就可以非常有利于捕获bug。底线,你的选择……我只是说:哦

保持与数据库的连接处于打开状态,直到您知道已完成连接。在同一方法中,不需要在不同条件下始终打开和关闭它。利用finally块关闭方法末尾的对象

使用SQL字符串处理数据库数据时使用PreparedStatement。这将有助于防止SQL注入的可能性,并使您的SQL字符串更加安全。对这件事多了解一点

虽然不是绝对强制性的,而且在这个特定的用例中,我认为您应该尝试使用while循环来从数据库中获取数据。我不会开始跳转到其他方法,检查可能存在不止一次迭代的特定条件。毕竟,如果在第一次迭代中一个条件为真,但在第二次迭代中,如果有一个条件为假,该怎么办

当前的任务是:

因为您没有公开示例代码的主页,所以我必须假设它包含在Submit按钮的事件块中或类似的内容中,所以我下面提供的示例代码使用return语句退出事件方法

下面的示例不是递减登录尝试实例变量计数器,而是递增名为:LoginAttents的计数器。这是在尝试从数据库中获取相关数据后完成的,以防在该特定过程中发生错误。你不想因为某人无法控制的事情而冒险禁用他的卡

阅读代码中的注释:

// Class Member (instance) Variables
Connection conn = null;
int loginAttempts = 0;
int maxAttemptsAllowed = 3;
String acountNum = "AB42321"; 
String customerServicePhone = "XXX-XXX-XXXX";
//===========================================================

// Local (method) Variables
PreparedStatement pStmt = null;
ResultSet rs = null;
String password = new String(passtft.getPassword());
String pin = null;
String saltpin = null;
boolean viewPanel = false;

try {
    if (isclick.equals(BI.getText())) {
        conn = DriverManager.getConnection(dburl, userdb, passdb);
        pStmt = conn.prepareStatement("SELECT * FROM accounts WHERE accountnumber = ?");
        pStmt.setString(1, acountNum);
        rs = pStmt.executeQuery();
        while (rs.next()) {
            /* Check status to see if User's card was previously deactivated
               and wasn't yet reactivated by admin.   */
            String status = rs.getString("status");
            if (status.equalsIgnoreCase("deactivated")) {
                // Card is currently deactivated. Inform User
                JOptionPane.showMessageDialog(null, "<html><font color=red><b>Your Card has "
                        + "been previously Deactivated!</b></font><br><br>Please Contact our "
                        + "<font color=blue>Customer Service</font> at<br>Phone: " + 
                          customerServicePhone, "Card Deactivated", JOptionPane.ERROR_MESSAGE);
                // Get out of loop (don't bother with other data).
                break;
            }
            // Status is good so get pin and saltpin
            pin = rs.getString("pin");
            saltpin = rs.getString("saltpin");
        }

        loginAttempts++;    // Increment login counter

        /* Make sure pin and saltpin actually contain something 
           and that login attempts is less than or equal to the 
           max attempts allowed.    */
        if (pin != null && saltpin != null && loginAttempts <= maxAttemptsAllowed) {
            // Check the pin for validity
            viewPanel = PasswordUtils.verifyUserPassword(password, pin, saltpin);
            //PIN Valid!
            if (viewPanel) {
                PassPanel.setVisible(false);
                balancePanel.setVisible(true);

                // TO DO: Descriptively Log the LOGIN success in a DB Login Table.

                return; // Success. Get out of this particular method or event.
            }
            else {
                // pin is INVALID!
                JOptionPane.showMessageDialog(null, "<html>Wrong Pin Number Supplied!<br>"
                        + "You have <b><font color=red>" + (maxAttemptsAllowed - loginAttempts) + "</font>"
                        + "</b> attempt remaining.<br><br><font color=blue>Please try "
                        + "again...</font></html>", "Invalid PIN", JOptionPane.WARNING_MESSAGE);
                passtft.setText("");    // Clear JPasswordField
                passtft.requestFocus(); // Set focus to JPasswordField
                amountT = "";           // Clear Amount JTextField

                // TO DO: Descriptively Log the LOGIN failure in a DB Login Table.
            }
        }

        // If code ever reaches this point then...
        // See if passwordAttempts has reached Max login attempts
        if (loginAttempts == maxAttemptsAllowed) {
            /* The Maximum number of login attempts has been processed
               with no success so, apply deactivation!   */
            pStmt = conn.prepareStatement("UPDATE accounts SET status = ?"
                    + " WHERE accountnumber = ?");
            pStmt.setString(1, "Deactivated");
            pStmt.setString(2, acountNum);
            int i = pStmt.executeUpdate();
            System.out.println(i + " Records Updated"); // Can remove

             if (i > 0) { loginAttempts = 0; } // Zero the member variable loginAttempts

            // TO DO: Descriptively Log the Deactivation in a DB Login Table.

            JOptionPane.showMessageDialog(null, "<html><font align=justify>For safety reasons "
                    + "&nbsp;<b><font color=red>your Card has been Disabled</b></font>&nbsp; "
                    + "from<br>use since you have reached your maximum login attempts!<br><br>"
                    + "Please contact our <font color=blue>Customer Service</font> to have your "
                    + "card<br>reactivated! Phone: " + customerServicePhone + "</html>", 
                    "Card Disabled", JOptionPane.ERROR_MESSAGE);
        }
    }
}
catch (Exception ex) {
    // Inform User of an Error!
    JOptionPane.showMessageDialog(null, "<html><b><font color=red>A System Error "
        + "Has Occured!</font></b><br><br> Please contact Customer Service<br>"
        + "for assistance at: <font color=blue>" + customerServicePhone + 
        "</font></html>", "System Error!", 
        JOptionPane.ERROR_MESSAGE);
    // TO DO: Descriptively Log the Error in a DB Error Table.
}

// This finally block will always ensure 
// objects are closed if they are open.
finally {
    try {
        if (rs != null)    { rs.close();    }
        if (pStmt != null) { pStmt.close(); }
        if (conn != null)  { conn.close();  }
    }
    catch (SQLException ex) { 
        // TO DO: Descriptively Log the Error in a DB Error Table.
    }
}

由于不清楚循环在哪里,也不清楚如何调用循环,因此只能说它将名称不正确的x5设置为3,如果verify为false,则可能会将其递减。司机代码明显缺失。同时,考虑重构为紧密聚焦的更小的方法。谢谢你的帮助!我忘了评论它:我解决了我的问题 因为你: