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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
import java.sql.*;
import oracle.jdbc.driver.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; // JTable, JScrollPane, JPanel, JFrame
import java.util.Vector;
public class Req2 extends JFrame {
// Construction de l'interface
public Req2() {
// Le composant JTable
JTable table = remplirTable();
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
// Un panneau , placé dans la fenêtre
JScrollPane panneau = new JScrollPane(table);
getContentPane().add(panneau, BorderLayout.CENTER);
// Réflexe sur la table: Clic souris --> affichage des valeurs
table.addMouseListener(new RefSouris(table));
// Pour fermer la fenêtre
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
}
// Méthode Principale
public static void main(String[] args) {
Req2 fen = new Req2();
fen.pack();
fen.setLocation(100, 100);
fen.setVisible(true);
}
/** Requête et résultats dans une Jtable */
JTable remplirTable() {
Vector tabLignes=new Vector(), nomCols=new Vector();
int i;
try {
// Charger le pilote jdbc - odbc
Class pil = Class.forName("oracle.jdbc.driver");
// Connexion avec choix du pilote
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@asus1:1521:XE","hr","hr");
// Création d'un objet 'requête'
Statement req = con.createStatement();
// Définir, envoyer la requête et récupérer le résultat
ResultSet res = req.executeQuery("Select * from Employees");
// Extraire noms des colonnes; les placer dans Vector nomCols
ResultSetMetaData md = res.getMetaData();
nomCols = new Vector();
for ( i=1 ; i<= md.getColumnCount (); i++)
nomCols.add( md.getColumnLabel(i));
// Mémoriser le résultat dans la JTable
while(res.next()) {
// Placer les 5 valeurs dans Vector ligne
Vector ligne=new Vector();
for (i=1; i<=5; i++) {
String ch=res.getString(i);
ligne.add(ch);
}
tabLignes.add(ligne);
}
// Fermer la connexion
con.close();
}
catch(Exception e) { e.printStackTrace(); }
return new JTable(tabLignes,nomCols);
}
/** Réflexe associé au clic souris ( classe interne)
Un champ mémorise la table swing
*/
class RefSouris extends MouseAdapter {
private JTable table;
RefSouris(JTable t) {table=t; }
public void mouseClicked(MouseEvent e) {
AfficherDonnees();
}
private void AfficherDonnees() {
int i,j, numLigs = table.getRowCount(),
numCols = table.getColumnCount();
javax.swing.table.TableModel modele = table.getModel();
a("\nLa table:");
for ( i=0; i < numLigs; i++) {
a("\n ligne " + i + ":");
for ( j=0; j < numCols; j++)
System.out.print(" " + modele.getValueAt(i, j));
}
a("\n");
}
} // fin Class RefSouris
/** Utilitaire: affichage */
static void a( String txt) {System.out.print(txt);}
/** Utilitaire: message lié à une exception */
static void a( String txt, Exception e) {
a(txt+" ");
if( e!= null) {
a(e.getLocalizedMessage()+"\n --> " + e.toString());
// e.printStackTrace();
}
}
/*************************************************************/
} // fin class Req2 |
Partager