Probleme de connexion java

Résolu/Fermé
papillonlibre22 Messages postés 27 Date d'inscription mardi 30 juin 2015 Statut Membre Dernière intervention 2 août 2016 - Modifié par papillonlibre22 le 20/07/2016 à 14:03
KX Messages postés 16752 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 août 2024 - 22 juil. 2016 à 19:13
Bonjour, je developpe une application en java avec une base de donnes mysql mais des probleme quand j'essaie de me connecter,on m'affiche une erreur que je n'arrive pas a comprendre.
voici l'erreur et mon code:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '='gg' and password='cxvx'' at line 1
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at BaseDeDonnee.Login.btnlActionPerformed(Login.java:127)
at BaseDeDonnee.Login.access$000(Login.java:15)
at BaseDeDonnee.Login$1.actionPerformed(Login.java:59)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

mon code:

package BaseDeDonnee;

import Application.BDD;
import Application.Parameter;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
*
  • @author ADAMA SAVADOGO
  • /

public class Login extends javax.swing.JFrame {

ResultSet r;
BDD db;
String username1, password1,hak;

public Login() {
db=new BDD(new Parameter().HOST_DB, new Parameter().USERNAME_DB, new Parameter()
.PASSWORD_DB, new Parameter().IPHOST, new Parameter().PORT);
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.
  • /

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
txtid = new javax.swing.JTextField();
txtpw = new javax.swing.JTextField();
btnl = new javax.swing.JButton();
btnf = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
jLabel1.setText("Login");

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

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

btnl.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
btnl.setText("Login");
btnl.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnlActionPerformed(evt);
}
});

btnf.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
btnf.setText("Fermer");
btnf.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnfActionPerformed(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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(134, 134, 134)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(89, 89, 89)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(51, 51, 51)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(txtpw)
.addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(btnl)
.addGap(47, 47, 47)
.addComponent(btnf)))
.addContainerGap(85, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(38, 38, 38)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(txtid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3)
.addComponent(txtpw, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnl)
.addComponent(btnf))
.addGap(41, 41, 41))
);

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

private void btnfActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
}

private void boutonloginActionPerformed(java.awt.event.ActionEvent evt) {
r=db.querySelectAll("utilisateur", "username='"+txtid.getText()
+ "' and password='" +txtpw.getText()+"'");
try{
while(r.next()){
username1=r.getString("username");
password1=r.getString("password");
hak=r.getString("type");
}
}catch(SQLException ex){
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}

if(username1 == null && password1 == null){
JOptionPane.showMessageDialog(this, "Nom utilsateur ou password incorrect");
} else {

if (hak.equals("directeur")) {//ouvre form pour directeur
Principale h=new Principale();
h.setVisible(true);
this.dispose();
} else {
Cachier k=new Cachier();//ouvre form pour cashier
k.setVisible(true);
this.dispose();
}
}

}

/**
  • @param args the command line arguments
  • /

public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Login().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton btnf;
private javax.swing.JButton btnl;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField txtid;
private javax.swing.JTextField txtpw;
// End of variables declaration
}
A voir également:

1 réponse

KX Messages postés 16752 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 août 2024 3 018
20 juil. 2016 à 19:44
Bonjour,

Il y a deux erreurs dans ce que tu as mis

1) une erreur très explicite :
"MySQLSyntaxErrorException: You have an error in your SQL syntax"

2) une erreur plus technique
NullPointerException at BaseDeDonnee.Login.btnlActionPerformed

Celle ci est probablement liée au fait que n'ayant pas réussi la requête (premier message) tu n'aurais pas du continuer le traitement car il utilise un résultat incohérent.

Je t'invite à logguer la requête que tu essayes d'exécuter pour voir pourquoi elle est fausse, et éventuellement rajouter un try/catch pour qu'une exception sur l'exécution de la requête empêche de traiter le résultat de la requête (résultat qui n'existe pas puisque la requête a planté)
0
papillonlibre22 Messages postés 27 Date d'inscription mardi 30 juin 2015 Statut Membre Dernière intervention 2 août 2016
22 juil. 2016 à 11:39
Bonjour j'ai essaye de corrige,voici maintenant la seule erreur qui s'affiche:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''sf' and password='fdd'' at line 1
merci pour votre aide!!!
0
aebischerle Messages postés 34 Date d'inscription vendredi 5 décembre 2014 Statut Membre Dernière intervention 22 juillet 2016 1
22 juil. 2016 à 11:49
Hello,
comme KX te l'a déjà dis, c'est très explicite... ta requête SQL est fausse, elle comporte une erreur, je te conseille de la copier et de la tester directement sur ta base de données, puis dès qu'elle fonctionne comme tu le désire, de la remettre dans ton prog.

Si tu poste ta requête peut-être pourrons nous t'aider.
0
papillonlibre22 Messages postés 27 Date d'inscription mardi 30 juin 2015 Statut Membre Dernière intervention 2 août 2016 > aebischerle Messages postés 34 Date d'inscription vendredi 5 décembre 2014 Statut Membre Dernière intervention 22 juillet 2016
Modifié par papillonlibre22 le 22/07/2016 à 13:45

bonjour, voici la requête

r=db.querySelectAll("utilisateur", "username='"+txtid.getText()
+ "' and password='" +txtpw.getText()+"'");
try{
while(r!=null && r.next()){
username1=r.getString("username");
password1=r.getString("password");
hak=r.getString("type");
}
}catch(SQLException ex){
Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}

if(username1 == null && password1 == null)
{
JOptionPane.showMessageDialog(this, "Nom utilsateur ou password incorrect");
Jlabel.setText("errrrrrrrrrrrrrr");
Jlabel.setVisible(true);
} else {

if (hak.equals("directeur")) {//ouvre form pour directeur
Principale h=new Principale();
h.setVisible(true);
this.dispose();
} else {
Cachier k=new Cachier();//ouvre form pour cashier
k.setVisible(true);
this.dispose();
}
}

//fonction querySelectAll

public ResultSet querySelectAll(String nomTable, String etat){

connexionDatabase();
SQL = "SELECT * FROM " +nomTable+ "where" +etat;
return this.executionQuery(SQL);
}

Merci grandement!!!
0
KX Messages postés 16752 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 31 août 2024 3 018
Modifié par KX le 22/07/2016 à 19:48
"SELECT * FROM " +nomTable+ "where" +etat;

Il faudrait rajouter des espaces autour de where, là ça va faire :
SELECT * FROM utilisateurwhereusername=

Je t'invite aussi à regarder comment fonctionne les PreparedStatement et leur avantage contre les injections SQL
0