Résolution

Debuttant
Messages postés
1
Date d'inscription
mardi 16 octobre 2018
Statut
Membre
Dernière intervention
17 octobre 2018
- Modifié le 17 oct. 2018 à 08:42
Bonjour, je n'arrive pas afficher sur jTable la valeur entrée,mais les valeurs saisies affiche sur la base de donnée

package BaseDD; 


import BaseDD.Connexion; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.ArrayList; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JOptionPane; 
import javax.swing.table.DefaultTableModel; 

/** 
*
@author BOb Roots
/

public class addCli extends javax.swing.JFrame { 
Connection con; 
Statement smt; 
PreparedStatement pst; 
Connexion maconnexion =new Connexion(); 
ResultSet rs; 
public addCli() throws ClassNotFoundException { 
initComponents(); 
} 
public ArrayList<Client> clientList() throws ClassNotFoundException{ 
ArrayList<Client> clientList= new ArrayList<>(); 
try{ 
Class.forName("com.mysql.jdbc.Driver"); 
String urlBaseDonnee="jdbc:mysql://localhost/vmi"; 
con= (Connection) DriverManager.getConnection(urlBaseDonnee); 
String query = "SELECT *FROM client"; 
Statement smt = con.createStatement(); 
ResultSet rs= smt.executeQuery(urlBaseDonnee); 
Client client; 
while(rs.next()){ 
client=new Client(rs.getString("NumCLient"),rs.getString("Nom")); 
clientList.add(client); 
}}catch(SQLException ex) 
{ 
Logger.getLogger(addCli.class.getName()).log(Level.SEVERE,null,ex); 
} 
return clientList; 
} 
public void show_client() throws ClassNotFoundException 
{ 
ArrayList<Client> list = clientList(); 
DefaultTableModel model =(DefaultTableModel)tablec.getModel(); 
Object[] row =new Object [2]; 
for(int i=0;i<list.size();i++) 
{ 
row[0]=list.get(i).getNumCLient(); 
row[1]=list.get(i).getNom(); 
model.addRow(row); 
} 
} 

@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code"> 
private void initComponents() { 

jLabel2 = new javax.swing.JLabel(); 
txtnc = new javax.swing.JTextField(); 
txtn = new javax.swing.JTextField(); 
jLabel3 = new javax.swing.JLabel(); 
btna = new javax.swing.JButton(); 
btnm = new javax.swing.JButton(); 
btns = new javax.swing.JButton(); 
btnr = new javax.swing.JButton(); 
txtr = new javax.swing.JTextField(); 
jScrollPane1 = new javax.swing.JScrollPane(); 
tablec = new javax.swing.JTable(); 
jLabel1 = new javax.swing.JLabel(); 

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N 
jLabel2.setText("NumClient"); 

txtn.addActionListener(new java.awt.event.ActionListener() { 
public void actionPerformed(java.awt.event.ActionEvent evt) { 
txtnActionPerformed(evt); 
} 
}); 

jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N 
jLabel3.setText("Nom"); 

btna.setText("Ajouter"); 
btna.addActionListener(new java.awt.event.ActionListener() { 
public void actionPerformed(java.awt.event.ActionEvent evt) { 
btnaActionPerformed(evt); 
} 
}); 

btnm.setText("Modifier"); 

btns.setText("Supprimer"); 

btnr.setText("Rechercher"); 

tablec.setModel(new javax.swing.table.DefaultTableModel( 
new Object [][] { 

}, 
new String [] { 
"NumClient", "Nom" 
} 
) { 
boolean[] canEdit = new boolean [] { 
false, false 
}; 

public boolean isCellEditable(int rowIndex, int columnIndex) { 
return canEdit [columnIndex]; 
} 
}); 
tablec.addMouseListener(new java.awt.event.MouseAdapter() { 
public void mouseClicked(java.awt.event.MouseEvent evt) { 
tablecMouseClicked(evt); 
} 
}); 
jScrollPane1.setViewportView(tablec); 

jLabel1.setFont(new java.awt.Font("Tahoma", 3, 48)); // NOI18N 
jLabel1.setForeground(new java.awt.Color(255, 0, 153)); 
jLabel1.setText(" Client"); 

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
getContentPane().setLayout(layout); 
layout.setHorizontalGroup( 
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
.addGroup(layout.createSequentialGroup() 
.addGap(53, 53, 53) 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
.addGroup(layout.createSequentialGroup() 
.addComponent(btna, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE) 
.addGap(100, 100, 100) 
.addComponent(btnm) 
.addGap(81, 81, 81) 
.addComponent(btns) 
.addContainerGap()) 
.addGroup(layout.createSequentialGroup() 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE) 
.addComponent(jLabel3)) 
.addGap(45, 45, 45) 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
.addComponent(txtnc, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) 
.addComponent(txtn, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)) 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
.addGroup(layout.createSequentialGroup() 
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
.addComponent(btnr) 
.addGap(90, 90, 90) 
.addComponent(txtr, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE) 
.addGap(44, 44, 44)) 
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE) 
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 496, javax.swing.GroupLayout.PREFERRED_SIZE) 
.addContainerGap(34, Short.MAX_VALUE)))))) 
.addGroup(layout.createSequentialGroup() 
.addGap(356, 356, 356) 
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE) 
.addGap(0, 0, Short.MAX_VALUE)) 
); 
layout.setVerticalGroup( 
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
.addGroup(layout.createSequentialGroup() 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
.addGroup(layout.createSequentialGroup() 
.addGap(109, 109, 109) 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
.addComponent(txtnc, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE) 
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)) 
.addGap(74, 74, 74) 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
.addComponent(txtn, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) 
.addComponent(jLabel3))) 
.addGroup(layout.createSequentialGroup() 
.addComponent(jLabel1) 
.addGap(29, 29, 29) 
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE))) 
.addGap(57, 57, 57) 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
.addComponent(btns, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) 
.addComponent(btnm, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) 
.addComponent(btna, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)) 
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
.addComponent(txtr, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btnr, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)) 
.addContainerGap(124, Short.MAX_VALUE)) 
); 

pack(); 
}// </editor-fold> 

private void txtnActionPerformed(java.awt.event.ActionEvent evt) { 
// TODO add your handling code here: 
} 

private void btnaActionPerformed(java.awt.event.ActionEvent evt) { 

try{ 
String NumCLient = txtnc.getText(); 
String Nom = txtn.getText(); 
// String verify= "SELECT COUNT(*) FROM <client> WHERE identifiant = '<NumCLient>'"; 
// smt.executeUpdate(verify); 
/* if(verify=){ 
JOptionPane.showMessageDialog(null,"Numéro déjà existé"); 
} 
else{*/ 

