Peut on faire cela ?

colruyt Messages postés 20 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 18 novembre 2004 - 11 mai 2003 à 19:39
jtrain Messages postés 1 Date d'inscription jeudi 8 avril 2004 Statut Membre Dernière intervention 8 avril 2004 - 8 avril 2004 à 16:40
A mon avis non car ca ne marche pas !!!!!!!
une idée ?
( But : créer un jComboBox après l'actionPerformed d'un autre . )

private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
//String choixArticle = jComboBox2.getSelectedItem().toString();
String url = "jdbc:odbc:db";
Connection con=null;

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,"","");
Statement requete8 = con.createStatement();
ResultSet resultat3=requete8.executeQuery("select * from Stock");
while(resultat3.next()){
jComboBox3.addItem(resultat3.getString(5));
};
}
catch(Exception e) { e.printStackTrace();}
finally {
try { con.close();}
catch(SQLException e) {e.printStackTrace();}
}
}

toutes les idées sont les bienvenues ...

11 réponses

cs_Duss Messages postés 890 Date d'inscription lundi 8 avril 2002 Statut Membre Dernière intervention 29 juillet 2004 11
11 mai 2003 à 20:11
salut,
c est possible a condition que ta combo3 soit declarée comme un attribut de ta classe et qu elle ait été instanciée (le new) avant
Duss
0
colruyt Messages postés 20 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 18 novembre 2004
11 mai 2003 à 21:47
/---------------------------\
|| ***************** ||
*********************
* *
* *

Merci de ta réponse mais il y a autre chose qui me bloque :
J'essaie sur base d'un choix jComboBox(référence) de renvoyer le libellé dans un jTextField. J'ai essayé ceci mais ca ne marche pas et je cherche tjrs......

String moi=jComboBox2.getSelectedItem().toString();
String url = "jdbc:odbc:db";
Connection con=null;

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,"","");
Statement requete = con.createStatement();
ResultSet resultat4=requete.executeQuery("SELECT Libellé FROM Stock where 'ID Produit'='moi' ");
String toi=resultat4.toString();
jTextField1.setText(toi);
}
0
cs_Duss Messages postés 890 Date d'inscription lundi 8 avril 2002 Statut Membre Dernière intervention 29 juillet 2004 11
11 mai 2003 à 21:51
salut,
ResultSet resultat4=requete.executeQuery("SELECT Libellé FROM Stock where 'ID Produit'='"+moi+"' ");
il faut concatener ta variable !!
Duss
0
colruyt Messages postés 20 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 18 novembre 2004
11 mai 2003 à 21:54
/---------------------------\
|| ***************** ||
*********************
* *
* *
Concernant le dernier message envoyé , j'ai ceci qui s'affiche dans le jtextfield : "sun.jdbc.odbc.JdbcOdbcResultSet@c837cd"???????
De plus , si je ne mets pas le nom de la colonne de la base de données entre cote , il me dit qu'il manque l'opérateur de la requete ???

Merci Jean Claude
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
colruyt Messages postés 20 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 18 novembre 2004
11 mai 2003 à 22:00
/---------------------------\
|| ***************** ||
*********************
* *
* *

Merci beaucoup pour tes éclaircissements utils mais g tjrs ce message dans mon textfield :
jdbc.odbc.JdbcOdbcResultSet@.........
Ché pas c'que c'est .
0
cs_Duss Messages postés 890 Date d'inscription lundi 8 avril 2002 Statut Membre Dernière intervention 29 juillet 2004 11
11 mai 2003 à 22:00
salut,
resultat4.first();
String toi=resultat4.getString("nom de la colonne");
Duss
0
colruyt Messages postés 20 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 18 novembre 2004
11 mai 2003 à 22:07
Désolé de t'embeter si tard pour des conneries mais j'ai un travail a rendre Mardi soir et je reste bloqué à certain endroits cependant , j'ai maintenant un nouveau message d'erreur .
Le code donne bien ceci ?

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,"","");
Statement requete = con.createStatement();
ResultSet resultat4=requete.executeQuery("SELECT Libellé FROM Stock where 'ID Produit'='"+moi+"' ");
resultat4.first();
String toi=resultat4.getString("Libellé");
jTextField1.setText(toi);

