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 49 50 51 52 53
|
public Fenetre(){
this.setTitle("Liste des film");
this.setSize(600, 140);
int compteurDeLigne = 0;
try
{
String url = "jdbc:postgresql://localhost:5432/videotheque";
String user = "postgres";
String passwd = "******";
Connection conn = DriverManager.getConnection(url, user, passwd);
//création d'un objet Statement pour la liste des films
Statement state = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
//On va chercher tous les film pour les afficher
String query = "SELECT * FROM Film ORDER BY id_film";
ResultSet res = state.executeQuery(query);
res.last();//on va à la dernière ligne
int nbLignes = res.getRow(), //donne la ligne courante
compteurDeLignes = 0;
res.first();//on se replace à la première ligne
//Les titres des colonnes
String title[] = {"idfilm", "Nom", "Realisateur", "acteur principal", "genre", "Commentaire"};
Object [][] data = new Object[nbLignes][title.length];//on va stocker nos données dedans
JTable films = new JTable(data, title);
while(res.next()){
//on insère les données dans un tableau
for(int i=0; i<title.length;i++){
data[compteurDeLigne][i] = res.getString(i+1);
//System.out.println(getString(i));
}
compteurDeLigne++;
}
this.getContentPane().add(new JScrollPane(films));
this.setLocationRelativeTo(null);
this.setTitle("JTable");
}
catch (Exception e)
{
e.printStackTrace();
}
} |