Aller au contenu

Recherche d'un client par nom en java


NiTrOuS

Messages recommandés

Bonjour à tous ...

Voilà j'ai un petit soucis, je fais un programme en java, j'arrive a faire une recherche par numero mais pas par nom.

Voici les différentes classes:

Ca c'est mon panneau recherche client par numéro

package magasin;

import java.awt.*;
import java.awt.event.*;

public class PanRechCli extends Panel implements ActionListener  
{
Button go = new Button("Rechercher");
TextField tf = new TextField();
PanneauData pdta =new PanneauData();
String[] liste = {"ID client","Nom","Prénom","Rue","Numéro","Code postal","Ville","Téléphone"};
Titre t = new Titre(liste);
   PanneauData ptitre = new PanneauData(t);
Frame parent;

public PanRechCli(Frame parent)
{
 this.parent=parent;
 GridLayout gl = new GridLayout(20,1,1,0);
 setLayout(gl);
 Panel pQuestion = new Panel();
 Label l = new Label("Numero recherché: ");
 pQuestion.add(l);
 pQuestion.add(tf);
 pQuestion.add(go);
 go.addActionListener(this);
 setBackground(new java.awt.Color(51141240));
 add(pQuestion);
 add(ptitre);
 add(pdta);      
}

public void actionPerformed(ActionEvent e)
{
 int n= Integer.parseInt(tf.getText());
 remove(pdta);
 ClientDB cl = GestCli.rechnum(n);
 if(cl!=null)
 {
 	pdta =new PanneauData(cl);
 	add(pdta);
 }	
 parent.show();
}
}

Ca c'est mon panneau recherche client par nom (la ou j'ai un probleme)

package magasin;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class PanRechCliNom extends Panel implements ActionListener  
{
Button go = new Button("Rechercher");
TextField tf = new TextField();
PanneauData pdta =new PanneauData();
String[] liste = {"ID client","Nom","Prénom","Rue","Numéro","Code postal","Ville","Téléphone"};
Titre t = new Titre(liste);
   PanneauData ptitre = new PanneauData(t);
Frame parent;

public PanRechCliNom(Frame parent)
{
 this.parent=parent;
 GridLayout gl = new GridLayout(20,1,1,0);
 setLayout(gl);
 Panel pQuestion = new Panel();
 Label l = new Label("Nom recherché: ");
 pQuestion.add(l);
 pQuestion.add(tf);
 pQuestion.add(go);
 go.addActionListener(this);
 setBackground(new java.awt.Color(51141240));
 add(pQuestion);
 add(ptitre);
 add(pdta);      
}

public void actionPerformed(ActionEvent e)
{
    String nomr = tf.getText();
 remove(pdta);
 nomr = GestCli.rechnom(nomr);
 if(nomr!=null)
 {
 	pdta =new PanneauData();
 	add(pdta);
 }	
 parent.show();
}
}

PanneauData

package magasin;

import java.awt.*;
import java.util.*;

public class PanneauData extends Panel  
{
protected String[] ligne;
protected TextField[] tabFields;
protected int l;
public PanneauData()
{
}

public PanneauData(Affichable aff )
{
 ligne = aff.getRens();
 l=ligne.length;
 tabFields = new TextField[l];
 GridLayout gl = new GridLayout(1,l,2,2);
 setLayout(gl);
 for(int i = 0;i<l;i++)
 {
 	tabFields[i]= new TextField(ligne[i]);
 	add(tabFields[i]);
 	}
}
 
public String[] getValeurs ()
{
 String[] ligne2 = new String[l];
 for(int i=0;i<l;i++)
 {
 	ligne2[i]=tabFields[i].getText();
 }
 return ligne2;
}	
}


Ma recherche par nom