Il me dit : "Result set type is TYPE_FORWARD_ONLY"

Si ca te dit qlq chose
0
cs_Duss Messages postés 890 Date d'inscription lundi 8 avril 2002 Statut Membre Dernière intervention 29 juillet 2004 11
12 mai 2003 à 08:41
salut,
quand tu créé ton statement, tu peux lui passer des arguments (je les ai pas en tete la ;) ) qui ressemble a un truc comme ResultSet.TYPE_SCROLL_INSENSITIVE qui permet de dire que tu peux parcourir ton ResultSet dans le sens que tu veux
Duss
0
colruyt Messages postés 20 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 18 novembre 2004
12 mai 2003 à 11:00
En fait , j'ai des blemes car mes noms de colonnes de ma db sont en plusieurs mots .......
Depuis ca marche mais j'ai autre chose :

Regarde un peu ce qui suit :

j'aimerais mettre dans jComboBox3 ( 1,2,3, .... jusqu'a ma valeur de QT dans ma db )

et j'ai ceci comme code mais apparement c pas correct :

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,"","");
Statement requete = con.createStatement();
ResultSet resultat3=requete.executeQuery("select QT from Stock where IDProduit='"+moi+"' ");
System.out.println(""+moi);
String reprend=resultat3.getString("QT");
int nbre = Integer.parseInt(reprend);
System.out.println(nbre);
int tab[];
tab=new int[nbre];
for(int xc=1;xc<=nbre;xc++) {
tab[xc-1]=xc;
System.out.println(""+tab[xc-1]);
}
while(resultat3.next()){
jComboBox3.removeAllItems();
jComboBox3.addItem(tab);
};
}

PS: Ca fait combien de temps que tu travailles en java ?

Merci pour tout
0
cs_Duss Messages postés 890 Date d'inscription lundi 8 avril 2002 Statut Membre Dernière intervention 29 juillet 2004 11
12 mai 2003 à 11:44
salut,
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,"","");
Statement requete = con.createStatement();
ResultSet resultat3=requete.executeQuery("select QT from Stock where IDProduit='"+moi+"' ");
System.out.println(""+moi);
String reprend=resultat3.getString("QT");
int nbre = Integer.parseInt(reprend);
jComboBox3.removeAllItems();

for(int xc=1;xc<=nbre;xc++) {
jComboBox3.addItem(new Integer(xc));
}
}

Voila ;)
Duss
PS : ben ca fait 6/7 mois
0
jtrain Messages postés 1 Date d'inscription jeudi 8 avril 2004 Statut Membre Dernière intervention 8 avril 2004
8 avril 2004 à 16:40
Bonjour,

G 1 prb de combo qui n'est pas initialisée à chaque requête, pourtant je mets bien un removeallitems().
Voilà, mon code: il s'agit de 4 combo, la 1ère c'est "groupe", la seconde c'est "nom", la troisième c'est "prénom", la dernière qui pose prb c'est "date".

En fait je fais des requêtes SQL, une fois je choisis le groupe, j'appelle BatirListeNom, qui en fonction du groupe choisit va lister dans la combo "nom" les noms des personnes du groupe, et selon le nom choisit, j'appelle BatirListePrénom qui affiche le prénom, puis j'appelle la fonction determinernumsportif() qui fait une requete SQL pour obtenir le num de la personne dont le nom et le prenom sont choisit. Apres, j'appelle la fonction BatirListeDate(numsportif), qui sort les dates disponibles pour le num du sportif. Le numsportif est exact, cependant, dans la combo"date", on trouve les bonnes dates plus les dates trouvées avant, comme si la combo n'est pas réinitialisé, pourtant......!!!!

