Tablemodel !!

Résolu/Fermé
guns65 Messages postés 32 Date d'inscription mardi 28 septembre 2010 Statut Membre Dernière intervention 26 février 2014 - 4 janv. 2013 à 17:48
guns65 Messages postés 32 Date d'inscription mardi 28 septembre 2010 Statut Membre Dernière intervention 26 février 2014 - 4 janv. 2013 à 19:53
salut !! j'ai tou essayé pour faire marcher la table model mais je n'arive pas si vous pouvez m'aider pour me dire ou modifier mon code !! il marche bien mais juste il fiche la base sur le jtable sans actualisation !!

voici une de mes classe !!

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.*;
//import javax.swing.table.TableColumn;
//import javax.swing.table.DefaultTableModel;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////onglet fournisseur//////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class Fournisseur extends JPanel implements ActionListener {
public Connection con ;
public Statement st ;
public ResultSet rs ;




JOptionPane option = new JOptionPane();
JLabel l1 = new JLabel(" Id-F");
JLabel l2 = new JLabel(" Nom-F");
JLabel l3 = new JLabel(" Prenom-F");
JLabel l4 = new JLabel(" Adresse");
JLabel l5 = new JLabel(" N° Telephone");
JLabel l6 = new JLabel("total des fournisseurs");
JLabel l7 = new JLabel("");
JLabel l8 = new JLabel("");

JTextField t1 = new JTextField();
JTextField t2 = new JTextField();
JTextField t3 = new JTextField();
JTextField t4 = new JTextField();
JTextField t5 = new JTextField();
JTextField t6 = new JTextField();


JButton bt1 = new JButton("Ajouter");
JButton bt2 = new JButton("Supprimer");
JButton bt3 = new JButton("Selectionner");
JButton bt4 = new JButton("update");

public JPanel p1 = new JPanel();
public JPanel p2 = new JPanel();
public JPanel p3 = new JPanel();
public JPanel p4 = new JPanel();

////////////////////////////////////////////////////////////////////jtable///////////////


public int i,j;

Object[][] data = new Object[100][100];
String title[] = {"id", "nom","prenom","adresse","n°telephone"};
JTable table = new JTable(data, title);

//DefaultTableModel myData = new DefaultTableModel();
// JTable table = new JTable(myData);



/////////////////////////////////////////////////////////////////////////////////////////
public Fournisseur() {



p3.setLayout(new GridLayout(1,2,20,20));
p1.setLayout(new GridLayout(6, 3, 15,15));
p1.add(l1);
p1.add(t1);
p1.add(bt1);
p1.add(l2);
p1.add(t2);
p1.add(bt2);
p1.add(l3);
p1.add(t3);
p1.add(bt3);
p1.add(l4);
p1.add(t4);
p1.add(bt4);
p1.add(l5);
p1.add(t5);
p1.add(l7);
p1.add(l6);
p1.add(t6);

p3.add(p1);
p3.add(p2);


//////////////////////////////////////////////////////////////
JScrollPane scroll = new JScrollPane(table);
p2.add(BorderLayout.EAST,scroll);
table.setPreferredScrollableViewportSize(new Dimension (400,200));
table.setFillsViewportHeight(true);


////////////////////////////////////////////////////////////////

bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);

p1.setBackground(Color.orange);
p1.setSize(400,400);

this.add(p3);
this.setSize(400,400);
this.con = con;
}

/////////////////////////////////////connection////////////////////////////////////////////////////////////////////

public void connection(){


try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e5){
System.out.println("impossible de chargern les drivers");
}
try{
String url="jdbc:odbc:test";
con=DriverManager.getConnection(url, "","");
}
catch(SQLException e5)
{
System.out.println("impossible de créer une connexion");
}
}
//////////////////////////////////////////////

//////////////////////////////////////fermeture/////////////////////////////////////
public void fermeture(){

try {
rs.close();
st.close();
con.close();
}
catch(SQLException e){}
}
/////////////// recuperer le text dans le textfield ///////////////////////////////////////////////////////////////////

public String gett1() {

return t1.getText();
}
//////
public String gett2() {

return t2.getText();
}
//////
public String gett3() {

return t3.getText();
}
//////
public String gett4() {

return t4.getText();
}
//////
public String gett5() {

return t5.getText();
}

//////////////////////////////executer le requete lors de lappuis du bouton //////////////////////////////////////////