protected static ClientDB rechnom()
{
 ClientDB cl=null;
    System.out.println("Nom recherché:");
    String nomr = Lecture.lireString();
    Vector clisRech = GestCli.rechnom(nomr);
    if(clisRech.size()==0) 
    {
     System.out.println("Aucun client trouve pour ce nom");
        return null;
    }	
  
    //sinon poursuite du programme
    for(int i = 0;i<clisRech.size();i++)
    {
     cl = (ClientDB)clisRech.elementAt(i);
        System.out.print((i+1)+".");
        affiche(cl);
    }
 boolean erreurChoix;
    do
    {
     erreurChoix=false;	
     System.out.print("Indiquez le numero a charger (0 pour aucun): ");
        int n = Lecture.lireInt();
        if(n==0) return null;
     else
     {
     	if ((n>0) && (n <= clisRech.size()))
         cl=(ClientDB) clisRech.elementAt(n-1);
     else 
     {
     	System.out.println("Position invalide"); 
     erreurChoix=true;
     }
  }
  }
  while(erreurChoix);
  return GestCli.stocke(cl);
//plutot que return cl au cas ou le client serait deja charge
}

GestCli

package magasin;

import java.util.*;
import myconnections.Connectable;

public class GestCli 
{
protected  static HashMap chargement = new HashMap();	

static
{
 	try
 	{   
 	Class.forName("myconnections.Connectable");
 }
    catch(ClassNotFoundException e)
    {
 	System.out.println("Impossible de charger la classe Connectable");
     System.exit(0);
    }	
}	
  /*la clause static est exécutée une fois lors du chargement de
  la classe , ici provoque l'appel de Connectable  */

public GestCli() 
{
}  

public static ClientDB stocke(ClientDB cl)
{
 if(chargement.get(cl.getCleCli())==null)chargement.put(cl.getCleCli(),cl);
 return (ClientDB) chargement.get(cl.getCleCli());
}

public static ClientDB rechnum(int nr)
{
 ClientDB cl = null;
 String cleCli=""+nr;
 cl=(ClientDB) chargement.get(cleCli);
 if (cl!=null) return cl;
 else 
 {
 	cl =rechDB(nr);
 	if(cl!=null) 
 	{ 
   chargement.put(cleCli,cl);
   return cl;
 	}
 	else return null;
 }                      	
}

 protected static ClientDB rechDB(int nr)
 {
 	ClientDB cl = new ClientDB(nr);
 	cl.restaurer();
 	if(ClientDB.erreur==null)return cl;
     else 
     { 
         System.out.println("erreur de restauration "+ClientDB.erreur);
         return null;
     }        
 }

public static Vector rechnom(String nomr)
{
 	miseAJour();
 	/*pour que par exemple , en cas de modification du
   telephone  on n'ait pas deux fois 
   le même client avec deux telephones différents .
   le premier venant de chargement , le second 
   de la DB */
 	return ClientDB.recupNom(nomr); 
}

public static void miseAJour()
{
 ClientDB cl;	
 Iterator liste = chargement.values().iterator();
 Vector efface = new Vector();
 while(liste.hasNext())
 {  
 cl=(ClientDB)liste.next();
 int etat = cl.getEtat();
 System.out.println(""+etat);
 switch (etat)
 {
 	case ClientDB.AJOUR: cl.restaurer();
   //en cas de modification par une autre application
           break;	
   case ClientDB.MODIFIE : cl.modifier();
                         	break;
 	case ClientDB.EFFACE : cl.supprimer();
                            efface.add(cl);
                         	break;	
 }
 
}

/*on ne peut pas effacer dans une HashMap qui supporte 
 un iterator en cours d'utilisation
 */
 
Iterator itEfface = efface.iterator();
while(itEfface.hasNext())
{
 cl=(ClientDB)itEfface.next();
 chargement.remove(cl.getCleCli());  
   }   
}  	

public static void fermeture()
{
try
{
 Connectable.dbConnect.close();
}
catch(Exception e)
{
}
   	
}	
}

ClientDB

package magasin;

import myconnections.Connectable;
import java.sql.*;
import java.util.*;

public class ClientDB extends Client implements Connectable,Affichable
{
   protected int etat=AJOUR;
    
   public static Exception erreur=null;
      
   public ClientDB()
   {
   }
   
