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
|
try
{
String url = "jdbc:postgresql://localhost:5432/videotheque";
String user = "postgres";
String passwd = "titi30501";
Connection conn = DriverManager.getConnection(url, user, passwd);
//création d'un objet Statement pour le nombre de film
Statement statenb = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
//On compte le nombre de film
String query1 = "SELECT COUNT(id_film) AS nbfilm FROM Film";
ResultSet result = statenb.executeQuery(query1);
result.first();
System.out.println(""+result.getInt("nbfilm")+"");
int nbfilm =result.getInt("nbfilm");
//création d'un objet Statement pour la liste des films
Statement state = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
//On va chercher tous les film pour les afficher
String query = "SELECT * FROM Film ORDER BY id_film";
ResultSet res = state.executeQuery(query);
res.first();
//Les titres des colonnes
String title[] = {"idfilm", "Nom", "Realisateur", "acteur principal", "genre", "Commentaire"};
//on insère les données dans un tableau
Object[][] data = {{""+res.getString("id_film")+"", ""+res.getString("nom_film")+"", ""+res.getString("realisateur")+"", ""+res.getString("acteur")+"", ""+res.getString("genre")+"", ""+res.getString("commentaire")+""}};
JTable tableau = new JTable(data, title);
//On ajoute notre tableau à notre contentPane dans un scroll
//Sinon les titres des colonnes ne s'afficheront pas ! !
this.getContentPane().add(new JScrollPane(tableau));
this.setLocationRelativeTo(null);
this.setTitle("JTable");
System.out.println(data);
/*
for(int i=1;i<=nbfilm;i++)
{
}*/
}
catch (Exception e)
{
e.printStackTrace();
} |
Partager