// PrepaGraphePOMS.java
// ------------------------
// Projet Sport Elite Partner
// ------------------------
// (c) 04/2003
// ------------------------
// Cette classe est utilisee pour preparer
// les donnees dun graphe poms

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

public class PrepaGraphePOMS extends JFrame{
//INSTANCES
private JComboBox listeGroupe,listeNom,listePrenom;
private JComboBox listeDate;
private String query, choixGroupe, choixNom, choixPrenom, choixDate;
private int numSportif, mx, my;
private JTextField champNbJours;
private String typeData[] = {"Jours","Entrainements"};
private float tAnx[] = {32,34,35,37,38,39,41,42,43,45,
46,48,49,50,52,53,55,56,57,59,
60,61,63,64,66,67,68,70,71,72,
74,75,77,78,79,81,82};
private float tCol[] = {35,36,37,38,39,41,42,43,44,46,
47,48,49,50,52,53,54,55,56,58,
59,60,61,63,64,65,66,67,68,70,
71,72,73,75,76,77,78,80,81,82,
83,84,86,87,88,89,91,92,93};
private float tConf[]= {32,34,36,38,40,42,44,46,48,50,
52,54,57,59,61,63,65,67,69,71,
73,75,77,79,81,83,85,87,89};
private float tDep[] = {39,40,41,42,43,44,45,46,47,49,
50,51,52,53,54,55,56,57,58,59,
61,62,63,64,65,66,67,68,69,70,
71,73,74,75,76,77,78,79,80,81,
82,83,85,86,87,88,89,90,91,92,
93,94,95,97,98,99,100,101,102,103,104};
private float tFat[] = {34,36,37,39,41,42,44,45,47,49,
50,52,53,55,57,58,60,61,63,65,
66,68,69,71,73,74,76,77,79};
private float tVig[] = {23,25,27,28,30,32,34,35,37,39,
41,42,44,46,48,49,51,53,54,56,
58,60,61,63,65,67,68,70,72,74,
75,77,76};
private float tRel[] = {16,18,20,22,25,27,29,31,33,35,
37,40,42,44,46,48,50,52,54,56,
59,61,63,65,67,69,71,74,76};

//CONSTRUCTEURS
public PrepaGraphePOMS()
{
super("Preparation du graphe");
mx=400;
my=400;
setSize(mx,my);

listeGroupe=new JComboBox();
listeGroupe.setMaximumRowCount(5);
listeGroupe.setBackground(new Color(180,180,255));
listeNom=new JComboBox();
listeNom.setMaximumRowCount(5);
listeNom.setBackground(new Color(180,180,255));
listePrenom=new JComboBox();
listePrenom.setMaximumRowCount(5);
listePrenom.setBackground(new Color(180,180,255));
listeDate = new JComboBox();
listeDate.setMaximumRowCount(5);
listeDate.setBackground(new Color(180,180,255));

//Definition du conteneur graphique
Container c = getContentPane();
FlowLayout AligGauche = new FlowLayout();
AligGauche.setAlignment(FlowLayout.LEFT);
c.setLayout(AligGauche);

JLabel labelTitre = new JLabel("Préparation du graphe P.O.M.S.");
labelTitre.setFont(new Font("Serif", Font.BOLD+Font.ITALIC,18));
JPanel panelTitre = new JPanel(new FlowLayout(FlowLayout.CENTER)); // Paneau pour le bouton valider
panelTitre.setPreferredSize(new Dimension(mx-20,40));
panelTitre.setBackground(new Color(180,180,255));
panelTitre.add(labelTitre);

JPanel panneauEspace = new JPanel();
panneauEspace.setPreferredSize(new Dimension(mx-20,20));
JPanel panneauEspace2 = new JPanel();
panneauEspace2.setPreferredSize(new Dimension(mx-20,20));
JPanel panneauEspace3 = new JPanel();
panneauEspace3.setPreferredSize(new Dimension(mx-20,20));

JLabel etiquetteGroupe = new JLabel("Groupe du sportif : ",SwingConstants.RIGHT);
etiquetteGroupe.setPreferredSize(new Dimension(150,25));

query="SELECT nom FROM groupe ORDER by nom";
Connexion connex = new Connexion(0,query);
try {
while (connex.results.next())
{
listeGroupe.addItem(connex.results.getString("nom"));
}// Fin de while connex.results.next()
listeGroupe.setMaximumRowCount(5);
listeGroupe.setSelectedIndex(0);
choixGroupe=listeGroupe.getSelectedItem().toString();
System.out.println("Groupe sélectionné au départ : "+choixGroupe);
}
catch(Exception e){};
batirListeNom(choixGroupe);
// Gestion des evenements sur le groupe
listeGroupe.addItemListener(
new ItemListener()
{
public void itemStateChanged (ItemEvent ie) {
if (ie.getStateChange()==ItemEvent.SELECTED){
choixGroupe=listeGroupe.getSelectedItem().toString();
System.out.println("...Groupe selectionné..."+choixGroupe);
// Initialisation du combobox nom
batirListeNom(choixGroupe);
}//fin de if
}
}//Fin de la classe interne anonyme
); //Fin de l'appel vers addActionListener

JLabel etiquetteNom = new JLabel("Nom : ",SwingConstants.RIGHT);
etiquetteNom.setPreferredSize(new Dimension(150,25));
// Gestion des prenom en fonction du nom
//Ajout essai *******************************************
//query="SELECT nom FROM sportif WHERE groupe='"+choixGroupe+"'";
//Fin Ajout essai *********************************
listeNom.addItemListener(
new ItemListener() {
public void itemStateChanged (ItemEvent ie) {
if (ie.getStateChange()==ItemEvent.SELECTED){
System.out.println("Entree dans le gestionnaire de changement item du combo du nom");
choixNom=listeNom.getSelectedItem().toString();
System.out.println("...Nom selectionné...");
// Initialisation du combobox nom
batirListePrenom(choixGroupe, choixNom);
}//fin de if
}
}//Fin de la classe interne anonyme
); //Fin de l'appel vers addActionnListener
JLabel etiquettePrenom = new JLabel("Prenom : ",SwingConstants.RIGHT);
etiquettePrenom.setPreferredSize(new Dimension(150,25));
listePrenom.addItemListener(
new ItemListener() {
public void itemStateChanged (ItemEvent ie) {
if (ie.getStateChange()==ItemEvent.SELECTED){
choixPrenom=listePrenom.getSelectedItem().toString();
determinerNumSportif();
batirListeDate(numSportif);
}//fin de if
}
}//Fin de la classe interne anonyme
); //Fin de l'appel vers addActionnListener

JLabel etiquetteDate = new JLabel("POMS du : ",SwingConstants.RIGHT);
etiquetteDate.setPreferredSize(new Dimension(150,25));
listeDate.addItemListener(
new ItemListener() {
public void itemStateChanged (ItemEvent ie) {
if (ie.getStateChange()==ItemEvent.SELECTED){
choixDate=listeDate.getSelectedItem().toString();
}//fin de if
}
}//Fin de la classe interne anonyme
); //Fin de l'appel vers addActionnListener

determinerNumSportif();
batirListeDate(numSportif);

JButton validerPrepa = new JButton ("Afficher le graphe");
validerPrepa.setBackground(new Color(230,220,210));
validerPrepa.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
query="SELECT sommeANX,sommeCOL,sommeCONF,sommeDEP,sommeFAT,sommeVIG,sommeREL FROM pomsrempli WHERE numsp='"+numSportif+"' AND date='"+choixDate+"' LIMIT 1";
System.out.println(query);
Connexion c2 = new Connexion(0,query);
try {
c2.results.next();
System.out.println("DRAPEAU 1...");
float pomsData[] = new float[7];
float pomsLigne[] = new float[7];
String titreA[]= new String[7];
//System.out.println("DRAPEAU 1-1...");
int i,scg;

pomsData[0]=tAnx[c2.results.getInt("sommeANX")];
pomsData[1]=tCol[c2.results.getInt("sommeCOL")];
pomsData[2]=tConf[c2.results.getInt("sommeCONF")];
pomsData[3]=tDep[c2.results.getInt("sommeDEP")];
pomsData[4]=tFat[c2.results.getInt("sommeFAT")];
pomsData[5]=tVig[c2.results.getInt("sommeVIG")];
pomsData[6]=tRel[c2.results.getInt("sommeREL")];

scg=0;
scg+=c2.results.getInt("sommeANX");
scg+=c2.results.getInt("sommeCOL");
scg+=c2.results.getInt("sommeCONF");
scg+=c2.results.getInt("sommeDEP");
scg+=c2.results.getInt("sommeFAT");
scg-=c2.results.getInt("sommeREL");

//System.out.println("DRAPEAU 2...");
titreA[0]="Anxiete";
titreA[1]="Colere";
titreA[2]="Confusion";
titreA[3]="Depression";
titreA[4]="Fatigue";
titreA[5]="Vigueur";
titreA[6]="Relationel";

for(i=0;i<=6;i++) {
pomsLigne[i]=50;
}//fin de for i

System.out.println("Score POMS : "+scg);
//System.out.println("DRAPEAU 4...");
Abscisse CIAbscisse = new Abscisse(7,titreA,0,7,0);
Donnee pomsDonnee[];
Donnee pomsd = new Donnee("Scores",7,pomsData,1);
Donnee pomsl= new Donnee("Limite",7,pomsLigne,2);
Donnee CIDonnee[];
CIDonnee = new Donnee[2];
CIDonnee[0]=pomsd;
CIDonnee[0].setCouleur(51,255,51);
CIDonnee[0].setTitre("Les scores");
CIDonnee[0].setMiniTitre("Sc");
CIDonnee[1]=pomsl;
CIDonnee[1].setCouleur(0,255,51);
CIDonnee[1].setTitre("La limite");
CIDonnee[1].setMiniTitre("Li");
AxeOrdonnees axeG = new AxeOrdonnees(0,100,0,5,1);
AxeOrdonnees axeD = new AxeOrdonnees(0,100,0,5,2);

String _tit="Resultat Poms de ";
_tit+=choixPrenom+" "+choixNom;
// Graphe2(String _titre, Abscisse _y, int _nbX, Donnee _x[], int _type, AxeOrdonnees _axe1, AxeOrdonnees _axe2){
//public AxeOrdonnees(float _reference, float vmx, float vmn,float _int,int _type) {
Graphe2 gPoms= new Graphe2(_tit,CIAbscisse, 2, CIDonnee,1,axeG,axeD);// 4 nombre de données, 1 type du graphe :)
ConfigureGraphe2 conGEssai=new ConfigureGraphe2(gPoms,"poms.cfg");
hide();
}//Fin de try
catch(Exception ex) {
System.out.println("PrepaGraphePOMS : CATCH sur exploitation des données !!! ");
}// Fin de catch

}//fin de public actionPerformed
}//Fin de la classe interne anonyme
);//Fin de l'appel vers addActionListener

