简体   繁体   中英

Java How to assign variable from array to another array

Okay, so I'm trying to do the login screen to a bank app in java swing. I have to get a user to input his login, password and ID in the login screen; The problem is that the user can input any ID and get past the login screen. BankGlowna() is the method that shows the window past the login screen. Here's the code:

        Connection c = null;
        Statement stmt = null;

        int countLogin = 0;
        int countPwd = 0;
        int countIDs = 0;

        String[] logins = new String[50];
        String[] pwds = new String[50];
        String[] ids = new String[50];

        try {
            String url = "jdbc:sqlite:D:\\SQLiteDB\\Bank.db";
            c = DriverManager.getConnection(url);
            c.setAutoCommit(false);

            stmt = c.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM Logins;");

            while (rs.next()) {
                id = rs.getString("IDLogin");
                login = rs.getString("Login");
                pwd = rs.getString("Password");

                if (countIDs < ids.length) {
                    ids[countIDs] = id;
                    countIDs++;
                }

                if (countLogin < logins.length) {
                    logins[countLogin] = login;
                    countLogin++;
                }

                if (countPwd < pwds.length) {
                    pwds[countPwd] = pwd;
                    countPwd++;
                }
            }
        }
        catch (Exception ex) {
            System.out.println(ex);
        }

        JFrame loginFrame = new JFrame("Bank");
        loginFrame.setSize(400, 400);

        JLabel loginLabel = new JLabel("Login:");
        loginLabel.setBounds(5, 10, 200, 20);

        JTextField loginText = new JTextField();
        loginText.setBounds(5, 30, 200 ,20);

        JLabel pwdLabel = new JLabel("Hasło: ");
        pwdLabel.setBounds(5, 50, 200 ,20);

        JPasswordField pwdField = new JPasswordField();
        pwdField.setBounds(5, 70, 200, 20);

        JLabel idLabel = new JLabel("ID:");
        idLabel.setBounds(5, 90, 200, 20);

        JTextField idTF = new JTextField();
        idTF.setBounds(5, 110, 200 ,20);

        JButton loginButton = new JButton("Zaloguj");
        loginButton.setBounds(5, 140, 200, 20);

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginChk = loginText.getText();
                pwdChk = new String(pwdField.getPassword());
                idChk = idTF.getText();

                for (int i = 0; i < logins.length; i++) {
                    if (loginText.getText().equals(logins[i])) {
                        for (int a = 0; a < pwds.length; a++) {
                            if (pwdChk.equals(pwds[a])) {
                                for (int c = 0; c < ids.length; c++) {
                                    if (idChk.equals(ids[c])) {
                                        idDoKonta = ids[c];
                                        loginFrame.dispose();
                                        BankGlowna();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        });

        loginFrame.setVisible(true);
        loginFrame.setLayout(null);
        loginFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        loginFrame.add(loginLabel);
        loginFrame.add(loginText);
        loginFrame.add(pwdLabel);
        loginFrame.add(pwdField);
        loginFrame.add(loginButton);
        loginFrame.add(idLabel);
        loginFrame.add(idTF);
    }```

When you are transferring the data from your database, you should be putting the information associated with each user in the same locations in your arrays, because you are on the same row. So, the password and ID associated with logins[0] are pwds[0] and ids[0] ; therefore, you can use that when you authenticate the user. Your code for the ActionListener:

@Override
            public void actionPerformed(ActionEvent e) {
                loginChk = loginText.getText();
                pwdChk = new String(pwdField.getPassword());
                idChk = idTF.getText();

                for (int i = 0; i < logins.length; i++) {
                    if (loginChk.equals(logins[i])) {
                        // Then EVERYTHING should be at index i
                        if (pwdChk.equals(pwds[i]) && idChk.equals(ids[i])) {
                           // user authenticated
                           idDoKonta = ids[i];
                           loginFrame.dispose();
                           BankGlowna();
                           return; // leave the function so you don't keep iterating through the loop
                        }
                    }
                }
            }

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