public class ListeClient extends JPanel { final private static String url = "jdbc:mysql://localhost/db_gestion_veh"; Connection ctx; Statement stmt; JButton SupprimerButton; Vector ligne; JTable table; // Construction de l'interface public ListeClient() { // Le composant JTable table = remplirTable(); table.setPreferredScrollableViewportSize(new Dimension(620,70)); JScrollPane panneau = new JScrollPane(table); add(panneau, BorderLayout.CENTER); table.addMouseListener( new RefSouris(table) ); SupprimerButton = new JButton("Supprimer Client"); SupprimerButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent event) { SwingUtilities.invokeLater(new Runnable(){ public void run() { SupprimerClient(); } }); } }); add(SupprimerButton); } public void windowClosing(WindowEvent e){ System.exit(0); } /** Requête et résultats dans une JTable */ JTable remplirTable() { Vector tabLignes=new Vector(), nomCols=new Vector(); int i; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection ctx = DriverManager.getConnection (url,"root",""); // Création d'un objet 'requête' stmt = ctx.createStatement(); // Définir, envoyer la requête et récupérer le résultat ResultSet res = stmt.executeQuery("Select * From tableclient"); // Extraire noms des colonnes; les placer dans Vector nomCols ResultSetMetaData rsmd = res.getMetaData(); nomCols = new Vector(); for( i=1; i<=rsmd.getColumnCount (); i++) nomCols.add( rsmd.getColumnLabel(i)); // Mémoriser le résultat dans la JTable while( res.next()) { // placer les valeurs dans Vector ligne ligne=new Vector(); for(i=1; i<=7; i++) { String ch=res.getString(i); ligne.add(ch); } tabLignes.add(ligne); } // Fermer la connexion ctx.close(); } catch(Exception e) { e.printStackTrace(); } return new JTable(tabLignes,nomCols); } // méthode pour detruire une cellule private void SupprimerClient(){ ((DefaultTableModel)table.getModel()).removeRow(table.getSelectedRow()); } /** 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(); //récupère le model de ma table 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 ListeClient
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questiondelete from tableclient where id=?
delete from tableclient where id = num