0

I created a java application where login credentials are required to have access. I recorded the information about username in ArrayList.

private void jBLoginActionPerformed(java.awt.event.ActionEvent evt) {                                        

    String sql="select * from logins where username=? and password=? and idTypeLogin=?";
    try{
        pst=(PreparedStatement) conexao.prepareStatement(sql);
        pst.setString(1, jTUser.getText());
        pst.setString(2, jPass.getText());
        pst.setString(3, jComboBoxTipoLogin.getSelectedItem().toString());

        rs=pst.executeQuery();
        if(rs.next()){

            String idTypeLogin= rs.getString("idTypeLogin"); 

            if (idTypeLogin.equals("Administrator")) {

                jTTaskAdmin ah = new jTTaskAdmin();
                ah.setVisible(true);

            }
             else {
                jTTaskTecnic eh = new jTTaskTecnic();
                eh.setVisible(true);

            }
            this.setVisible(false);
            }
        else{
            JOptionPane.showMessageDialog(null, "User Invalid");
              }
                //close();
            }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
            }



        List<String> loggedInUsers = new ArrayList<String>();
     loggedInUsers.add(jTUser.getText())

            List<String> t = loggedInUsers;   

           jTTasktecnic.recordVar=loggedInUsers.get(0);  
   }         

enter image description here

And I add this code in second form

static String recordVar;

How can I do to get the information that was recorded in the array, but in another form. That is, I recorded the information in ArrayList in the Login form, now I want to get this information in another form.

How can I do this?

Thank you

strange_098
  • 1,261
  • 6
  • 24
  • 44
  • Hey .. jUst try like this `yourList.get(0)` if the list having one element.. Check out for the documentation. – Rookie007 Apr 18 '14 at 08:37

3 Answers3

1

You can do it Like this

Form two

Declare A variable where you want to store ArrayList Item(May be String or Int or something else)

You can also declare it static.

But declare it Globally (Means at start of the class not inside method or something else)

Form One

Store ArrayList Item in Variable of Form 2 OnClick of logIn button.

Form2 ob=new Form2();
ob.recordVar=arrayList.get(3);//it will access recordVar of Form2 3rd element of ArrayList for example

Or you can declare Form2 variable static and acces it via class name.

Like this

Form2.recordVar=arrayList.get(3);
akash
  • 22,664
  • 11
  • 59
  • 87
0

define ArrayList as global so you can access it whenever you want.

Jay M
  • 103
  • 1
  • 1
  • 8
0

Let your method return the loggedInUsers and use it where you want. Change the method signature accordingly. Global variables are notorious. It is better to reduce the scope to where it is just required

private List<String> jBLoginActionPerformed(java.awt.event.ActionEvent evt) 
{
   // do samething
   return t
}
Shiva Kumar
  • 3,111
  • 1
  • 26
  • 34