Good afternoon. I am validating a user if it exists but I need you to show your role and ID, thank you. (MVC).
public String[] validaUser(String user, String pass) {
String sql = "SELECT \n"
+ "u.user,\n"
+ "u.password,\n"
+ "r.rol,\n"
+ "p.cedula,\n"
+ "p.nom_nom,\n"
+ "p.nom_ape\n"
+ "from users as u \n"
+ "INNER JOIN roles as r\n"
+ " ON r.id = u.id_rol\n"
+ "INNER JOIN personal as p\n"
+ " ON p.id = u.id_personal\n"
+ "WHERE u.user = ? AND u.password = ?";
try {
cn = con.getConection();
ps = cn.prepareStatement(sql);
ps.setString(1, user);
ps.setString(2, pass);
rs = ps.executeQuery();
if (!rs.next()) {
JOptionPane.showMessageDialog(null, "Usuario y/o Contraseña invalida.");
return null;
} else {
String[] cedulaRol = {rs.getString("rol"), rs.getString("cedula")};
return cedulaRol;
/*return rs.getString("rol");*/
}
} catch (HeadlessException | SQLException e) {
System.err.println("Error al traer los datos de los usuario modelo:" + e);
}
return null;
}
And here it validates if it is a teacher or an administrator but I need it to show: "Welcome: "+rol+cedula
, I don't know if I am making a mistake in the database but I have reviewed it and it looks good.
private void
txtIngresarActionPerformed(java.awt.event.ActionEvent evt) {
mUsuarios datos = new mUsuarios();
String[] rol = {"rol", "cedula"};
rol = datos.validaUser(txtLogin.getText(), txtPassword.getText());
/*rol = datos.validaUser(txtLogin.getText(), txtPassword.getText());
user = datos.validaUser(txtLogin.getText(), txtPassword.getText());*/
if (rol.equals("ADMINISTRADOR")) {
JOptionPane.showMessageDialog(null, "Bienvenido: " + rol);
vAdmin form = new vAdmin();
form.setVisible(true);
this.dispose();
} else if (rol.equals("DOCENTE")) {
JOptionPane.showMessageDialog(null, "Bienvenido: " + rol);
vDocente form = new vDocente();
form.setVisible(true);
this.dispose();
} else {
JOptionPane.showMessageDialog(null, "No existen datos");
txtLogin.setText("");
txtPassword.setText("");
txtLogin.requestFocus();
}
}
Here you return an array with two values of type
String
:Which means that the data will travel in an index
0
, the role and1
the cedula (array indices start with0
.So to read that data you have to look it up by its indices.
We will fix a few things in the code that receives the data: