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
|
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import javax.swing.*;
public class Authentification extends JFrame {
//extends JDesktopPane {
JTextField jTextFieldLogin = null;
JPasswordField jPasswordField = null;
JLabel jLabelLogin = null;
JLabel jLabelPassword = null;
JButton jButtonConnection = null;
JPanel jPanelFond = null;
public Authentification()
{
super("Login GPI");
fenetreLogin();
}
public void fenetreLogin()
{
// zone correpondant au label Login
jLabelLogin = new JLabel();
jLabelLogin.setBounds(new java.awt.Rectangle(30,65,110,16));
jLabelLogin.setText("Nom d'utilisateur");
getContentPane().add(jLabelLogin);
//this.add(jLabelLogin, null);
// Zone correspondant au label password
jLabelPassword = new JLabel();
jLabelPassword.setBounds(new java.awt.Rectangle(30,109,110,16));
jLabelPassword.setText("Mot de passe");
getContentPane().add(jLabelPassword);
//this.add(jLabelPassword, null);
// Zone correspondant au textfield Login
jTextFieldLogin = new JTextField();
jTextFieldLogin.setBounds(new java.awt.Rectangle(174,60,90,30));
getContentPane().add(jTextFieldLogin);
//this.add(jTextFieldLogin, null);
// Zone correspondant au password field password
jPasswordField = new JPasswordField();
jPasswordField.setBounds(new java.awt.Rectangle(174,103,90,30));
getContentPane().add(jPasswordField);
//this.add(jPasswordField, null);
// Zone correspondant au bouton de connection
jButtonConnection = new JButton();
jButtonConnection.setBounds(new java.awt.Rectangle(107,161,98,22));
jButtonConnection.setText("Connection");
getContentPane().add(jButtonConnection);
//this.add(jButtonConnection, null);
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);
}
try
{
Connection connexion = DriverManager.getConnection("jdbc:postgresql://localhost:5432/GPI", "postgres", "postgres");
System.out.println ("La connexion fonctionne parfaitement");
Statement stmt = connexion.createStatement();
ResultSet rs=stmt.executeQuery ("SELECT * FROM users WHERE login='superloulou'"); //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
String test;
test = rs.getString(1);
String log = jTextFieldLogin.getText();
System.out.println(log);
if (log == test)
{
System.out.println("Valeur OK");
}
else
{
System.out.println("Connection Refusée");
System.exit(0);
}
}
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();
}
}
//System.exit(0);
}
});
this.setSize(300, 300);
this.setResizable(false);
jPanelFond = new JPanel();
jPanelFond.setBounds(new java.awt.Rectangle(1,0,298,202));
this.add(jPanelFond, null);
}
public static void main(String[] args) {
Authentification fenetre = new Authentification();
fenetre.setVisible(true);
}
} |
Partager