Urgent : le code de source de authentificatio

Fermé
noor - 6 mars 2009 à 17:01
ezo_es Messages postés 100 Date d'inscription dimanche 1 mars 2009 Statut Membre Dernière intervention 16 mars 2010 - 7 mars 2009 à 22:20
Bonjour,
svp je travaille une application en language java +bd access et j'en ai besoin d'une authentification : Nom d'utilisateur et mot de passe et se connecter je veux le code source de connexion pour que l'utilisateur puisse se connecter ok merci d'avance.

8 réponses

ezo_es Messages postés 100 Date d'inscription dimanche 1 mars 2009 Statut Membre Dernière intervention 16 mars 2010 9
6 mars 2009 à 17:04
Bonjour
est ce que tu as deja ton jframe avec le formulaire et une table connexion qui contient les champs logine et mot de passe dans ta base de donnees?
0
oui j'ai une table sur access connexion qui as deux champs login et mot de pass bon je veux donner juste le droit de se connecter à l'utilisateur non au admin ok stp aidez moi et merci
0
ezo_es Messages postés 100 Date d'inscription dimanche 1 mars 2009 Statut Membre Dernière intervention 16 mars 2010 9
6 mars 2009 à 17:16
de toute maniere moi aussi j'ai fais de l'authentification dans mon application
j'ai une table connexion dans ma base avec 3 champs (login,pass,type)
parcque je veux donner l'acces au admin et non au user

et voila le code de ma classe

/*
* Acces.java
*
* Created on 4 mars 2009, 09:51
*/

package Frame;


import java.awt.Color;
import java.awt.Cursor;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import Connexion.BDConnection;
import java.awt.Color;
import java.awt.Cursor;

import java.sql.Connection;
import java.sql.ResultSet;
import javax.swing.JOptionPane;

/**
*
* @author Zineb
*/
public class Acces extends javax.swing.JFrame {

public static String loginDeplace;
public static String TypeUser="user";
/** Creates new form Acces */
public Acces() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel3 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
txtLogin = new javax.swing.JTextField();
txtPassword = new javax.swing.JPasswordField();
btnAnnuler = new javax.swing.JButton();
btnConnecter = new javax.swing.JButton();
jSeparator1 = new javax.swing.JSeparator();
jSeparator2 = new javax.swing.JSeparator();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 14));
jLabel3.setText("Authentification");

jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 14));
jLabel1.setText("Login :");

jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14));
jLabel2.setText("Password :");

txtLogin.setColumns(13);

txtPassword.setColumns(13);

btnAnnuler.setFont(new java.awt.Font("Times New Roman", 1, 12));
btnAnnuler.setText("Annuler");
btnAnnuler.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAnnulerActionPerformed(evt);
}
});

btnConnecter.setFont(new java.awt.Font("Times New Roman", 1, 12));
btnConnecter.setText("Connecter");
btnConnecter.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
btnConnecterMouseClicked(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(btnConnecter, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 138, Short.MAX_VALUE)
.addComponent(btnAnnuler, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jLabel2)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
.addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
.addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(80, 80, 80)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(63, 63, 63)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 76, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(1, 1, 1))))
.addGap(80, 80, 80)))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(47, 47, 47)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(jLabel1)
.addGap(26, 26, 26)
.addComponent(jLabel2))
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 144, Short.MAX_VALUE)
.addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnAnnuler, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnConnecter, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(25, 25, 25))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(jLabel3)
.addGap(99, 99, 99)
.addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(163, Short.MAX_VALUE)))
);

pack();
}// </editor-fold>

private void btnAnnulerActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

private void btnConnecterMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
try{
if(txtLogin.getText().isEmpty() || txtPassword.getText().isEmpty())
JOptionPane.showMessageDialog(null,"Veuillez remplir tous les champs.", "Message erreur", JOptionPane.ERROR_MESSAGE);
else {
String login = txtLogin.getText();
String pass = txtPassword.getText();
Connection conn = BDConnection.connecter();
try{
Statement st = (Statement) conn.createStatement();

ResultSet rs = st.executeQuery("select * from connexion where login like '"+login+"' and Pass like '"+pass+"';");

if(rs.next()){
if(rs.getString("type").equals("admin")){
TypeUser="admin";
this.setVisible(false);
new accueil().setVisible(true);

}else{
TypeUser="user";
this.setVisible(false);
new accueil().setVisible(true);

}
}else{
txtPassword.setText("");
JOptionPane.showMessageDialog(null, " Compte ou mot de passe incorrect!!","Erreur",JOptionPane.ERROR_MESSAGE);
}
}finally{BDConnection.deconnecter();}
}
} catch(Exception e ){/*JOptionPane.showMessageDialog(null, "Erreur dans btnConnecterActionPerformed() :"+e.getMessage());*/}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Acces().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton btnAnnuler;
private javax.swing.JButton btnConnecter;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JTextField txtLogin;
private javax.swing.JPasswordField txtPassword;
// End of variables declaration

}
0
ezo_es Messages postés 100 Date d'inscription dimanche 1 mars 2009 Statut Membre Dernière intervention 16 mars 2010 9
6 mars 2009 à 22:07
Bon voila dans ce cas la tu rajoute un champs"type" dans ta table connexion sur access dans tu designe est ce que c'est admin ou user

