Problem to pass a variable from one form to another

Asked

Viewed 96 times

0

beauty? I have a problem with a login of a system that I am creating, in which according to the type of user of the client, it will open the next form with some visible components or not. When I give a Sout to the variable in the login form (which receives the "type" field of the database) it appears, now if I do the same already in the menu form, ends up giving "null" in the terminal ie, the variable is not transiting. Detail is that when I first wrote the code worked, what happens here??? Note: I am a computer technician, I have basic/intermediate knowledge about Java, I wanted your opinion and what would you do to solve. Thanks in advance!

Login form code(I called the try method on the login button and the Keyevent event):

private final Connection connection;

public static String tipo;

public void tentativa() {

    String login = txt_usuario.getText().trim();

    try {


            String sql = "SELECT tipo FROM usuario WHERE login = '" + login + "'";

            PreparedStatement stmt = connection.prepareStatement(sql);

            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                tipo = rs.getString("tipo");
                System.out.println(tipo);
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
}

Menu form code:

public String tipo = "";

public String getTipo() {
    return tipo;
}

public void setTipo(String tipo) {
    this.tipo = tipo;
}

/**
 * Creates new form form_menu
 */
public form_menu() {
    initComponents();
    this.setTipo(form_login.tipo);
    System.out.println(tipo);
}
  • 1

    In the past, most java programs used JDBC but JPA came to make it easier because it doesn’t use JPA? when it comes to your code it is incomplete. see this: https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-ask-questions? cb=1

  • After the login is done, there form_menu() if you do this System.out.println(Connection.tipo) what the result?

1 answer

0

The ideal would be to pass the type value when starting the Menu form, so your Login form would call the Menu form and pass as parameter to the constructor the type value, the code would look like this:

Login form code(I called the try method on the login button and the Keyevent event):

private final Connection connection;

public static String tipo;

public void tentativa() {

    String login = txt_usuario.getText().trim();

    try {


            String sql = "SELECT tipo FROM usuario WHERE login = ?";

            PreparedStatement stmt = connection.prepareStatement(sql);

            stmt.setString(1,login);

            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                tipo = rs.getString("tipo");
                JFormMenu menu = new Menu(tipo);
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
}

Menu form code:

public String tipo = "";

public String getTipo() {
    return tipo;
}

public void setTipo(String tipo) {
    this.tipo = tipo;
}

/**
 * Creates new form form_menu
 */
public form_menu(String tipoRecebido) {
    initComponents();
    this.setTipo(tipoRecebido);
    System.out.println(tipo);
}

I recommend reading this devmedia article.

  • 1

    Just one observation, it is not comfortable this sql query contacted, since it gives to cheat with an sql Injection. The ideal is to do via stmt.setString(1,"login")

  • 1

    I edited the answer to be in the most appropriate form.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.