Authentification

Résolu/Fermé
mariya2016 Messages postés 30 Date d'inscription dimanche 13 mars 2016 Statut Membre Dernière intervention 19 mai 2016 - 18 avril 2016 à 01:00
mariya2016 Messages postés 30 Date d'inscription dimanche 13 mars 2016 Statut Membre Dernière intervention 19 mai 2016 - 19 avril 2016 à 21:42
Bonjour,
svp c'est urgent j'ai un problème dans mon code d'authentification et j'arrive pas à le régler en essayant plusieurs codes.
Voici le code
class Authentificat
package authen;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;



	/* Cette classe est l'interface d'affichage de la Connexion   */

public class Authentification extends JFrame {
	
	JLabel login,mdp;
	JTextField login1;
	JPasswordField mdp1;
	static JButton valider; //j'ai changé valider à static..
	JButton annuler;
	
		
	public Authentification(){
		
		super();
		this.setTitle(" Domotique ");
		this.setSize(new Dimension(400,200));
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		
		
		login = new JLabel("Login");
		login1 = new JTextField();
		
		mdp = new JLabel("Mot de Passe");
		mdp1 = new JPasswordField();
		
		valider = new JButton("Valider ");
		annuler = new JButton(" Annuler");
		
		
		Container contenu = this.getContentPane();
		contenu.setLayout(null);
		
		contenu.add(login);
		login.setBounds(20, 20, 100, 20);
		
		contenu.add(login1);
		login1.setBounds(150, 20, 150, 20);
		
		contenu.add(mdp);
		mdp.setBounds(22, 55, 100, 20);
		
		contenu.add(mdp1);
		mdp1.setBounds(150, 55, 150, 20);
		
		contenu.add(valider);
		valider.setBounds(125,100 ,77 ,20 );
		
		contenu.add(annuler);
		annuler.setBounds(225, 100, 82, 20);
		
		valider.addActionListener(new ValiderListener());	
		
		this.setVisible(true);
	
	}
		


		class ValiderListener extends JFrame implements ActionListener{
		
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent e) {
//ActionEvent a;
					try {
						
					} catch (Exception e2) {
						
					}
			}
		
			
		}
	
		public static void main(String[] args){
			Authentification a = new Authentification();
 a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 TraitementAuthentification tr= new TraitementAuthentification();
		     valider.addActionListener(tr);
		     
		}
		

}	




la classe Base
package authen;

import java.sql.Connection;
import java.sql.DriverManager;
 
 
public class Base {
     
    public Connection conn;
     
    public void connexionBD(){
         
         
        /*  Chargement de la Base de données  */
            try{
                 //
                Class.forName("oracle.jdbc.driver.OracleDriver");
                 
            } catch (Exception e) {
                 
                System.out.println(" Erreur de chargement de la Base de données");
                e.getMessage();
                System.exit(0);
                 
            }
         
         
            /*    Connexion de la Base de données  */
            try {
                 
                String url = "jdbc:oracle:thin:system/orcl1@localhost:1521:XE";
                String user = "admin";
                String passwd ="admin";
                conn = DriverManager.getConnection(url, user, passwd);
                 
            } catch (Exception e) {
                System.out.println(" Erreur de Connexion à la Base de données ");
            }  
             
        }
     
            /*   */
            public Connection getConnect(){
                 
                return conn;
            }
     
             
            /*   Deconnexion  */
            public void Deconnexion(){
             
                try {
                    conn.close();
                     
                } catch (Exception e) {
                    System.out.println(" Déconnexion Impossible ");
                }
            }
         
             
             
             
}


la classe TraitementAuthentification
package authen;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import javax.swing.JOptionPane;
import javax.swing.JTextField;
 
/*  Traitement à faire   */
 
public class TraitementAuthentification implements ActionListener {
 
    public Base b = new Base();
    public Connection conn;
     
    static JTextField login1,passwd ;
     
    PreparedStatement statement = null;
     
    ResultSet resultat;
     
