IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JDBC Java Discussion :

Enregistrement des données d'un formulaire en base de données


Sujet :

JDBC Java

  1. #1
    Membre du Club
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Juillet 2015
    Messages
    53
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Mali

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : Conseil

    Informations forums :
    Inscription : Juillet 2015
    Messages : 53
    Points : 53
    Points
    53
    Par défaut Enregistrement des données d'un formulaire en base de données
    Bonjour,

    Je suis sur une application et je suis en train de faire un formulaire que je dois enregistrer dans une base de donnée MySQL.

    La connexion à la base de données passe mais l'enregistrement ne passe pas.

    Nom : ok (2).png
Affichages : 4170
Taille : 116,7 Ko

    Quelqu'un saurait-il m'indiquer comment résoudre ce problème ?

    Merci d'avance pour votre aide.

  2. #2
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    Bonjour,

    Peux-tu nous mettre un peu de code pour qu'on puisse voir où est-ce que tu es bloqué ?

  3. #3
    Membre du Club
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Juillet 2015
    Messages
    53
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Mali

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : Conseil

    Informations forums :
    Inscription : Juillet 2015
    Messages : 53
    Points : 53
    Points
    53
    Par défaut
    // voici le code merci d'avance
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    package guimob;
     
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.text.SimpleDateFormat;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JOptionPane;
     
     
    public class Formulaire extends javax.swing.JFrame {
     
        Connection conn = new DBconnecte().connect();
        Statement st;
        PreparedStatement ps;
     
     
        public Formulaire() {
            initComponents();
        }
     
     
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {
     
            jDateChooser1 = new com.toedter.calendar.JDateChooser();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jLabel9 = new javax.swing.JLabel();
            jPanel1 = new javax.swing.JPanel();
            jTadresse = new javax.swing.JTextField();
            jLabel3 = new javax.swing.JLabel();
            jLabel7 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            jLabel6 = new javax.swing.JLabel();
            jLabel8 = new javax.swing.JLabel();
            jTprofession = new javax.swing.JTextField();
            jComboBox1 = new javax.swing.JComboBox();
            jTlieu = new javax.swing.JTextField();
            jLabel1 = new javax.swing.JLabel();
            jDateChooser2 = new com.toedter.calendar.JDateChooser();
            jLabel5 = new javax.swing.JLabel();
            jTprenom = new javax.swing.JTextField();
            jTid = new javax.swing.JTextField();
            jTnom = new javax.swing.JTextField();
            jLabel2 = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setPreferredSize(new java.awt.Dimension(600, 500));
     
            jButton1.setText("Ajouter");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
     
            jButton2.setText("Modifier");
     
            jButton3.setText("Quitter");
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
     
            jLabel9.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N
            jLabel9.setText("Formulaire d'inscription");
     
            jTadresse.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTadresseActionPerformed(evt);
                }
            });
     
            jLabel3.setText("Nom:");
     
            jLabel7.setText("Sexe:");
     
            jLabel4.setText("Adresse:");
     
            jLabel6.setText("Lieu de Naissance:");
     
            jLabel8.setText("Profession:");
     
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "M", "F" }));
     
            jTlieu.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTlieuActionPerformed(evt);
                }
            });
     
            jLabel1.setText("IdMembre:");
     
            jLabel5.setText("Date de Naissance:");
     
            jLabel2.setText("Prenom:");
     
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2)
                                .addComponent(jLabel4)
                                .addComponent(jLabel3)
                                .addComponent(jLabel5))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTadresse)
                                    .addComponent(jTnom, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jTprenom)
                                    .addComponent(jTid, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jDateChooser2, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel7)
                                .addComponent(jLabel8)
                                .addComponent(jLabel6))
                            .addGap(120, 120, 120)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTlieu, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jTprofession, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(20, 20, 20)))
                    .addContainerGap())
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jTprenom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTnom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTadresse, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4))
                    .addGap(21, 21, 21)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jDateChooser2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(28, 28, 28)
                            .addComponent(jLabel6))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jTlieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTprofession, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel8))
                    .addContainerGap())
            );
     
            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(45, 45, 45)
                    .addComponent(jButton1)
                    .addGap(70, 70, 70)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(20, 20, 20))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(0, 0, Short.MAX_VALUE)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGap(112, 112, 112))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(9, 9, 9)
                    .addComponent(jLabel9)
                    .addGap(18, 18, 18)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGap(47, 47, 47)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton2)
                        .addComponent(jButton3)
                        .addComponent(jButton1))
                    .addGap(37, 37, 37))
            );
     
            pack();
        }// </editor-fold>                        
     
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            System.exit(0);
        }                                        
     
        private void jTlieuActionPerformed(java.awt.event.ActionEvent evt) {                                       
            // TODO add your handling code here:
        }                                      
     
        private void jTadresseActionPerformed(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 Numero = jTid.getText();
            String prenom = jTprenom.getText();
            String nom = jTnom.getText();
            String adresse = jTadresse.getText();
            SimpleDateFormat sp = new SimpleDateFormat("dd/MM/yyyy");
          String date=sp.format(jDateChooser1.getDate());
            String lieu = jTlieu.getText();
           String sexe = (String)jComboBox1.getSelectedItem();
            String profession = jTprofession.getText();
     
            String requete = "INSERT INTO membres (Prenom, Nom, Adresse,Date, Lieu, Sexe, Profession)VALUES ('"+prenom+"','"+nom+"','"+adresse+"','"+date+"', '"+lieu+"', '"+sexe+"','"+profession+"') ";
     
            try
            {
            ResultSet rs = st.executeQuery(requete);    
            st=conn.createStatement();
            st.executeUpdate(requete);
            JOptionPane.showMessageDialog(null, "requete executée");
     
            } catch (SQLException ex) {
                Logger.getLogger(Formulaire.class.getName()).log(Level.SEVERE, null, ex);
            }
     
        }                                        
     
        /**
         * @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 <a href="http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html" target="_blank">http://download.oracle.com/javase/tu...feel/plaf.html</a> 
             */
            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(Formulaire.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(Formulaire.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(Formulaire.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(Formulaire.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 Formulaire().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
        private javax.swing.JComboBox jComboBox1;
        private com.toedter.calendar.JDateChooser jDateChooser1;
        private com.toedter.calendar.JDateChooser jDateChooser2;
        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.JLabel jLabel9;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextField jTadresse;
        private javax.swing.JTextField jTid;
        private javax.swing.JTextField jTlieu;
        private javax.swing.JTextField jTnom;
        private javax.swing.JTextField jTprenom;
        private javax.swing.JTextField jTprofession;
        // End of variables declaration                   
    }

  4. #4
    Membre chevronné Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Points : 2 120
    Points
    2 120
    Par défaut
    salut,
    il ya 2 corrections a faire, les voici:
    changer ceci,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    String date = sp.format(jDateChooser2.getDate());
    et supprimmer cette ligne:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    			//ResultSet rs = st.executeQuery(requete);
    eric

  5. #5
    Modérateur
    Avatar de MasterMbg
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2011
    Messages
    719
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Congo-Kinshasa

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2011
    Messages : 719
    Points : 1 493
    Points
    1 493
    Par défaut
    Bonsoir,

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
     
     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            String Numero = jTid.getText();
            String prenom = jTprenom.getText();
            String nom = jTnom.getText();
            String adresse = jTadresse.getText();
            SimpleDateFormat sp = new SimpleDateFormat("dd/MM/yyyy");
          String date=sp.format(jDateChooser1.getDate());
            String lieu = jTlieu.getText();
           String sexe = (String)jComboBox1.getSelectedItem();
            String profession = jTprofession.getText();
     
            String requete = "INSERT INTO membres (Prenom, Nom, Adresse,Date, Lieu, Sexe, Profession)VALUES ('"+prenom+"','"+nom+"','"+adresse+"','"+date+"', '"+lieu+"', '"+sexe+"','"+profession+"') ";
     
            try
            {
            ResultSet rs = st.executeQuery(requete);    
            st=conn.createStatement();
            st.executeUpdate(requete);
            JOptionPane.showMessageDialog(null, "requete executée");
     
            } catch (SQLException ex) {
                Logger.getLogger(Formulaire.class.getName()).log(Level.SEVERE, null, ex);
            }
     
        }
    Suivant le code si dessus, les remarques que je peux te faire sont les suivantes :

    1) Enregistrer les dates sous forme des chaines de caractères n'est pas une bonne pratique. Toutes les opérations sur les dates seront d'office indisponibles.
    2) Pour des raisons de simplicité, mais aussi de sécurité il est recommandé d'utiliser la classe PreparedStatement quant à l'exécution des requêtes. Tu peux lire ICI, encore LA ou soit LA.

    Pour enregistrer une date, avec PreparedStatement tu n'as pas besoin de passer par une transformation en chaine de caractères. Tu n'as qu'à utiliser la méthode setDate(int num, long time). Voici comment je transformerais ton code si ça t’intéressait :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
     
            String Numero = jTid.getText();
            String prenom = jTprenom.getText();
            String nom = jTnom.getText();
            String adresse = jTadresse.getText();
            Date date = jDateChooser1.getDate();
            String lieu = jTlieu.getText();
            String sexe = jComboBox1.getSelectedItem().toString();
            String profession = jTprofession.getText();
     
            String requete = "INSERT INTO membres (Prenom, Nom, Adresse,Date, Lieu, Sexe, Profession)VALUES (?,?,?,?,?,?,?)";
     
            try (PreparedStatement ps = ConnectionFresty.getConnection().prepareStatement(requete)){
                ps.setString(1, prenom);
                ps.setString(2, nom);
                ps.setString(3, adresse);
                //Et pour la date...
                ps.setDate(4, new java.sql.Date(date.getTime()));
                ps.setString(5, lieu);
                ps.setString(6, sexe);
                ps.setString(7, profession);
     
                //Exécution de la requête 
                int ligneAjoutee = ps.executeUpdate();
                JOptionPane.showMessageDialog(null, "Enregistrement effectué avec succès");
     
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
    PS : Dans la base de données, garde le type Date pour tous les champs dans lesquels les dates doivent être enregistrées.
    Christian

  6. #6
    Membre du Club
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Juillet 2015
    Messages
    53
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Mali

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : Conseil

    Informations forums :
    Inscription : Juillet 2015
    Messages : 53
    Points : 53
    Points
    53
    Par défaut merci infiniment
    merci infiniment vos liensm'ont beaucoup aider à comprendre l'usage du PreparedStatement une fois encore merci ça marche très bien maintenant au faite j'ai auto incrementé l'Id et l'enlevé sur le code
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    [ATTACH=CONFIG]183118[/ATTACH]



    Citation Envoyé par MasterMbg Voir le message
    Bonsoir,



    Suivant le code si dessus, les remarques que je peux te faire sont les suivantes :

    1) Enregistrer les dates sous forme des chaines de caractères n'est pas une bonne pratique. Toutes les opérations sur les dates seront d'office indisponibles.
    2) Pour des raisons de simplicité, mais aussi de sécurité il est recommandé d'utiliser la classe PreparedStatement quant à l'exécution des requêtes. Tu peux lire ICI, encore LA ou soit LA.

    Pour enregistrer une date, avec PreparedStatement tu n'as pas besoin de passer par une transformation en chaine de caractères. Tu n'as qu'à utiliser la méthode setDate(int num, long time). Voici comment je transformerais ton code si ça t’intéressait :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
     
            String Numero = jTid.getText();
            String prenom = jTprenom.getText();
            String nom = jTnom.getText();
            String adresse = jTadresse.getText();
            Date date = jDateChooser1.getDate();
            String lieu = jTlieu.getText();
            String sexe = jComboBox1.getSelectedItem().toString();
            String profession = jTprofession.getText();
     
            String requete = "INSERT INTO membres (Prenom, Nom, Adresse,Date, Lieu, Sexe, Profession)VALUES (?,?,?,?,?,?,?)";
     
            try (PreparedStatement ps = ConnectionFresty.getConnection().prepareStatement(requete)){
                ps.setString(1, prenom);
                ps.setString(2, nom);
                ps.setString(3, adresse);
                //Et pour la date...
                ps.setDate(4, new java.sql.Date(date.getTime()));
                ps.setString(5, lieu);
                ps.setString(6, sexe);
                ps.setString(7, profession);
     
                //Exécution de la requête 
                int ligneAjoutee = ps.executeUpdate();
                JOptionPane.showMessageDialog(null, "Enregistrement effectué avec succès");
     
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
    PS : Dans la base de données, garde le type Date pour tous les champs dans lesquels les dates doivent être enregistrées.
    Christian

  7. #7
    Membre du Club
    Homme Profil pro
    Chercheur en informatique
    Inscrit en
    Juillet 2015
    Messages
    53
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Mali

    Informations professionnelles :
    Activité : Chercheur en informatique
    Secteur : Conseil

    Informations forums :
    Inscription : Juillet 2015
    Messages : 53
    Points : 53
    Points
    53
    Par défaut merci beaucoup pour vos suggestion
    merci beaucoup de m'avoir montrer le problème maintenant le code marche



    Citation Envoyé par jeffray03 Voir le message
    salut,
    il ya 2 corrections a faire, les voici:
    changer ceci,
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    String date = sp.format(jDateChooser2.getDate());
    et supprimmer cette ligne:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    			//ResultSet rs = st.executeQuery(requete);
    eric

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [syndrom] salut tout le monde
    Par syndrom dans le forum Présentations
    Réponses: 2
    Dernier message: 14/02/2011, 09h56
  2. Réponses: 2
    Dernier message: 07/09/2009, 16h12
  3. salut tout le monde
    Par maniravitch dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 14/04/2008, 18h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo