JAVA application de gestion de commande .besoin d aide tres urgent

Signaler
Messages postés
4
Date d'inscription
vendredi 8 janvier 2021
Statut
Membre
Dernière intervention
14 janvier 2021
-
Messages postés
16342
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
19 mai 2021
-
Bonjour, en fait j ai fini de réaliser mon mini projet et j essaye de le tester. Jai crée un exécutable mais la quand j essayes de faire un teste ca me donne l erreur suivante:

<<cannot "invoke java.sql.Statement.executeUpdate(String)" because "this.st" is null>>



Pourtant quand j execute mon projet dans mon IDE(Netbeans) tout marche correctement.
sil vous plait jai beson d aide sur ca

2 réponses

Messages postés
16342
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
19 mai 2021
118
Bonjour,

Le message d'erreur est clair : this.st est null donc tu ne peux pas faire st.executeUpdate

Après, pourquoi c'est null, cela dépend de comment la variable aurait dû être initialisé, mais sans ton code, impossible de t'aider davantage.
Messages postés
4
Date d'inscription
vendredi 8 janvier 2021
Statut
Membre
Dernière intervention
14 janvier 2021

bonsoir voila mon code
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gestion;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 *
 */
public class gestioncommande extends javax.swing.JFrame {
   
   

    //connecter c=new connecter();
    Statement st;
    ResultSet rs;
    DefaultTableModel model= new DefaultTableModel();
    public gestioncommande(){
        initComponents();
        model.addColumn("Id");
        model.addColumn("Poduit");
        model.addColumn("Prix");
        model.addColumn("Ville");
        model.addColumn("Adresse");
        model.addColumn("Date");
        try{Connection con;
            Class.forName("com.mysql.cj.jdbc.Driver");
    System.out.println("connexion etablie");
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mini_pro","root", "");
            st= con.createStatement();
            rs=st.executeQuery("select * from commande");
            while(rs.next()){
        model.addRow(new Object[]{rs.getString("Id"),rs.getString("Produit"),rs.getString("Prix"),rs.getString("Ville"),rs.getString("Adresse"),rs.getString("Date")});
        }
        }catch(Exception e){System.err.println(e);}
        Table.setModel(model);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */   @SuppressWarnings("unchecked")
    private void deplace(int i){
    try{
        txtid.setText(model.getValueAt(i,0).toString());
        txtproduit.setText(model.getValueAt(i,1).toString());
        txtprix.setText(model.getValueAt(i,2).toString());
        txtville.setSelectedItem(model.getValueAt(i,3).toString());
        txtadresse.setText(model.getValueAt(i,4).toString());
        txtdate.setText(model.getValueAt(i,5).toString());
    }catch(Exception e){
    System.err.println(e);
    JOptionPane.showMessageDialog(null," erreur de deplacement\n"+e.getLocalizedMessage());}
    }
      private void afficher(){
          try{
     model.setRowCount(0);
     Connection con;
            Class.forName("com.mysql.cj.jdbc.Driver");
    System.out.println("connexion etablie");
    con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mini_pro","root", "");
           // st= c.obtenirconnection().createStatement();
           st= con.createStatement();
            rs=st.executeQuery("select * from commande");
            while(rs.next()){
        model.addRow(new Object[]{rs.getString("Id"),rs.getString("Produit"),rs.getString("Prix"),rs.getString("Ville"),rs.getString("Adresse"),rs.getString("Date")});
        }
        }catch(Exception e){System.err.println(e);}
        Table.setModel(model);  
      
      }
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents(){

        jMenu7 = new javax.swing.JMenu();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Table = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        txtrecherche = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtdate = new javax.swing.JTextField();
        txtadresse = new javax.swing.JTextField();
        txtville = new javax.swing.JComboBox<>();
        txtprix = new javax.swing.JTextField();
        txtproduit = new javax.swing.JTextField();
        txtid = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();

        jMenu7.setText("jMenu7");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setSize(new java.awt.Dimension(0, 0));
        getContentPane().setLayout(null);

        Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6"
            }
        ));
        Table.addMouseListener(new java.awt.event.MouseAdapter(){
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(Table);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 4, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel2);
        jPanel2.setBounds(74, 372, 560, 110);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        getContentPane().add(jPanel1);
        jPanel1.setBounds(60, 100, 0, 0);

        jButton1.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gestion/icones/ajouter.png"))); // NOI18N
        jButton1.setText("Ajouter");
        jButton1.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(370, 130, 120, 30);

        jButton2.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gestion/icones/modifier.png"))); // NOI18N
        jButton2.setText("Modifier");
        jButton2.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(520, 130, 120, 30);

        jButton3.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gestion/icones/poubelle.png"))); // NOI18N
        jButton3.setText("Supprimer");
        jButton3.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3);
        jButton3.setBounds(366, 194, 120, 30);

        jButton4.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gestion/icones/actualiser.png"))); // NOI18N
        jButton4.setText("Actualiser");
        jButton4.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton4);
        jButton4.setBounds(520, 195, 120, 30);

        jButton5.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gestion/icones/recherche..png"))); // NOI18N
        jButton5.setText("Rechercher");
        jButton5.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton5);
        jButton5.setBounds(367, 260, 123, 30);

        txtrecherche.setText(" ");
        txtrecherche.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtrechercheActionPerformed(evt);
            }
        });
        txtrecherche.addKeyListener(new java.awt.event.KeyAdapter(){
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtrechercheKeyPressed(evt);
            }
        });
        getContentPane().add(txtrecherche);
        txtrecherche.setBounds(510, 260, 136, 30);

        jLabel7.setFont(new java.awt.Font("Times New Roman", 1, 36)); // NOI18N
        jLabel7.setText("gestion de commande");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(160, 30, 350, 40);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel1.setText("Id:");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(110, 100, 18, 17);

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel2.setText("Produit:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(110, 140, 50, 17);

        jLabel3.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel3.setText("Prix:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(110, 190, 30, 17);

        jLabel4.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel4.setText("Ville:");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(110, 220, 34, 17);

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel5.setText("Adresse:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(110, 260, 54, 17);

        jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel6.setText("Date:");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(110, 300, 34, 17);

        txtdate.setText(" ");
        getContentPane().add(txtdate);
        txtdate.setBounds(200, 300, 121, 28);

        txtadresse.setText(" ");
        txtadresse.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtadresseActionPerformed(evt);
            }
        });
        getContentPane().add(txtadresse);
        txtadresse.setBounds(200, 260, 121, 27);

        txtville.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        txtville.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Agadir", "Beni mella", "CAsablanca ", "Rabat", "Settat", "Fes", "Oujda", "Tetouan", " " }));
        txtville.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtvilleActionPerformed(evt);
            }
        });
        getContentPane().add(txtville);
        txtville.setBounds(200, 220, 121, 28);

        txtprix.setText(" ");
        getContentPane().add(txtprix);
        txtprix.setBounds(200, 180, 121, 28);

        txtproduit.setText(" ");
        txtproduit.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtproduitActionPerformed(evt);
            }
        });
        getContentPane().add(txtproduit);
        txtproduit.setBounds(200, 140, 121, 27);

        txtid.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtidActionPerformed(evt);
            }
        });
        getContentPane().add(txtid);
        txtid.setBounds(200, 100, 121, 28);

        jLabel8.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gestion/icones/commande-entreprises.jpg"))); // NOI18N
        getContentPane().add(jLabel8);
        jLabel8.setBounds(0, 0, 670, 510);

        jMenu1.setText("File");

        jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_J, java.awt.event.InputEvent.CTRL_DOWN_MASK));
        jMenuItem1.setText("Ajouter");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_DOWN_MASK));
        jMenuItem2.setText("Modifier");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_DELETE, 0));
        jMenuItem3.setText("Supprimer");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuItem4.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_DOWN_MASK));
        jMenuItem4.setText("Actualiser");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuItem5.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_DOWN_MASK));
        jMenuItem5.setText("Rechercher");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener(){
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem5);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        setBounds(0, 0, 684, 550);
    }// </editor-fold>                        

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

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        String Id=txtid.getText();
         String Produit=txtproduit.getText();
         String Prix=txtprix.getText();
         String Ville= txtville.getSelectedItem().toString();
         String Adresse=txtadresse.getText();
          String Date=txtdate.getText();
        String requete="insert into commande(Id,Produit,Prix,Ville,Adresse,Date)Values('"+Id+"','"+Produit+"','"+Prix+"','"+Ville+"','"+Adresse+"','"+Date+"')";
        try{
            st.executeUpdate(requete);
            JOptionPane.showMessageDialog(null,"commande ajouter");
            txtid.setText("");
             txtproduit.setText("");
             txtprix.setText("");
             txtville.setSelectedItem("");
             txtadresse.setText("");
             txtdate.setText("");
            afficher();
        }catch(Exception ex){JOptionPane.showMessageDialog(null, ex.getMessage());}
        
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        try{
            if(JOptionPane.showConfirmDialog(null,"supprimer la commande","suppression",JOptionPane.YES_NO_OPTION)==JOptionPane.OK_OPTION)
                if(txtid.getText().length()!=0){
                      String h1=txtid.getText();
                       h1="'"+h1+"'";  
                st.executeUpdate("delete from commande where Id = "+h1);
                afficher();
                } else{JOptionPane.showMessageDialog(null,"remplicez l Id");}
            
            
        }catch(Exception e){
        JOptionPane.showMessageDialog(null,"erreur de suppression\n"+e.getMessage());
        }
    }                                        

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   try{  
        {
            String h2=txtrecherche.getText();
        h2="'"+h2+"'";
            rs=st.executeQuery("select * from commande where Produit="+h2);
            model.setRowCount(0);
        } while(rs.next()){
           
        Object [] commande ={rs.getString(1),rs.getString(2),rs.getInt(3),rs.getString(4),rs.getString(5),rs.getString(6).toString()};
        model.addRow(commande);
          
        }if (model.getRowCount()==0){JOptionPane.showMessageDialog(null,"aucune commande");
        }else{int i=0;
        deplace(i);}
    }catch(Exception e){
    System.out.println(e);
    JOptionPane.showMessageDialog(null,e.getLocalizedMessage());
    }
