Results 1 to 2 of 2
  1. #1
    guns65 is offline Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Default table model probléme !

    hi :) can someone help me to make modification to my code so i can use table model im using table right now ans there is no synchronisation when i call my request !! this is my code !!

    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.se tText("");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(); }

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

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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

Similar Threads

  1. GMF Model & Table View
    By residentevil in forum SWT / JFace
    Replies: 2
    Last Post: 10-28-2012, 05:40 PM
  2. Best way to model table with header characters?
    By doa24uk in forum New To Java
    Replies: 1
    Last Post: 07-01-2011, 01:44 PM
  3. Copy Default table model to another default table model?
    By greatmajestics in forum AWT / Swing
    Replies: 2
    Last Post: 04-28-2010, 04:08 PM
  4. Replies: 1
    Last Post: 01-08-2010, 06:19 AM
  5. Table model
    By Manfizy in forum NetBeans
    Replies: 4
    Last Post: 11-08-2008, 02:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •