1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
   | import java.sql.*;
import javax.swing.*;
 
public class Fenetre extends JFrame {
  Statement stm ;
  Connection con ;
  ResultSet  res ; 
  int i;
  int l ;
  public Fenetre(){
    this.setLocationRelativeTo(null);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("JTable");
    this.setSize(300, 120);
    try {
        Class.forName("com.mysql.jdbc.Driver");
        con=DriverManager.getConnection("jdbc:mysql://localhost/gestion_stagiaires","root","point."); 
         stm = con.createStatement() ;
         res=stm.executeQuery("Select nom,prenom from Stagiaire;");
         res.last();
         int x = res.getRow();
         res.first();
         Object[][] data = new Object [2][2];
         l = 1;
         while(res.next()) {
    data[l][1] = res.getString(1);
    System.out.print(data[l][1]);
    data[l][2]= res.getString(2);
    System.out.print(data[l][2]);
    l++;
    res.next();   }
 
    //Les titres des colonnes
    String  title[] = {"nom", "prenom"};
    JTable tableau = new JTable(data, title);
    //Nous ajoutons notre tableau à notre contentPane dans un scroll
    //Sinon les titres des colonnes ne s'afficheront pas !
    this.getContentPane().add(new JScrollPane(tableau)); }
    catch (Exception e) { 
            System.out.println("ERROR :"+e.getMessage());
        }  
  }   
 
  public static void main(String[] args){
    Fenetre fen = new Fenetre();
    fen.setVisible(true);
  }   
} | 
Partager