/* String rech = txtrecherche.getText().toUpperCase();
        DefaultTableModel model = (DefaultTableModel) Table.getModel();
        TableRowSorter<DefaultTableModel> tr = new TableRowSorter<>(model);
        Table.setRowSorter(tr);
        tr.setRowFilter(javax.swing.RowFilter.regexFilter(rech));
        while(rs.next()){
           
        Object [] commande ={rs.getString(1),rs.getString(2),rs.getInt(3),rs.getString(4),rs.getString(5),rs.getString(6).toString()};
        model.addRow(commande);
          
        }if (model.getRowCount()==0){JOptionPane.showMessageDialog(null,"aucune commande");
        }else{int i=0;
        deplace(i);}
    }catch(Exception e){
    System.out.println(e);
    JOptionPane.showMessageDialog(null,e.getLocalizedMessage());*/
    }                                        

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

    private void TableMouseClicked(java.awt.event.MouseEvent evt) {                                   
     try{
         int i=Table.getSelectedRow();
         deplace(i);
     }catch(Exception e){
     JOptionPane.showMessageDialog(null,"erreur de deplacement "+e.getLocalizedMessage());}
    }                                  

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
     afficher(); 
        
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   try{if (JOptionPane.showConfirmDialog(null,"confirmer la modification","modification",JOptionPane.YES_NO_OPTION)==JOptionPane.OK_OPTION){
       String h=txtid.getText();
       h="'"+h+"'";
   st.executeUpdate("update commande set Produit='"+txtproduit.getText()+"',Prix='"+txtprix.getText()+"',Ville='"+txtville.getSelectedItem().toString()+"',Adresse='"+txtadresse.getText()+"',Date='"+txtdate.getText()+"'where Id="+h);;
   afficher();
   }}catch(Exception e){
           JOptionPane.showMessageDialog(null,"erreur de modification"+e.getLocalizedMessage());
           System.err.println(e);
           }
          
    }                                        

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

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
            String Id=txtid.getText();
         String Produit=txtproduit.getText();
         String Prix=txtprix.getText();
         String Ville= txtville.getSelectedItem().toString();
         String Adresse=txtadresse.getText();
          String Date=txtdate.getText();
        String requete="insert into commande(Id,Produit,Prix,Ville,Adresse,Date)Values('"+Id+"','"+Produit+"','"+Prix+"','"+Ville+"','"+Adresse+"','"+Date+"')";
        try{
            st.executeUpdate(requete);
            JOptionPane.showMessageDialog(null,"commande ajouter");
            txtid.setText("");
             txtproduit.setText("");
             txtprix.setText("");
             txtville.setSelectedItem("");
             txtadresse.setText("");
             txtdate.setText("");
            afficher();
        }catch(Exception ex){JOptionPane.showMessageDialog(null,"id existe deja  , changer l'Id"+ex.getMessage());}
        
    }                                          

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
         try{if (JOptionPane.showConfirmDialog(null,"confirmer la modification","modification",JOptionPane.YES_NO_OPTION)==JOptionPane.OK_OPTION){
       String h=txtid.getText();
       h="'"+h+"'";
   st.executeUpdate("update commande set Produit='"+txtproduit.getText()+"',Prix='"+txtprix.getText()+"',Ville='"+txtville.getSelectedItem().toString()+"',Adresse='"+txtadresse.getText()+"',Date='"+txtdate.getText()+"'where Id="+h);;
   afficher();
   }}catch(Exception e){
           JOptionPane.showMessageDialog(null,"erreur de modification"+e.getLocalizedMessage());
           System.err.println(e);
           }
          
    }                                          

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
         try{
            if(JOptionPane.showConfirmDialog(null,"supprimer la commande","suppression",JOptionPane.YES_NO_OPTION)==JOptionPane.OK_OPTION)
                if(txtid.getText().length()!=0){
                      String h1=txtid.getText();
                       h1="'"+h1+"'";  
                st.executeUpdate("delete from commande where Id = "+h1);
                afficher();
                } else{JOptionPane.showMessageDialog(null,"remplicez l Id");}
            
            
        }catch(Exception e){
        JOptionPane.showMessageDialog(null,"erreur de suppression\n"+e.getMessage());
        }
    }                                          

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

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        try{
          model.setRowCount(0);
       
        {
            String h2=txtrecherche.getText();
        h2="'"+h2+"'";
            rs=st.executeQuery("select * from commande where Produit="+h2);
          
        } while(rs.next()){
           
        Object [] commande ={rs.getString(1),rs.getString(2),rs.getInt(3),rs.getString(4),rs.getString(5),rs.getString(6).toString()};
        model.addRow(commande);
          
        }if (model.getRowCount()==0){JOptionPane.showMessageDialog(null,"aucune commande");
        }else{int i=0;
        deplace(i);}
    }catch(Exception e){
    System.out.println(e);
    JOptionPane.showMessageDialog(null,e.getLocalizedMessage());
    }
    }                                          

    private void txtrechercheKeyPressed(java.awt.event.KeyEvent evt) {                                        
        // TODO add your handling code here:
        if(evt.getKeyCode()==KeyEvent.VK_R)//quand on clique sur entrer ilva rechercher
        {
         try{
       
        {
            String h2=txtrecherche.getText();
        h2="'"+h2+"'";
            rs=st.executeQuery("select * from commande where Produit="+h2);
            model.setRowCount(0);
        } while(rs.next()){
           
        Object [] commande ={rs.getString(1),rs.getString(2),rs.getInt(3),rs.getString(4),rs.getString(5),rs.getString(6).toString()};
        model.addRow(commande);
          
        }if (model.getRowCount()==0){JOptionPane.showMessageDialog(null,"aucune commande");
        }else{int i=0;
        deplace(i);}
    }catch(Exception e){
    System.out.println(e);
    JOptionPane.showMessageDialog(null,e.getLocalizedMessage());
    }}
    }                                       

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

    /**
     * @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(gestioncommande.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(gestioncommande.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(gestioncommande.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(gestioncommande.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(){
                new gestioncommande().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JTable Table;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu7;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtadresse;
    private javax.swing.JTextField txtdate;
    private javax.swing.JTextField txtid;
    private javax.swing.JTextField txtprix;
    private javax.swing.JTextField txtproduit;
    private javax.swing.JTextField txtrecherche;
    private javax.swing.JComboBox<String> txtville;
    // End of variables declaration                   
}
 
Messages postés
16342
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
19 mai 2021
118
Supprimes les
Statement st;
et
ResultSet rs;
qui sont en ligne 29 et 30, ce sont des variables globales.
Il faudrait les remplacer par des variables locales, chacune étant initialisée à chaque appel parce que là tout se mélange un peu, parfois il en manque, et les erreurs s'enchaînent donc ça fait un peu n'importe quoi...

De plus, ne fait pas de
catch(Exception e){System.err.println(e);}
car ça veut dire que s'il y a une erreur tu vas continuer le programme comme s'il ne s'était rien passé alors qu'en vrai il faut arrêter le programme puisque toute la suite sera fausse.
De plus, le println(e) n'affiche que le message de l'exception alors qu'il y a d'autres informations importantes dans une exception, notamment la ligne où l'erreur s'est produite donc c'est important de garder l'intégralité de l'exception par exemple en faisant un
throw new IllegalStateException("un petit message", e);
(sans oublier le
,e
en second paramètre)