c.add(panelTitre);
c.add(panneauEspace);
Box boiteV = Box.createVerticalBox();

JPanel panelGroupe= new JPanel(new FlowLayout(FlowLayout.LEFT));
panelGroupe.add(etiquetteGroupe);
panelGroupe.add(listeGroupe);
boiteV.add(panelGroupe);
JPanel panelNom= new JPanel(new FlowLayout(FlowLayout.LEFT));
panelNom.add(etiquetteNom);
panelNom.add(listeNom);
boiteV.add(panelNom);
JPanel panelPrenom= new JPanel(new FlowLayout(FlowLayout.LEFT));
panelPrenom.add(etiquettePrenom);
panelPrenom.add(listePrenom);
JPanel panelDate = new JPanel(new FlowLayout(FlowLayout.LEFT));
panelDate.add(etiquetteDate);
panelDate.add(listeDate);
boiteV.add(panelPrenom);
boiteV.add(panneauEspace2);
boiteV.add(panelDate);

JPanel panelValider = new JPanel(new FlowLayout(FlowLayout.CENTER));
panelValider.add(validerPrepa);
panelValider.setPreferredSize(new Dimension(mx-20,40));

c.add(boiteV);

//c.add(etiquetteNbJours);
//c.add(listeNbQDS);
//c.add(labelDernier);
c.add(panneauEspace3);
c.add(panelValider);