String requete = "INSERT INTO client (NumCLient,Nom)VALUES('"+NumCLient+"','"+Nom+"')"; 
smt= maconnexion.ObtenirConnexion().createStatement(); 
//smt.executeQuery(query); 
smt.executeUpdate(requete); 
DefaultTableModel model =(DefaultTableModel)tablec.getModel(); 
model.addRow(new Object[]{txtnc.getText(),txtn.getText()}); 
JOptionPane.showMessageDialog(null,"Client Ajouté avec succès"); 
txtnc.setText(""); 
txtn.setText(""); 



} 
catch(SQLException ex){ 
System.out.print(ex); 
} 

} 

private void tablecMouseClicked(java.awt.event.MouseEvent evt) { 

int rowIndex=tablec.getSelectedRow(); 
txtnc.setText(tablec.getValueAt(rowIndex,0).toString()); 
txtn.setText(tablec.getValueAt(rowIndex,1).toString()); 
} 

/**
@param args the command line arguments
/

public static void main(String args[]) { 
/* Set the Nimbus look and feel */ 
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
/

try { 
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
if ("Nimbus".equals(info.getName())) { 
javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
break; 
} 
} 
} catch (ClassNotFoundException ex) { 
java.util.logging.Logger.getLogger(addCli.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
} catch (InstantiationException ex) { 
java.util.logging.Logger.getLogger(addCli.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
} catch (IllegalAccessException ex) { 
java.util.logging.Logger.getLogger(addCli.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
} catch (javax.swing.UnsupportedLookAndFeelException ex) { 
java.util.logging.Logger.getLogger(addCli.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
} 
//</editor-fold> 

/* Create and display the form */ 
java.awt.EventQueue.invokeLater(new Runnable() { 
public void run() { 
try { 
new addCli().setVisible(true); 
} catch (ClassNotFoundException ex) { 
Logger.getLogger(addCli.class.getName()).log(Level.SEVERE, null, ex); 
} 
} 
}); 
} 

// Variables declaration - do not modify 
private javax.swing.JButton btna; 
private javax.swing.JButton btnm; 
private javax.swing.JButton btnr; 
private javax.swing.JButton btns; 
private javax.swing.JLabel jLabel1; 
private javax.swing.JLabel jLabel2; 
private javax.swing.JLabel jLabel3; 
private javax.swing.JScrollPane jScrollPane1; 
private javax.swing.JTable tablec; 
private javax.swing.JTextField txtn; 
private javax.swing.JTextField txtnc; 
private javax.swing.JTextField txtr; 
// End of variables declaration 
}


EDIT : Code remis dans la bonne discussion par un modérateur....