Création Jtable avec une base de données

Résolu
Shakaso Messages postés 4 Statut Membre -  
Shakaso Messages postés 4 Statut Membre -
Bonjour à tous.

J'ai un petit soucis sous netbeans. J'ai créé un problème pour faire un tableau (Jtable) contenant des données récupérés grâce à une requête dans une base de données. Ma connexion fonctionne, je pense que ma recherche est correcte.
Dans ma base de données j'ai une liste de médicaments ( nom,exploitant,stock ).
J'aimerais afficher dans le tableau la liste des médicaments ayant un stock inférieur à 10.
Mon problème est situé au niveau de l'utilisation de Object[][] =. J'ai remarqué que l'on doit donner explicitement les données à rajouter. Que puis-je faire?
Voici mes deux classes que j'utilise pour l'instant:

public static ArrayList Afficheproduitcommande(){
ArrayList Res = new ArrayList();
try {
Connection con = Connexion.defaultCon;
Statement sta = con.createStatement();
String Aproduitcommande = "SELECT nom, exploitant, stock FROM PRODUIT WHERE stock<=10";
ResultSet rs = sta.executeQuery(Aproduitcommande);
while (rs.next()){
String apc1 = rs.getString("nom");
String apc2 = rs.getString("exploitant");
String apc3 = rs.getString("stock");
Res.add(apc1 + ' ' + apc2 + ' ' + apc3);
}
return Res;
} catch (Exception ex) {
throw new Error(ex);
}
}

Dans la seconde classe:

public class Commande extends JFrame {
public Commande(){
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Pharma & cie");
this.setSize(300, 100);
String title[] = {"Produit", "Exploitant", "Stock"};
JTable tableau = new JTable();
int i;
for (i = 0; i < Connexion.Afficheproduitcommande().size(); i++) {
Object[][] =
}
this.getContentPane().add(new JScrollPane(tableau));
}
public static void main(String[] args){
Commande fen = new Commande();
fen.setVisible(true);
}
}

Merci d'avance.
Configuration: Linux
Firefox 3.0.8

1 réponse

  1. Shakaso Messages postés 4 Statut Membre
     
    Bon j'ai réussi en passant par une autre méthode.

    public static ArrayList Afficheproduitcommande(){
    ArrayList <String[]> Res = new ArrayList();
    try {
    Connection con = Connexion.defaultCon;
    Statement sta = con.createStatement();
    String Aproduitcommande = "SELECT nom, exploitant, stock FROM PRODUIT WHERE stock<=10";
    ResultSet rs = sta.executeQuery(Aproduitcommande);
    String[] Stack;
    while (rs.next()){
    Stack = new String[3];
    Stack[0] = rs.getString("nom");
    Stack[1] = rs.getString("exploitant");
    Stack[2] = rs.getString("stock");
    Res.add(Stack);
    }
    return Res;
    } catch (Exception ex) {
    throw new Error(ex);
    }
    }

    public class Commande extends JFrame {
    public Commande(){
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Pharma & cie");
    this.setSize(300, 100);

    ArrayList <String[]> al = Connexion.Afficheproduitcommande();

    String[][] col = new String[al.size()][3];
    for(int i = 0; i < al.size(); i++){
    col[i] = al.get(i);
    }
    String title[] = {"Produit", "Exploitant", "Stock"};
    JTable tableau = new JTable(col, title);
    this.getContentPane().add(new JScrollPane(tableau));
    }

    public static void main(String[] args){
    Commande fen = new Commande();
    fen.setVisible(true);
    }
    }
    0