public void actionPerformed(ActionEvent e){

Object src=e.getSource();
if(src instanceof JButton){
JButton var=(JButton) src;
///////////////////////ajouter//////////////////////
if (var==bt1) {
try
{
connection();
st=con.createStatement();
String req ="INSERT INTO Fournisseur VALUES('"+gett1()+"','"+gett2()+"','"+gett3()+"','"+gett4()+"','"+gett5()+"')";
int c =st.executeUpdate(req);
t1.setText("");t2.setText("");t3.setText("");t4.setText("");t5.setText("");
option.showMessageDialog(this,"Fournisseur ajouté avec succes");
fermeture();
}
catch(SQLException e1){
e1.printStackTrace();
option.showMessageDialog(this, "Erreur d'ajout.", "Inane error", JOptionPane.ERROR_MESSAGE);////msg d'erreur//////
}
}
////////////////////supprimer//////////////
else if (var==bt2){
try
{
connection();
st=con.createStatement();
String req2="DELETE * FROM Fournisseur WHERE prenom =('"+gett3()+"') ";
int b =st.executeUpdate(req2);
t3.setText("");
option.showMessageDialog(this,"Fournisseur supprimé avec succes");

}
catch(SQLException e2)
{
e2.printStackTrace();
option.showMessageDialog(this, "Erreur de suppression!", "Inane error", JOptionPane.ERROR_MESSAGE);////msg d'erreur//////

}
fermeture();
}
///////////////////////////////////////////////////select////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
else if (var==bt3){


connection();
////////////////////////////////////// select nom////////////////////////////
if (gett1().isEmpty() && gett2().isEmpty() && gett3().isEmpty() && gett4().isEmpty() && gett5().isEmpty())
{
try
{
st=con.createStatement();
String req="select* from Fournisseur ";
rs=st.executeQuery(req);

int C=0;
for (int i=0; rs.next(); i++)
for (int j=0; j<title.length; j++)
{

data[i][j] = rs.getString(title[j]);
C=C+1;
}
t6.setText(String.valueOf(C/5));
TableColumn column = null;
for(int i=0;i<5 ; i++){
column= table.getColumnModel().getColumn(i);
column.setPreferredWidth(180);

}

}

catch(SQLException e3){ e3.printStackTrace(); }

}
/////////prenom////////
else if (gett1().isEmpty() && gett2().isEmpty() && !gett3().isEmpty() && gett4().isEmpty() && gett5().isEmpty())
{
try {

st=con.createStatement();
String req="select* from Fournisseur WHERE prenom =('"+gett3()+"') ";
rs=st.executeQuery(req);

int C=0;
for (int i=0; rs.next(); i++)
for (int j=0; j<title.length; j++)
{

data[i][j] = rs.getString(title[j]);

C=C+1;
}

t6.setText(String.valueOf(C/5));
TableColumn column = null;
for(int i=0;i<5 ; i++){
column= table.getColumnModel().getColumn(i);
column.setPreferredWidth(180);
}
}
catch(SQLException e3){ e3.printStackTrace(); }
}


//////////////////////////////////////////////////////////////////////////////
fermeture();
}
/////////////////////////////////////update////////////////////////////////////////////////
else if (var==bt4){
try
{
connection();

if (!gett1().isEmpty() && !gett2().isEmpty() && gett3().isEmpty() && gett4().isEmpty() && gett5().isEmpty())
{
st=con.createStatement();
String req="UPDATE Fournisseur SET nom='"+gett2()+"' WHERE id= "+gett1();
st.executeUpdate(req);
}
else if (!gett1().isEmpty() && gett2().isEmpty() && !gett3().isEmpty() && gett4().isEmpty() && gett5().isEmpty())
{
st=con.createStatement();
String req1="UPDATE Fournisseur SET prenom='"+gett3()+"' WHERE id= "+gett1();
st.executeUpdate(req1);
}
else if (!gett1().isEmpty() && gett2().isEmpty() && gett3().isEmpty() && !gett4().isEmpty() && gett5().isEmpty())
{
st=con.createStatement();
String req2="UPDATE Fournisseur SET adresse='"+gett4()+"' WHERE id= "+gett1();
st.executeUpdate(req2);
}
else if (!gett1().isEmpty() && gett2().isEmpty() && gett3().isEmpty() && gett4().isEmpty() && !gett5().isEmpty())
{
st=con.createStatement();
String req3="UPDATE Fournisseur SET n°telephone='"+gett5()+"' WHERE id= "+gett1();
st.executeUpdate(req3);
}
}
catch(SQLException e4){e4.printStackTrace(); }

}
}
////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
} }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////

1 réponse

KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
4 janv. 2013 à 18:42
Evidemment il faudrait tester avec ta BDD, mais normalement ça donne quelque chose comme ceci :

private static final String title[] = {"id", "nom","prenom","adresse","n°telephone"};
private final DefaultTableModel myData;
private final JTable table;

public Fournisseur() 
{
    myData = new DefaultTableModel();
    myData.setColumnIdentifiers(title);
    table = new JTable(myData);
    
    ...

Attention, après tu as des lignes qui utilisent data[][] et qu'il faut remplacer car l'intérêt du TableModel est de ne plus avoir ce tableau data[100][100].

//int C=0;
//for (int i=0; rs.next(); i++)
//for (int j=0; j<title.length; j++)
//{
//    data[i][j] = rs.getString(title[j]); 
//    C=C+1; 
//}

int C=0;
for (; rs.next(); C++)
{
    String[] rowData = new String[title.length];
    
    for (int j=0; j<title.length; j++)
        rowData[j] = rs.getString(title[j]);
    
    myData.addRow(rowData);
}
0
guns65 Messages postés 32 Date d'inscription mardi 28 septembre 2010 Statut Membre Dernière intervention 26 février 2014
4 janv. 2013 à 18:51
je declare data comment i me dit !!
error: array dimension missing Object[][] data = new Object[][];

merci beacoup !!
0
guns65 Messages postés 32 Date d'inscription mardi 28 septembre 2010 Statut Membre Dernière intervention 26 février 2014
4 janv. 2013 à 19:13
je l'ai chage par Object[][] data = new Object[n][];
mais quand je selectione un nom sa apparait mais quand je selectione a nouveau sa aparait sous les autre !! mois je veux efacer l'ancienne requete et aficher la nouvelle et quand je duprime rien ne marche ca reste dans le stableau :)
0
KX Messages postés 16734 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 24 avril 2024 3 015
4 janv. 2013 à 19:28
Si tu utilises un TableModel, tu n'as plus besoin de data[][], tu peux le supprimer partout.

Pour effacer les lignes tu dois pouvoir faire comme ça :

myData.setRowCount(0);

Si ça ne fonctionne pas, tu peux forcer la suppression avec :

while (myData.getRowCount()!=0)
	myData.removeRow(0);
0
guns65 Messages postés 32 Date d'inscription mardi 28 septembre 2010 Statut Membre Dernière intervention 26 février 2014
4 janv. 2013 à 19:53
ca marche :) tu ma beacoup aidé merci kx !!
0