   public ClientDB(int c_id)
   {
   	this.c_id=c_id;
   	restaurer();
   	etat=AJOUR;	
   }
   
   public ClientDB(int c_id,String nom,String prenom,String rue,
   int numero,int codepost,String ville,String tel)
   {	
     	super(c_id,nom,prenom,rue,numero,codepost,ville,tel);	
     	etat=AJOUR;
   }

public ClientDB(String nom,String prenom,String rue,int numero,
int codepost,String ville,String tel)
{
 super(0,nom,prenom,rue,numero,codepost,ville,tel);	
       setC_id();
       ajouter();
       etat=AJOUR;
   }
   
   protected static int getMaxDB()
   {
   	erreur = null;
   	try
   	{
     String req="select max(c_id) from clients";
     Statement stm = dbConnect.createStatement();
     ResultSet rs = stm.executeQuery(req);
     if(rs.next()) return rs.getInt(1);
     else return 0;	
   	}
   	catch(Exception e)
   	{
           erreur= e;
     return -1;
   	}
   }

public void ajouter()
{
 erreur = null;  
       try
       {
       	String req = "insert into clients values(?,?,?,?,?,?,?,?)";	
        PreparedStatement pstm = dbConnect.prepareStatement(req);
        pstm.setInt(1,c_id);
        pstm.setString(2,nom);
        pstm.setString(3,prenom);
        pstm.setString(4,rue);
        pstm.setInt(5,numero);
        pstm.setInt(6,codepost);
        pstm.setString(7,ville);
        pstm.setString(8,tel);
        pstm.executeUpdate();
        pstm.close();
    }
    catch(Exception e)
    {
     erreur=e;
    }
    etat=AJOUR;
} 
 
public void restaurer()
{
 nom="+inconnu+";
 prenom="+inconnu+";
 rue="";
 numero=0;
 codepost=0;
 ville="";
 tel="000000000";  
 erreur = null;
 try
 {
 	String req = "select * from clients where c_id = ?";	
        PreparedStatement pstm = dbConnect.prepareStatement(req);
        pstm.setInt(1,c_id);
        ResultSet rs = pstm.executeQuery();
        if(rs.next())
        {
         nom=rs.getString("nom");
          prenom=rs.getString("prenom");
       rue=rs.getString("rue");
       numero=rs.getInt("numero");
       codepost=rs.getInt("codepost");
       ville=rs.getString("ville");
       tel=rs.getString("tel");
         pstm.close();
         etat=AJOUR;
       }
       else 
       {        
       	pstm.close();
            throw new Exception("Code inconnu");
       }
 }
 catch(Exception e)
 {
 	erreur =e;
 }  
}

public void modifier()
{
    erreur = null;  
       try
       {
       	String req = "update clients set nom=?,prenom=?,rue=?,numero=?,codepost=?,ville=?,tel=? where c_id=?";	
     	PreparedStatement pstm = dbConnect.prepareStatement(req);
 	pstm.setString(1,nom);
     	pstm.setString(2,prenom);
     	pstm.setString(3,rue);
     	pstm.setInt(4,numero);
     	pstm.setInt(5,codepost);
     	pstm.setString(6,ville);
     	pstm.setString(7,tel);
     	pstm.setInt(8,c_id);
     	int nl= pstm.executeUpdate();
     	etat=AJOUR;
     	if(nl==0)throw new Exception("Aucune ligne mise à jour");
     	pstm.close();
 	}
 catch(Exception e)
 {
 	erreur=e;
   }
} 
 
public void setRue(String rue) 
{
 this.rue=rue;
 etat=MODIFIE;	
}

public void setNumero(int numero) 
{
 this.numero=numero;
 etat=MODIFIE;
}
 