setVisible(true);

}//Fin de constructeur

// METHODES

public void determinerNumSportif() {
//determination du num du sportif
String query1="SELECT num FROM sportif WHERE nom='"+choixNom+"' AND prenom='"+choixPrenom+"' AND groupe='"+choixGroupe+"'";
Connexion c3 = new Connexion(0,query1);
try {
while (c3.results.next()) {
numSportif=c3.results.getInt("num");
System.out.println("Sportif sélectionné : "+numSportif);
batirListeDate(numSportif);
}// Fin de while connex.results.next()
}//Fin de try
catch(Exception ex) {
System.out.println("CATCH Determination de numSportif !!! ");
}// Fin de catch
}//Fin de determinerNumSportif

public void batirListeDate(int numsp) {
// Listage des dates des differents POMS
query="SELECT date FROM pomsrempli WHERE numsp='"+numsp+"' ORDER by date";
System.out.println(query);
Connexion connex2 = new Connexion(0,query);
try {System.out.println("je v nettoyer les dates");
listeDate.removeAllItems();
while (connex2.results.next()) {
listeDate.addItem(connex2.results.getDate("date").toString());
System.out.println(connex2.results.getDate("date"));
}// Fin de while connex.results.next()
listeDate.setSelectedIndex(0);
choixDate=listeDate.getSelectedItem().toString();
//System.out.println("Groupe sélectionné au départ : "+choixGroupe);
}
catch(Exception e){
System.out.println("CATCH dans la liste des dates du POMS");
};
}//Fin de batirListeDate

