Error: "Can not make a static reference to the non-static method from *"

2

Good morning,

I have the following error message. on line 23 and 24 of the UserID.class.

  

It says: Can not make a static reference to the non-static method from   UserModel.

    public class UsuarioDAO {

    public static Boolean doLogin(UsuarioDAO usuario) {

        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from usuario where nome=? and senha=?";

        try {

            // Validar
            ps = ConectarDB.getConexao().prepareStatement(sql);
            //UsuarioModel usuario = new UsuarioModel();
            ps.setString(1, UsuarioModel.getNome());
            ps.setString(2, usuarioModel.getSenha());
            rs=ps.executeQuery();

            // Validar
            if(rs.next()) {
                return true;
            } else {
                return false;
            }            

        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }

    }

}

    public class UsuarioModel {

// Variáveis
    public String nome;
    public String senha;

// Getters & Setters
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getSenha() {
        return senha;
    }
    public void setSenha(String senha) {
        this.senha = senha;
    }

    public UsuarioModel(String nome, String senha) {
        this.nome = nome;
        this.senha = senha;
    }

}
    
asked by anonymous 27.11.2015 / 15:01

2 answers

4

At line ps.setString(1, UsuarioModel.getNome()); switch to:

ps.setString(1, usuarioModel.getNome());

Note that when calling UsuarioModel.getNome() , U is uppercase, so Java will attempt to access the static method getNome() instead of the instance , which is what you probably want. That's exactly what the error message is saying.

    
27.11.2015 / 15:20
2

The following occurs:

ps.setString(1, UsuarioModel.getNome()); 

This method is not static:

I believe that what you wanted to do is to save UsuarioModel , correct?

Then you should pass it on the parameter!

It would look like this:

 public static Boolean doLogin(UsuarioModel usuarioModel) {

        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from usuario where nome=? and senha=?";

        try {

            // Validar
            ps = ConectarDB.getConexao().prepareStatement(sql);

            ps.setString(1, usuarioModel.getNome());
            ps.setString(2, usuarioModel.getSenha());
            rs=ps.executeQuery();

            // Validar
            if(rs.next()) {
                return true;
            } else {
                return false;
            }            

        } catch (SQLException ex) {
            ex.printStackTrace();
            return false;
        }

    }

I hope I have helped!

Greetings

    
27.11.2015 / 15:22