et dans l'evenement de ton button tu mets ce code

try{
if(txtLogin.getText().isEmpty() || txtPassword.getText().isEmpty())
JOptionPane.showMessageDialog(null,"Veuillez remplir tous les champs.", "Message erreur", JOptionPane.ERROR_MESSAGE);
else {
String login = txtLogin.getText();
String pass = txtPassword.getText();
Connection conn = BDConnection.connecter();
try{
Statement st = (Statement) conn.createStatement();

ResultSet rs = st.executeQuery("select * from Pass where login like '"+login+"' and passs like '"+pass+"';");

if(rs.next()){
if(rs.getString("type").equals("admin")){
TypeUser="admin";
this.setVisible(false);
new accueil().setVisible(true);

}else{
TypeUser="user";
this.setVisible(false);
new accueil().setVisible(true);

}
}else{
txtPassword.setText("");
JOptionPane.showMessageDialog(null, " Compte ou mot de passe incorrect!!","Erreur",JOptionPane.ERROR_MESSAGE);
}
}finally{BDConnection.deconnecter();}
}
} catch(Exception e ){/*JOptionPane.showMessageDialog(null, "Erreur dans btnConnecterActionPerformed() :"+e.getMessage());*/}

Bonne chance
0
noor14 Messages postés 2 Date d'inscription vendredi 6 mars 2009 Statut Membre Dernière intervention 6 mars 2009
6 mars 2009 à 23:00
j'ai fais tout ça mais ca n a rien donnée :s:s
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
ezo_es Messages postés 100 Date d'inscription dimanche 1 mars 2009 Statut Membre Dernière intervention 16 mars 2010 9
6 mars 2009 à 23:06
est ce que tu arrive deja a te connecter a ta base de donnee?
et n'oublie pas que les variable que tu utilise doivent etre identique que celle que tu as sur ta table
sinon dis moi exactemnt ceque tu as fais
0
noor14 Messages postés 2 Date d'inscription vendredi 6 mars 2009 Statut Membre Dernière intervention 6 mars 2009
6 mars 2009 à 23:42
voila le code de connexion :
import java.sql.*;
import java.sql.DriverManager;
public class connection {

public static Connection con;
public static Connection connect(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=(Connection) DriverManager.getConnection("jdbc:odbc:Gestion");
} catch (ClassNotFoundException e) {
System.out.println("impossible");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("Driver introuvable : ");
e.printStackTrace();
}

return con;

}
}










et bien sur j'ai ajouter les champs que tu m'as dis sur la table access

puis j'ai ajouter l'evenement du boutton :

private JButton getJButton() throws SQLException {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(162, 149, 142, 32));
jButton.setText("Se connecter");
try{
if(jTextField .getText().isEmpty() ||jTextField1 .getText ().isEmpty())
JOptionPane.showMessageDialog(null,"Veuillez remplir tous les champs.", "Message erreur", JOptionPane.ERROR_MESSAGE);
else {
String login = jTextField.getText();
String pass = jTextField1.getText();
Connection conn = Authentification.connecter();
try{
Statement st = (Statement) conn.createStatement();

ResultSet rs = st.executeQuery("select * from Pass where login like '"+login+"' and passs like '"+pass+"';");

if(rs.next()){
if(rs.getString("type").equals("utilisateur")){
type="user";
this.setVisible(false);
new Accueil().setVisible(true);

}else{

}
jTextField1.setText("");
JOptionPane.showMessageDialog(null, " Compte ou mot de passe incorrect!!","Erreur",JOptionPane.ERROR_MESSAGE);
}
}finally{ Authentification.deconnecter();}
}
} catch(Exception e ){/*JOptionPane.showMessageDialog(null, "Erreur dans btnConnecterActionPerformed() :"+e.getMessage());*/}


}
return jButton;
}
0
ezo_es Messages postés 100 Date d'inscription dimanche 1 mars 2009 Statut Membre Dernière intervention 16 mars 2010 9
6 mars 2009 à 23:53
dans la requete il faut mettre le nom de ta table avec tes champs pas ce que je t envoyer ce n'est qu'un exemple
0
oui bien sur que j'ai changée les noms et les champs en tt cas je vs remercie bcp pr votre aide
0
ezo_es Messages postés 100 Date d'inscription dimanche 1 mars 2009 Statut Membre Dernière intervention 16 mars 2010 9
7 mars 2009 à 22:20
il n ya pas de souci
0