    public TraitementAuthentification(){
         
    }
 
 
    @Override
    public void actionPerformed(ActionEvent e) {
        String login = login1.getText(); 
        String password = passwd.getText();
         
        b.connexionBD();
        conn = b.getConnect();

            try{
                statement = (PreparedStatement) conn.createStatement();
                String sql = "SELECT password FROM authentification WHERE login ='"+login+"'";
                resultat = statement.executeQuery(sql);
                 
                if(resultat.next()){
                     
                    String motDePasse = resultat.getString(1);
         
                if(motDePasse.equals(password)){
             
                    JOptionPane.showMessageDialog(null,"Connexion réussie ! ","Success",JOptionPane.PLAIN_MESSAGE);
                }else {
                     
                    JOptionPane.showMessageDialog(null,"Mot de passe incorrect ! ","Error",1);
                }
                }else {
                     
                    JOptionPane.showMessageDialog(null,"Login incorrect ! ","Error",1);
                }
 
                    conn.close();
         
            }
  catch (SQLException e4) {
             
                System.out.println(e4.getMessage());
            }
            
        }
     
}




ça m'affiche plein d'erreurs.
Merci.

1 réponse

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
18 avril 2016 à 12:13
Bonjour,

"ça m'affiche plein d'erreurs"
Lesquelles ?

Remarque :

} catch (Exception e) {
    System.out.println(" Erreur de chargement de la Base de données");
    e.getMessage();
    System.exit(0);
}

Jamais il ne faudrait faire comme ça...
Si tu veux arrêter ton programme parce qu'il génère une exception alors ça ne sert à rien de la rattraper, tu enlèves le try/catch et tu mets un throws sur la méthode.

De plus
e.getMessage()
envoie un String, donc si tu ne l'affiches pas, ça ne sert à rien...

Eventuellement tu peux faire de l'enrichissement d'exception, comme ceci :

} catch (Exception e) {
    throw new IllegalStateException("Erreur de chargement de la Base de données", e);
}

Il ne faut pas oublier le
, e);
puisque c'est lui qui informe sur l'origine de l'exception.

Idem on préférera avoir
e4.printStackTrace()
plutôt que
System.out.println(e4.getMessage());
pour conserver l'origine de l'exception, sinon le message d'erreur est quasiment inexploitable.
2
mariya2016 Messages postés 30 Date d'inscription dimanche 13 mars 2016 Statut Membre Dernière intervention 19 mai 2016
18 avril 2016 à 20:15
Bonjour,
Merci pour votre réponse, je vais essayer ce que vous m'avez dit mais avant de les corriger voilà ce que j'ai comme erreurs:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at authen.TraitementAuthentification.actionPerformed(TraitementAuthentification.java:33)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
0
mariya2016 Messages postés 30 Date d'inscription dimanche 13 mars 2016 Statut Membre Dernière intervention 19 mai 2016
18 avril 2016 à 20:23
bonsoir, j'ai corrigé ce que vous m'avez dit mais les mêmes erreurs s'affichent encore .
0
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
19 avril 2016 à 00:51
"j'ai corrigé ce que vous m'avez dit mais les mêmes erreurs s'affichent encore"
C'est normal, je t'indiquais juste comment obtenir plus d'informations sur les erreurs, je ne prétendais en rien les corriger puisque je ne savais pas ce qu'elles étaient...

java.lang.NullPointerException 
at authen.TraitementAuthentification.actionPerformed(TraitementAuthentification.java:33) 

Ca c'est déjà beaucoup plus précis, je reprends ton code de la classe TraitementAuthentification :

public void actionPerformed(ActionEvent e) { // ligne 32
    String login = login1.getText(); // ligne 33

Tu as un NullPointerException car login1 vaut null...
Tu as du confondre le login1 de de la classe Authentification (qui est initialisé) avec celui de la classe TraitementAuthentification (qui n'est jamais initialisé).

Il faudrait que tu passes en argument un objet Authentification à ton constructeur de TraitementAuthentification sinon les deux objets ne vont pas pouvoir agir l'un avec l'autre.

Authentification a = new Authentification();
TraitementAuthentification tr = new TraitementAuthentification(a);

private JTextField login1, passwd ; // pas static ici, ça n'a pas de sens !

public TraitementAuthentification(Authentification a) {
    login1 = a.login1;
    passwd = a.mp1;
}
0
mariya2016 Messages postés 30 Date d'inscription dimanche 13 mars 2016 Statut Membre Dernière intervention 19 mai 2016
19 avril 2016 à 21:42
Merci bcp :) c'est bon j'ai réglé le problème :)
0