1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import javax.swing.*;
public class Authentification extends JFrame {
static JTextField jTextFieldLogin = null;
JPasswordField jPasswordField = null;
JLabel jLabelLogin = null;
JLabel jLabelPassword = null;
JButton jButtonConnection = null;
JPanel jPanelFond = null;
JButton jButtonReturnPass = null;
public Authentification()
{
super("Login GPI");
fenetreLogin();
}
public void centerScreen() {
Dimension dim = getToolkit().getScreenSize();
Rectangle abounds = getBounds();
setLocation((dim.width - abounds.width) / 2,
(dim.height - abounds.height) / 2);
super.setVisible(true);
requestFocus();
}
public void fenetreLogin()
{
// appel des différentes classes que l'ont va utiliser dans l'application (principalement des appels de fenêtres JDialog)
//final Pass_perdu fenPassPerdu = new Pass_perdu(null);
String titreGestion = "Gestion Des Sites";
String titrePass = "Password Perdu ?";
final Pass_perdu fenPassPerdu = new Pass_perdu(new javax.swing.JFrame(), titrePass, true);
final Acceuil fenAcceuil = new Acceuil(new javax.swing.JFrame(), titreGestion, true);
// zone correpondant au label Login
jLabelLogin = new JLabel();
jLabelLogin.setBounds(new java.awt.Rectangle(200,115,110,16));
jLabelLogin.setText("Nom d'utilisateur");
getContentPane().add(jLabelLogin);
// Zone correspondant au label password
jLabelPassword = new JLabel();
jLabelPassword.setBounds(new java.awt.Rectangle(200,159,110,16));
jLabelPassword.setText("Mot de passe");
getContentPane().add(jLabelPassword);
// Zone correspondant au textfield Login
jTextFieldLogin = new JTextField();
jTextFieldLogin.setBounds(new java.awt.Rectangle(344,110,90,30));
getContentPane().add(jTextFieldLogin);
// Zone correspondant au password field password
jPasswordField = new JPasswordField();
jPasswordField.setBounds(new java.awt.Rectangle(344,153,90,30));
getContentPane().add(jPasswordField);
// Zone correspondant au bouton de connection
jButtonConnection = new JButton();
jButtonConnection.setBounds(new java.awt.Rectangle(277,211,98,22));
jButtonConnection.setText("Connection");
getContentPane().add(jButtonConnection);
jButtonConnection.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// Chargement du driver JDBC
try
{
System.out.println("debut");
Class.forName("org.postgresql.Driver").newInstance(); // chargement du driver jdbc pour MySQL
System.out.println("Chargement du driver JDBC");
System.out.println("fin");
}
catch (Exception b) // si le chargement du driver est impossible alors on lindique à lutilisateur
{
System.err.println("Chargement du pilote impossible.");
System.err.println(b);
}
String log = jTextFieldLogin.getText();
String pass = jPasswordField.getText();
String testpass;
try
{
Connection connexion = DriverManager.getConnection("jdbc:postgresql://localhost:5432/GPI", "postgres", "postgres");
System.out.println ("La connexion fonctionne parfaitement");
Statement stmt = connexion.createStatement();
String requete = "SELECT * FROM users WHERE login='"+log+"'";
ResultSet rs=stmt.executeQuery (requete); //commande SQL correspondant à une recherche du suspects Guéant
while (rs.next())
{
System.out.println("Resultat recherche");
System.out.println("Login :"+rs.getString(1)+"- Password : "+rs.getString(2)+" - Email : "+rs.getString(3)); // affichage du résultat de la recherche avec les valeurs contenues dans la base de données
testpass = rs.getString(2);
System.out.println(testpass);
System.out.println(log);
// if( log.equals(test) ) pour login et pass avec différence majuscules
if( pass.equalsIgnoreCase(testpass) )
{
System.out.println("Connection Autorisée");
setVisible(false);
fenAcceuil.show();
fenAcceuil.setVisible(true);
}
else
{
System.out.println("Connection Refusée");
}
}
stmt.close();
}
catch(SQLException a)
{
System.err.println("***** SQLException : *****");
while (a != null)
{
System.out.println("Message : " + a.getMessage ());
System.out.println("Etat : " + a.getSQLState ());
System.out.println("Code Erreur : " + a.getErrorCode () + "\n");
a = a.getNextException();
}
}
}
});
// Zone Correspondant au bouton de mot de passe oublié
jButtonReturnPass = new JButton();
jButtonReturnPass.setBounds(new java.awt.Rectangle(210,315,220,16));
jButtonReturnPass.setText("J'ai oublié mon mot de passe");
getContentPane().add(jButtonReturnPass);
jButtonReturnPass.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//lors du clique sur le bouton ajouter, on ouvrira la fenetre permettant de retrouver son password
fenPassPerdu.show();
setVisible(true);
}
});
this.setSize(640, 425);
this.setResizable(false);
jPanelFond = new JPanel();
ImageIcon img = new ImageIcon("fond.gif");
JLabel jPanelFond = new JLabel(img);
jPanelFond.setBounds(new java.awt.Rectangle(1,0,298,202));
this.add(jPanelFond, null);
this.centerScreen();
}
public static void main(String[] args) {
Authentification fenetre = new Authentification();
fenetre.setVisible(true);
}
} |
Partager