   public void setCodePost(int codepost) 
   {
 this.codepost=codepost;
       etat=MODIFIE;
}

public void setVille(String ville) 
{
 this.ville=ville;
 etat=MODIFIE;
}

public void setTel(String tel) 
{
 this.tel=tel;
     etat=MODIFIE;
}  	
 
public void efface()
{
 	etat=EFFACE;
}	
 
public int getEtat()
{
 return etat;
}
 	
public void supprimer()
{
 erreur=null;
 int nl=0;
 try
 {
 	String req = "delete from clients where c_id = ?";	
     	PreparedStatement pstm = dbConnect.prepareStatement(req);
     	pstm.setInt(1,c_id);
     	nl = pstm.executeUpdate();
     	pstm.close();
     	if(nl==0) throw new Exception("Aucune ligne  effacée");
    }	
    catch (Exception e)
    {
     	erreur =e;
    }
     
}

public void setC_id()
{
 super.setC_id(getMaxDB()+1);
}

public static Vector recupNom(String nomrech)
{
    Vector plusieurs=new Vector();
    String req = "select * from clients where nom = ?";	
    erreur=null;
    try
    {
     PreparedStatement pstm = dbConnect.prepareStatement(req);
     	pstm.setString(1,nomrech);
     	ResultSet rs = pstm.executeQuery();
     	while(rs.next()){
        int c_id=rs.getInt("c_id");
     	String nom=rs.getString("nom");
     	String prenom=rs.getString("prenom");
     	String rue=rs.getString("rue");
     	int numero=rs.getInt("numero");
     	int codepost=rs.getInt("codepost");
     	String ville=rs.getString("ville");
     	String tel=rs.getString("tel");
     	plusieurs.add(new ClientDB(c_id,nom,prenom,rue,numero,codepost,ville,tel));
 }
    pstm.close();
}

 catch(Exception e)
 {
 	erreur =e;
 }
 return plusieurs;
}	

public String[] getRens()
{
 String [] elts = new String[8];
 elts[0]=c_id+"";
 elts[1]=nom;
 elts[2]=prenom;
 elts[3]=rue;
 elts[4]=numero+"";
 elts[5]=codepost+"";
 elts[6]=ville;
 elts[7]=tel;
 return elts;	
}	
}

Voila, merci pour votre aide

Lien vers le commentaire
Partager sur d’autres sites

public static Vector recupNom(String nomrech)

{

    Vector plusieurs=new Vector();

    String req = "select * from clients where nom = ?";

    erreur=null;

    try

    {

    PreparedStatement pstm = dbConnect.prepareStatement(req);

      pstm.setString(1,nomrech);

      ResultSet rs = pstm.executeQuery();

      while(rs.next()){

        int c_id=rs.getInt("c_id");

      String nom=rs.getString("nom");

      String prenom=rs.getString("prenom");

      String rue=rs.getString("rue");

      int numero=rs.getInt("numero");

      int codepost=rs.getInt("codepost");

      String ville=rs.getString("ville");

      String tel=rs.getString("tel");

      plusieurs.add(new ClientDB(c_id,nom,prenom,rue,numero,codepost,ville,tel));

}

    pstm.close();

}

catch(Exception e)

{

  erreur =e;

}

return plusieurs;

}

erreur = e;

T'en fais quoi de ton erreur?

Généralement on fait un e.printStackTrace(); pour voir l'erreur retournée.

Je miserais au pif pour une java.sql.sqlException mais bon.

Ah et sinon, quand on code, on commente, c'est assez pratique pour le gars qui doit faire la maintenance après. Et la javadoc, si elle existe c'est pour s'en servir également.

Lien vers le commentaire
Partager sur d’autres sites

Je le prends pas de haut mais tu nous donnes 6 classes avec aucune indication d'erreur et aucun commentaires pour nous aider à comprendre le code.

Je pense que si t'as pas eu d'autres réponses c'est un peu pour ca. Après nous sommes des INpactiens donc je ne serai pas plus désagréable que ca seulement quand code, on commente, c'est une règle de base.

Sinon, ca donne quoi l'erreur alors???

Si je t'ai répondu c'est que j'ai voulu le faire alors éclaire moi un peu qu'on puis faire avancer le shimiliblick

Lien vers le commentaire
Partager sur d’autres sites

Archivé

Ce sujet est désormais archivé et ne peut plus recevoir de nouvelles réponses.

×
×
  • Créer...