Bonjour à tous,
Quelqu'un pourrait-il me dire l'origine de l'erreur??
Elle apparaït lorsque j'appuie sur le bouton save.
Merci d'avance
Habiler
Bonjour à tous,
Quelqu'un pourrait-il me dire l'origine de l'erreur??
Elle apparaït lorsque j'appuie sur le bouton save.
Merci d'avance
Habiler
ben déjà si tu nous donnais la stacktrace et le code plutot qu'une bout de capture d'écran?
Bonjour à tous.
Voici le code de mon probleme.
Merci a vous
Habiler
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 /* * 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 decisions; /** * * @author hab */ import java.awt.HeadlessException; import java.sql.*; import javax.swing.*; import static javax.swing.GroupLayout.Alignment.values; import java.io.IOException; // public class JAddDecision1 extends javax.swing.JFrame { // Connection conn = null; ResultSet rs = null; PreparedStatement pst = null; /** * Creates new form JAddDecision */ public JAddDecision1() { initComponents(); } /** * 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") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { TxtMatr = new javax.swing.JTextField(); CmdSave = new javax.swing.JButton(); Matricule = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); CmdSave.setBackground(new java.awt.Color(255, 255, 153)); CmdSave.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N CmdSave.setText("Save"); CmdSave.setBorder(new javax.swing.border.MatteBorder(null)); CmdSave.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { CmdSaveMouseClicked(evt); } }); CmdSave.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { CmdSaveActionPerformed(evt); } }); Matricule.setText("Matricule :"); 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(19, 19, 19) .addComponent(Matricule, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(69, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(CmdSave, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(TxtMatr, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(276, 276, 276)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(31, 31, 31) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(TxtMatr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Matricule, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(38, 38, 38) .addComponent(CmdSave, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(411, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void CmdSaveMouseClicked(java.awt.event.MouseEvent evt) { conn = Connect.ConnectDB(); // System.out.println("--- Connection etablie---"); String sql = "INSERT INTO Decisions Matr VALUES ("+TxtMatr+")"; // +"Datedec," //+"Numero," // +"Debut," // +"Fin," // +"Pourcent," // +"ETP," // +"Abstype," // +"Refus," // +"Postpose," // +"Post_debut," // +"Post_fin," // +"Remarques," // +"Activite," // +"Filiation," // +"Nom_parent)" // + values'" + TxtMatr.getText()+"'; //,'"+ TxtDatedec.getText()+"','"+ TxtNumero.getText()+"','"+ TxtDebut.getText()+"','"+ TxtFin.getText()+"','"+ TxtPourcent.getText()+"','"+ TxtETP.getText()+"','"+ TxtAbstype.getText()+"','"+ TxtRefus.getText()+"','"+ TxtPostpose.getText()+"','"+ TxtPost_debut.getText()+"','"+ TxtPost_fin.getText()+"','"+ TxtRemarks.getText()+"','"+ TxtActivite.getText()+"','"+ TxtFiliation.getText()+"','"+ TxtParent.getText()+"')"; try { pst = conn.prepareStatement(sql); pst.execute(); JOptionPane.showMessageDialog(null, "Saved"); conn.close(); } catch(SQLException | HeadlessException e){ JOptionPane.showMessageDialog(null, e); } } private void CmdSaveActionPerformed(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(JAddDecision1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(JAddDecision1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(JAddDecision1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(JAddDecision1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new JAddDecision1().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton CmdSave; private javax.swing.JLabel Matricule; private javax.swing.JTextField TxtMatr; // End of variables declaration }
Maintenant si tu pouvais nous donner la stacktrace qui va avec. Pour ça tu remplace
par
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 catch(SQLException | HeadlessException e){ JOptionPane.showMessageDialog(null, e); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3 catch(SQLException | HeadlessException e){ e.printStacktrace(); }
Bonsoir la Communauté,
Voici ce à quoi je suis arrivé. Tj un problème.
Quelqu'un pourrrait-il me venir en aide.
Merci
utilise ton preparedStatement correctement:
http://java.developpez.com/faq/jdbc/...paredStatement
C'est un peu n'importe quoi d'utiliser un PreparedStatement pour ensuite constuire quand même la requête en faisant de la concaténation où on ne sait rien lire et on ne voit pas qu'il manque un guillemet.
Ensuite, la prochaine fois, copier coller du code et du message plutot qu'une capture d'écran bien difficile à lire.
Il lui manque effectivement un ' ici
@HABILER au lieu de mettre l'apostrophe manquant que je citai, utilise bien un PreparedStatement comme disait tchize_, ça t'évitera ce genre d'erreur et d'autre soucis.
Code sql : Sélectionner tout - Visualiser dans une fenêtre à part VALUES("+TxtId.getText()+"',
A+.
Bonjour,
Maintenant je bute sur mon prepareStatement. C'est ma première approche de Java. Je dois avouer que je sue.
Merci pour votre aide.
Habiler
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 package decisions; import java.sql.*; import javax.swing.*; public class DecisionsJFrame extends javax.swing.JFrame { Connection conn =null; ResultSet rs = null; PreparedStatement pst = null; /** * Creates new form DeciiJFrame */ public DecisionsJFrame() { initComponents(); } /** * 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") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { TxtId = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); CmdSave = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); TxtDecId = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); TxtMatr = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("Id"); CmdSave.setText("Save record"); CmdSave.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { CmdSaveMouseClicked(evt); } }); jLabel2.setText("DecId"); jLabel3.setText("Matricule"); 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(24, 24, 24) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(35, 35, 35) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(CmdSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(TxtMatr) .addComponent(TxtDecId) .addComponent(TxtId)) .addContainerGap(203, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(39, 39, 39) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(TxtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(TxtDecId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(TxtMatr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)) .addGap(18, 18, 18) .addComponent(CmdSave) .addContainerGap(148, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void CmdSaveMouseClicked(java.awt.event.MouseEvent evt) { //conn = Connect.ConnectDB(); String sql = "Insert INTO Decisions (Id,DecId,Matr)VALUES("+TxtId.getText()+"', '"+TxtDecId.getText()+"', '"+TxtMatr.getText() + ")"; { } try{ //System.out.println(rs.getString()); // conn = Connect.ConnectDB(); pst = conn.prepareStatement(sql); pst.execute(); JOptionPane.showMessageDialog(null, "Saved"); conn.close(); } catch(SQLException e){ JOptionPane.showMessageDialog(null, e); e.printStackTrace(); } } /** * @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(DecisionsJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(DecisionsJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(DecisionsJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(DecisionsJFrame.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 DecisionsJFrame().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton CmdSave; private javax.swing.JTextField TxtDecId; private javax.swing.JTextField TxtId; private javax.swing.JTextField TxtMatr; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; // End of variables declaration }
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 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at decisions.DecisionsJFrame.CmdSaveMouseClicked(DecisionsJFrame.java:102) at decisions.DecisionsJFrame.access$000(DecisionsJFrame.java:7) at decisions.DecisionsJFrame$1.mouseClicked(DecisionsJFrame.java:44) at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270) at java.awt.Component.processMouseEvent(Component.java:6536) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) BUILD SUCCESSFUL (total time: 54 seconds)
salut,
pourquoi as-tu supprimer ceci:
car
Code : Sélectionner tout - Visualiser dans une fenêtre à part // conn = Connect.ConnectDB();
semble etre null
Code : Sélectionner tout - Visualiser dans une fenêtre à part conn
Eric
Bonjour à tous,
Si je remet la connection voici le message d'erreur qui apparaît :
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 package decisions; import java.sql.*; import javax.swing.*; public class DecisionsJFrame extends javax.swing.JFrame { Connection conn =null; ResultSet rs = null; PreparedStatement pst = null; /** * Creates new form DeciiJFrame */ public DecisionsJFrame() { initComponents(); } /** * 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") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { TxtId = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); CmdSave = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); TxtDecId = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); TxtMatr = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("Id"); CmdSave.setText("Save record"); CmdSave.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { CmdSaveMouseClicked(evt); } }); jLabel2.setText("DecId"); jLabel3.setText("Matricule"); 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(24, 24, 24) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(35, 35, 35) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(CmdSave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(TxtMatr) .addComponent(TxtDecId) .addComponent(TxtId)) .addContainerGap(203, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(39, 39, 39) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(TxtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(TxtDecId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(TxtMatr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel3)) .addGap(18, 18, 18) .addComponent(CmdSave) .addContainerGap(148, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void CmdSaveMouseClicked(java.awt.event.MouseEvent evt) { conn = Connect.ConnectDB(); String sql = "Insert INTO Decisions (Id,DecId,Matr)VALUES("+TxtId.getText()+"', '"+TxtDecId.getText()+"', '"+TxtMatr.getText() + "')"; { } try{ //System.out.println(rs.getString()); // conn = Connect.ConnectDB(); pst = conn.prepareStatement(sql); pst.execute(); JOptionPane.showMessageDialog(null, "Saved"); conn.close(); } catch(SQLException e){ JOptionPane.showMessageDialog(null, e); e.printStackTrace(); } } /** * @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(DecisionsJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(DecisionsJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(DecisionsJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(DecisionsJFrame.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 DecisionsJFrame().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton CmdSave; private javax.swing.JTextField TxtDecId; private javax.swing.JTextField TxtId; private javax.swing.JTextField TxtMatr; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; // End of variables declaration }
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 net.ucanaccess.jdbc.UcanaccessSQLException: UCAExc:::3.0.5 unexpected token: , required: ) at net.ucanaccess.jdbc.UcanaccessConnection.prepareStatement(UcanaccessConnection.java:528) at decisions.DecisionsJFrame.CmdSaveMouseClicked(DecisionsJFrame.java:102) at decisions.DecisionsJFrame.access$000(DecisionsJFrame.java:7) at decisions.DecisionsJFrame$1.mouseClicked(DecisionsJFrame.java:44) at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270) at java.awt.Component.processMouseEvent(Component.java:6536) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) Caused by: java.sql.SQLSyntaxErrorException: unexpected token: , required: ) at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source) at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source) at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source) at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source) at net.ucanaccess.jdbc.UcanaccessConnection.prepareStatement(UcanaccessConnection.java:525) ... 35 more Caused by: org.hsqldb.HsqlException: unexpected token: , required: ) at org.hsqldb.error.Error.parseError(Unknown Source) at org.hsqldb.ParserBase.unexpectedTokenRequire(Unknown Source) at org.hsqldb.ParserBase.readThis(Unknown Source) at org.hsqldb.ParserDQL.XreadAllTypesValueExpressionPrimary(Unknown Source) at org.hsqldb.ParserDQL.XreadAllTypesPrimary(Unknown Source) at org.hsqldb.ParserDQL.XreadAllTypesFactor(Unknown Source) at org.hsqldb.ParserDQL.XreadAllTypesTerm(Unknown Source) at org.hsqldb.ParserDQL.XreadAllTypesCommonValueExpression(Unknown Source) at org.hsqldb.ParserDQL.XreadValueExpressionOrNull(Unknown Source) at org.hsqldb.ParserDQL.XreadValueExpressionWithContext(Unknown Source) at org.hsqldb.ParserDQL.readRow(Unknown Source) at org.hsqldb.ParserDQL.XreadContextuallyTypedTable(Unknown Source) at org.hsqldb.ParserDML.compileInsertStatement(Unknown Source) at org.hsqldb.ParserCommand.compilePart(Unknown Source) at org.hsqldb.ParserCommand.compileStatement(Unknown Source) at org.hsqldb.Session.compileStatement(Unknown Source) at org.hsqldb.StatementManager.compile(Unknown Source) at org.hsqldb.Session.execute(Unknown Source) ... 38 more
salut,
peux-tu ajouter ceci et nous donner le resultat?
eric
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 private void CmdSaveMouseClicked(java.awt.event.MouseEvent evt) { conn = Connect.ConnectDB(); String sql = "Insert INTO Decisions (Id,DecId,Matr)VALUES("+TxtId.getText()+"', '"+TxtDecId.getText()+"', '"+TxtMatr.getText() + "')"; { } try{ //System.out.println(rs.getString()); // conn = Connect.ConnectDB(); System.out.println(sql); pst = conn.prepareStatement(sql);
Bonjour,
Comme disait tchize_, utilise un preparedStatement pour affecter les valeurs de la requête. Ton problème est qu'il te manque un apostrophe qui entoure la valeur ici:
Code : Sélectionner tout - Visualiser dans une fenêtre à part String sql = "Insert INTO Decisions (Id,DecId,Matr)VALUES("+TxtId.getText()+"', '"+TxtDecId.getText()+"', '"+TxtMatr.getText()Et puis t'auras encore ce problème si les valeurs contiennent un apostrophe.
Code : Sélectionner tout - Visualiser dans une fenêtre à part String sql = "Insert INTO Decisions (Id,DecId,Matr)VALUES('"+TxtId.getText()+"', '"+TxtDecId.getText()+"', '"+TxtMatr.getText()
EDIT: un petit snippet:
A+.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 String sql = "Insert INTO Decisions (Id,DecId,Matr)VALUES(?,?,?)"; ... PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,TxtId.getText()); // à voir si tu dois utiliser ps.setInt() ps.setString(2,TxtDecId.getText()); ... ps .executeUpdate();
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager