Je cherche à afficher le résultat d'une requete SELECT dans des JList

gharsih Messages postés 2 Date d'inscription mercredi 30 novembre 2011 Statut Membre Dernière intervention 3 juin 2012 - 23 mai 2012 à 00:19
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 - 25 mai 2012 à 09:06
je cherche à afficher le résultat d'une requete SELECT dans des JList voila mon code:

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JButton;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class consultation2 extends JFrame {

private static final long serialVersionUID = 1L;

private JPanel jContentPane = null;

private JLabel jLabel = null;

private JComboBox jComboBox = null;

private JLabel jLabel1 = null;

private JComboBox jComboBox1 = null;

private JList jList = null;

private JList jList1 = null;

private JList jList2 = null;

private JList jList3 = null;

private JList jList4 = null;

private JLabel jLabel2 = null;

private JLabel jLabel3 = null;

private JLabel jLabel4 = null;

private JLabel jLabel5 = null;

private JLabel jLabel6 = null;

private JButton jButton = null;

/**
* This is the default constructor
*/
public consultation2() {
super();
initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(862, 484);
this.setContentPane(getJContentPane());
this.setTitle("consultation");
}

/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel6 = new JLabel();
jLabel6.setBounds(new Rectangle(486, 103, 74, 16));
jLabel6.setText("année");
jLabel5 = new JLabel();
jLabel5.setBounds(new Rectangle(377, 102, 73, 16));
jLabel5.setText("mois");
jLabel4 = new JLabel();
jLabel4.setBounds(new Rectangle(289, 103, 61, 16));
jLabel4.setText("état ");
jLabel3 = new JLabel();
jLabel3.setBounds(new Rectangle(176, 100, 59, 16));
jLabel3.setText("type");
jLabel2 = new JLabel();
jLabel2.setBounds(new Rectangle(52, 99, 87, 16));
jLabel2.setText("nom_matériel");
jLabel1 = new JLabel();
jLabel1.setBounds(new Rectangle(21, 63, 87, 16));
jLabel1.setText("type matériels :");
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(21, 34, 86, 16));
jLabel.setText("nom_unité :");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getJComboBox(), null);
jContentPane.add(jLabel1, null);
jContentPane.add(getJComboBox1(), null);
jContentPane.add(getJList(), null);
jContentPane.add(getJList1(), null);
jContentPane.add(getJList2(), null);
jContentPane.add(getJList3(), null);
jContentPane.add(getJList4(), null);
jContentPane.add(jLabel2, null);
jContentPane.add(jLabel3, null);
jContentPane.add(jLabel4, null);
jContentPane.add(jLabel5, null);
jContentPane.add(jLabel6, null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}

/**
* This method initializes jComboBox
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox() {
if (jComboBox == null) {
jComboBox = new JComboBox();
jComboBox.setBounds(new Rectangle(118, 32, 131, 19));
String[] infos={"BCC","TST","GTD","EXPLOITATION","TRAVEAUX NOEUFS","LABORATOIRE"};
for(int i=0;i<infos.length;i++)
jComboBox.addItem(infos[i]);
jComboBox.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
String chh = (String)jComboBox.getSelectedItem();

if(chh.equals("BCC"))
{jComboBox1.removeAllItems();
String[] infos={"un","deux","trois","quatre"};
for(int i=0;i<infos.length;i++){

jComboBox1.addItem(infos[i]);}}

else if(chh.equals("TST"))

{jComboBox1.removeAllItems();
String[] infos={"Equipement du monteur","Perches d'effort et accessoires","Matériel de protection","Matériel de shuntage et de connexion"};
for(int i=0;i<infos.length;i++)
jComboBox1.addItem(infos[i]);}
}
});
}
return jComboBox;
}

/**
* This method initializes jComboBox1
*
* @return javax.swing.JComboBox
*/
private JComboBox getJComboBox1() {
if (jComboBox1 == null) {
jComboBox1 = new JComboBox();
jComboBox1.setBounds(new Rectangle(117, 59, 131, 18));
}
return jComboBox1;
}

/**
* This method initializes jList
*
* @return javax.swing.JList
*/
private JList getJList() {
if (jList == null) {
jList = new JList(ListModel l);
jList.setBounds(new Rectangle(42, 123, 110, 120));
}
return jList;
}

/**
* This method initializes jList1
*
* @return javax.swing.JList
*/
private JList getJList1() {
if (jList1 == null) {
jList1 = new JList();
jList1.setBounds(new Rectangle(152, 123, 111, 120));
}
return jList1;
}

/**
* This method initializes jList2
*
* @return javax.swing.JList
*/
private JList getJList2() {
if (jList2 == null) {
jList2 = new JList();
jList2.setBounds(new Rectangle(263, 123, 107, 120));
}
return jList2;
}

/**
* This method initializes jList3
*
* @return javax.swing.JList
*/
private JList getJList3() {
if (jList3 == null) {
jList3 = new JList();
jList3.setBounds(new Rectangle(370, 123, 111, 120));
}
return jList3;
}

/**
* This method initializes jList4
*
* @return javax.swing.JList
*/
private JList getJList4() {
if (jList4 == null) {
jList4 = new JList();
jList4.setBounds(new Rectangle(481, 123, 107, 120));
}
return jList4;
}

/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(401, 24, 90, 14));
jButton.setText("consulter");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Connection connect=null; // connexion avec la base
Statement S=null; // objet d'émission des requêtes
ResultSet RS; // table résultat d'une requête
try{
// connexion à la base

Class.forName("com.mysql.jdbc.Driver");
connect=DriverManager.getConnection("jdbc:mysql://localhost:3306/gestion_inv","root","");
System.out.println("pppppppppppppppppppppppppppConnexion avec la base gestion inventaire établie");
// création d'un objet Statement
S=connect.createStatement();
String unite = (String)jComboBox.getSelectedItem();
String type = (String)jComboBox1.getSelectedItem();

RS=S.executeQuery("SELECT * FROM matériel WHERE type_matériel='"+type+"'");
System.out.println("type_matériel");

//On affiche le nom des colonnes
int i=0;
// i indique le nombre de ligne
while(RS.next())
{
JList.setModel(model);
String nom=RS.getString();

(DefaultListModel)jList.getModel().add(nom);

}
} catch (Exception e){};
}
});
}
return jButton;
}
public static void main(String args[])
{
new consultation2().setVisible(true);
}

} // @jve:decl-index=0:visual-constraint="10,10"

1 réponse

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
25 mai 2012 à 09:06
Bonjour,

Et qu'est ce qui ne va pas avec ce code ?
0
Rejoignez-nous