public void batirListePrenom(String chg,String chn) {
String q="SELECT prenom FROM sportif WHERE groupe='"+chg+"' AND nom='"+chn+"' ORDER BY prenom";
Connexion c3 = new Connexion(0,q);
try {
listePrenom.removeAllItems();
listeDate.removeAllItems();

while (c3.results.next()) {
listePrenom.addItem(c3.results.getString("prenom"));
}// Fin de while connex.results.next()
listePrenom.setSelectedIndex(0);
choixPrenom=listePrenom.getSelectedItem().toString();
System.out.println("je v déterminer le numsp à partir de batirlisteprenom ");
determinerNumSportif();
//determinerCaracSportif();
}//Fin de try
catch(Exception ex) {
System.out.println("CATCH batirListePrenom !!! ");
}// Fin de catch
System.out.println(" * "+choixGroupe+" * "+choixNom+" * "+choixPrenom+" * ");
}//Fin de batirListPrenom

public void batirListeNom(String chg) {
String q;
q="SELECT nom FROM sportif WHERE groupe='"+chg+"' ORDER BY nom";
Connexion c2 = new Connexion(0,q);
try {
listeNom.removeAllItems();
listePrenom.removeAllItems();
listeDate.removeAllItems();

while (c2.results.next()) {
listeNom.addItem(c2.results.getString("nom"));
}// Fin de while connex.results.next()
listeNom.setSelectedIndex(0);
choixNom=listeNom.getSelectedItem().toString();
}//Fin de try
catch(Exception ex) {
System.out.println("CATCH batirListeNom !!! ");
}// Fin de catch
System.out.println("je v btirlisteprenom à partir de batirlistenom ");
batirListePrenom(choixGroupe,choixNom);
}// Fin de methode batirListeNom

}//Fin de classe PrepaGraphe1

SOS!!!!!
0
Rejoignez-nous