Search code examples
javamysqlswingjframejtextfield

How can one test user input from a JPanel?


I am trying to form a simple login database with SQL (mySQL workbench) and Java (Eclipse). I am not sure how to test the string input (JTextField and JPasswordField) against the database in SQL on even against any other strings.

I would like Java to print a statement out if the login credentials are equal to specified strings. Here is my current code:

import javax.swing.*;
import java.sql.*;


public class Hello1 extends JFrame {




private static final long serialVersionUID = 1487932324102279819L;


public static void main(String[] args) {

    JFrame frame = new JFrame("Frame Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(350,200);



    JPanel panel = new JPanel();
    frame.add(panel);
    placeComponents(panel);

    frame.setVisible(true);

    String username0 = "java";
    String password = "password";

    Statement stmt = null;
    try{
        String url = "jdbc:mysql://localhost:3306/javabase?useSSL=false";
        Connection connection = DriverManager.getConnection(url, username0, password);
        stmt = connection.createStatement();
        ResultSet rs;
        rs = stmt.executeQuery("SELECT * from userids ");
        while(rs.next()) {
            String user = rs.getString("username");
            String pass = rs.getString("paswrd");
            System.out.println(user);
            System.out.println(pass);
        }
        connection.close();

        }

        catch (Exception e) {
             System.err.println("Got an exception! ");
             System.err.println(e.getMessage());
        }
     }



private static void placeComponents(JPanel panel) {

    panel.setLayout(null);
    JLabel userLabel = new JLabel("Username");
    userLabel.setBounds(10,20,80,25);
    panel.add(userLabel);

    JTextField userText = new JTextField(20);
    userText.setBounds(100, 20, 165, 25);
    panel.add(userText);
    String user = userText.getText();       

    JLabel passwordLabel = new JLabel("Password");
    passwordLabel.setBounds(10,50,80,25);
    panel.add(passwordLabel);

    JPasswordField passwordText = new JPasswordField(20);
    passwordText.setBounds(100,50,165,25);
    panel.add(passwordText);
    String pass = passwordText.getSelectedText();

    if(user.equals('b') && pass.equals('t')){
        System.out.println("Correct Login");
    }
    System.out.println(pass);

    JButton loginB = new JButton("Login");
    loginB.setBounds(10, 80, 80, 25);
    panel.add(loginB);


}

}

I have tried getting the text in each userText/passwordText by using the .getText() and .getSelectedText() methods.


Solution

  • If I understand correctly, then you need to get password when you click on the Login button.

    Please add below code in placeComponents method after initializing the button.

    loginB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                char[] pass = passwordText.getPassword();
                System.out.println(pass);
            }
        });
    

    Action listeners are probably the easiest — and most common — event handlers to implement. You implement an action listener to define what should be done when an user performs certain operation.

    You can read more on ActionListener here.

    You can refer this example to see how to use password field.