Bonjour, j'essaie de faire fonctionner ce simple login, mais pour une raison obscure mon bouton OK (btnOk) ne fonctionne que la première fois qu'on appuie dessus! Désolé pour les noms des textfields et autres affichages bizarres, ceci est en phase très peu avancé! Quelqu'un peut m'aider?? Merci d'avance!
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
public class LogIn extends JFrame {
private String url;
private Connection connecter;
private Statement instruction;
private ResultSet jeuResultats;
private JLabel lblID = new JLabel("ID: ", JLabel.RIGHT);
private JLabel lblPW = new JLabel("PW: ", JLabel.RIGHT);
private JButton btnOK = new JButton("OK");
private JButton btnRegister = new JButton("Remplissez les champs et cliquez ici pour vous enregistrer");
private JTextField txtID = new JTextField(10);
private JTextField txtPW = new JTextField(10);
private JTextField txtTest = new JTextField(50);
public LogIn() {
super ("Identification Joueur 1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
add(lblID);
add(txtID);
add(lblPW);
add(txtPW);
add(btnOK);
add(btnRegister);
add(txtTest);
setSize(350,150);
setVisible(true);
try
{
url = "jdbc:odbc:francis";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connecter = DriverManager.getConnection(url);
txtID.setText("Connection reussie");
}
catch(ClassNotFoundException cnfex)
{
cnfex.printStackTrace();
txtID.setText("Class not found exception");
}
catch(SQLException sqlex)
{
sqlex.printStackTrace();
txtID.setText("SQLException");
}
btnOK.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== btnOK)
trouverID();
}
}
);
}
private void trouverID()
{
try
{
if(!txtID.getText().equals(""))
{
Statement ordre = connecter.createStatement();
String requeteID
"SELECT * FROM players where userName '" + txtID.getText() + "'";
txtTest.setText(requeteID);
ResultSet rs = ordre.executeQuery(requeteID);
afficher(rs);
}
else
txtID.setText("Entrez votre ID pour commencer");
}
catch(Exception e)
{
}
}
private void afficher(ResultSet rs)
{
String numeroEnregistrement = "";
try {
boolean autresEnregistrements = rs.next();
if (autresEnregistrements)
{
numeroEnregistrement = rs.getString("userName");
}
else
{
JOptionPane.showMessageDialog(this,"Aucun enregistrement à montrer");
}
if (numeroEnregistrement.equals(txtID.getText())) {
txtPW.setText( numeroEnregistrement);
}
}
catch(SQLException sqlex)
{
txtPW.setText("erreur");
}
catch(Exception e)
{
txtPW.setText("Autre erreur");
}
}
public static void main(String[] arguments) {
LogIn latf = new LogIn();
}
}
Afficher la suite