Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Pedroalves

[Resolvido] Login

Recommended Posts

não estou a conseguir por a funcionar o login com restrições segue-se o codigo

    public void LOGIN() {

try {

String driverName = "org.gjt.mm.mysql.Driver";
Class.forName(driverName);
// Create a connection to the database
String id_BD = "gest";
String IP = "127.0.0.1";
String URL = "jdbc:mysql://" + IP + "/" + id_BD;
String username = "root";
String password = "123456";
conn = DriverManager.getConnection(URL, username, password);
} catch (ClassNotFoundException e) {
System.out.println("Erro no driver de ligação:"+e.getMessage());
} catch (SQLException e) {
System.out.println("Erro de SQL:"+e.getMessage());
}

try {
       Statement stmt = conn.createStatement();
       String user="";
       String pass="";
       String tipo="";
       user=this.USERNAME.getText();
       pass=this.PASSWORD.getText();

       try  {

      MessageDigest MD5 = MessageDigest.getInstance("MD5");

 MD5.update(pass.getBytes());
 BigInteger hash = new BigInteger(1, MD5.digest());
 pass = hash.toString(16);
} catch(NoSuchAlgorithmException ex)  {
 ex.printStackTrace();
}
String query = "SELECT password,tipo FROM user where username='"+user+"';";
       ResultSet rset = stmt.executeQuery(query);
       if(rset.next()){
       if(rset.getString(1).equals(pass)&&(rset.getString(11).equals("1"))){

this.MENUPRINCIPAL.setVisible(true);
this.MENUPRINCIPAL.setSize(434, 336);
this.MENUPRINCIPAL.setBounds(300, 400, 300, 200);
this.getFrame().setVisible(false);
}
if(rset.getString(1).equals(pass)&&(rset.getString(11).equals("2"))){

this.MENUPRINCIPALFUNCIONARIO.setVisible(true);
this.MENUPRINCIPALFUNCIONARIO.setSize(434, 336);
this.MENUPRINCIPALFUNCIONARIO.setBounds(300, 400, 300, 200);
this.getFrame().setVisible(false);
}
else{
  this.ERRO.setVisible(true);
this.ERRO.setSize(648, 558);
   this.getFrame().setVisible(false);
}
       }
       else{
        this.ERRO.setVisible(true);
this.ERRO.setSize(648, 558);
   this.getFrame().setVisible(false);
       }

       }
catch (Exception e){
System.out.println("Erro Ao executar a query: "+ e.getMessage());
this.jLabel15.setText("ERRO NA CONECÇÃO");
}
   }

Compartilhar este post


Link para o post
Compartilhar em outros sites

Isso não funciona:

if(rset.getString(1).equals(pass)&&(rset.getString(11).equals("1")))

 

A sua consulta retorna um conjunto de apenas duas colunas. Dessa forma você só conseguirá recuperar do índice 1 e 2 respectivamente.

if(rset.getString(1).equals(pass)&&(rset.getString(2).equals("1")))

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.