0

When the user login, the login frame close and open another frame where I need the user login details to get the others details. My prepareStatement & resultSet are already set at the top. Still a newbie in java

login action listener

btnLogin.addActionListener(( ActionEvent ae) -> {
    if (ae.getSource().equals(btnLogin)) {

    DB_connection db_connection = new DB_connection(); 
    String username = username_login_txtf.getText();
    String password = password_login_txtf.getText();

    try {   

     String sql = "SELECT * FROM user_profile as up WHERE up.username=? AND up.user_password=?";

     preStatement = db_connection.connect().prepareStatement(sql);

     preStatement.setString(1, username);
     preStatement.setString(2, password);

     res = preStatement.executeQuery();

     boolean login = false;

        while(res.next()) {         
             if(res.getString("username").equalsIgnoreCase(username) && res.getString("user_password").equalsIgnoreCase(password)) {

        JOptionPane.showMessageDialog(loginPanel, "You have Logged in!");

        login = true;

        fr.dispose();
        Main_menu.main(null);
        break;

        } ...

My other frame where i need to retrieve that specific user login data

    btnSearch.addActionListener(( ActionEvent ae) -> {
        if (ae.getSource().equals(btnSearch)) {

      // Right now i am getting the username value from another text field that I have set in this form but I want to retrieve that value directly form the login frame
            String get_username = username_profile_txtf.getText();

                DB_connection db_connection = new DB_connection(); 

                try{

                    preStatement = db_connection.connect().prepareStatement("Select * from user_profile where username = ?"); 

       // here I need the user login username from the login form
                    preStatement.setString(1, get_username);

                    res = preStatement.executeQuery();

                    if (res.next()){

                    String username = res.getString("username");
                                username_profile_txtf.setText(username);

                        ...
                        }

                    preStatement.close();
                    res.close();

                } catch(SQLException ex){

                    System.out.println(ex.getMessage());
                } finally {
                        db_connection.disconnect();
                }
            }
        });
  • Does your login frame create and show the other frame? Or are they both created from some other class? – Nicholas Hirras Aug 13 '17 at 12:19
  • when the user login, the login frame close and open another frame which has a button where i need the user's username – John Laimuin Aug 13 '17 at 12:27
  • When the user log in, pass the username to the second frame as an attribute. Either through a constructor or a setter. – Nicholas Hirras Aug 13 '17 at 13:06
  • Could you explain with an example for clarity – John Laimuin Aug 13 '17 at 13:26
  • Sure. Right now you're probably creating your second frame like this: `MySecondFrame secondFrame = new MySecondFrame()`. You could add a parameter to the constructor so you can pass the username in. Such as `MySecondFrame secondFrame = new MySecondFrame(username);` You would need to add a matching constructor such as `public MySecondFrame(String username) {this.username = username;}` – Nicholas Hirras Aug 13 '17 at 13:49
  • Another example using setters after creation: https://stackoverflow.com/questions/17412498/pass-values-entered-in-one-jframes-text-field-as-an-input-parameter-in-other-jf – Nicholas Hirras Aug 13 '17 at 13:51

1 Answers1

0

Follow the MVC-pattern.

Create a model-layer which is merrily a bunch of stupid DTO classes enhanced with some infrastructure to register Listener on changes withing the model. Create a store for username/password there.

Pass the model as dependency to any GUI class, so that all work against the same instance. (Do not use the Singelton Pattern to access the model.)

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51