0
Well I have 2 classes the User class and the Request class
my user class is like this:
public class Usuario {
private static Usuario instance;
private Long id;
private String login;
private String senha;
public Usuario(Long id, String login, String senha){
this.id=id;
this.login=login;
this.senha=senha;
}
public Usuario(String login, String senha){
this.login=login;
this.senha=senha;
}
public Usuario(Long id){
this.id = id;
}
public static Usuario getInstance() {
if (instance == null)
instance = new Usuario();
return instance;
}
I created a constructor to store the ID, first doubt how I could apply this to my login validation method to save the ID: my validation method:
public void validaLogin(){
UsuarioDAO dao = new UsuarioDAO();
List<Usuario> usuarios = dao.getList();
Usuario usuario = Usuario.getInstance();
for(int x = 0; x< usuarios.size(); x++){
if(jLogin.getText().equals(usuarios.get(x).getLogin()) && jSenha.getText().equals(usuarios.get(x).getSenha())){
Principal pr = new Principal ();
x = usuarios.size();
fecha();
try {
pr.start(new Stage());
} catch (Exception ex) {
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}
} else{
if(x == usuarios.size()-1){
Alert al = new Alert(Alert.AlertType.ERROR);
al.setHeaderText("Login Invalido");
al.show();
}
}
}
}
the User:
public List<Usuario> getList(){
List<Usuario> usuarios = new ArrayList<>();
String sql = "SELECT * FROM usuario";
try {
PreparedStatement stmt = con.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while(rs.next()){
Usuario u = new Usuario();
u.setId(rs.getLong("id_usuario"));
u.setLogin(rs.getString("login"));
u.setSenha(rs.getString("senha"));
usuarios.add(u);
}
stmt.close();
rs.close();
con.close();
} catch (SQLException ex) {
return null;
}
return usuarios;
}
I wanted you to keep the id on all the Trials that are opened as I would get it?
vlw man, I had seen that you study in uesc, I’m from Itabuna .
– Felipe
There’s no way you can tell me if this part of the code is correct ?
– Felipe
public user validateSenha(User u){ String sql = "SELECT id_usuario, name FROM user Where login = ? and password = ?" ;
 try {
 PreparedStatement stmt = con.prepareStatement(sql);
 ResultSet rs = stmt.executeQuery();
 u.setLogin(rs.getString("login"));
 u.setSenha(rs.getString("senha")); 
 if (rs.next()){
 Username = new User(rs.getLong("id_usuario")); userLog.setName(rs.getString("name")); // will even display login? user Return; }
– Felipe
and have you give an example how I would create this global variable? It would be in the USER class is not?
– Felipe
The "global" variable would be a variable declared as Static in the same class where vc has the main method. I don’t know if the code is 100% correct, I can’t test it, but the syntax is the one I put in. I graduated from UESC myself, but I don’t live there anymore. Greetings
– Gustavo Fragoso
i am using javafx and I have some screens, my doubt was in which class declare this global variable, ex I have login screen/main/register/etc
– Felipe