Salut tout le monde
svp je veux votre aide ,voila j'ai fait tout ce qu'il faut pour réussir la connexion à la base de données .
je veux inserer les données de mon formulaire des JTextFiled dans la base de données postgresql
Une fois que l'utilisateur clique sur le Jbutton enregister j'execute la requette insert into....
voila mon code:
public class Etudiant extends JFrame{
private JLabel LblInfo = new JLabel(" ENTRER VOS INFORMATIONS");
private JButton BtnEnregistrer = new JButton("Enregistrer");
private JLabel LblCode = new JLabel("*Code :");
private JLabel LblNom = new JLabel("*Nom :");
private JLabel LblPrenom = new JLabel("*Prénom :");
private JLabel LblPays = new JLabel("Pays:");
private JTextField TxtCode = new JTextField(10);
private JTextField TxtNom = new JTextField(35);
private JTextField TxtPrenom = new JTextField(35);
private JTextField TxtPays = new JTextField(10);
public Etudiant(){
Container c = getContentPane c.setBackground(Color.red);
c.setLayout(null);
this.setTitle("Identification étudiant");
this.setResizable(true);
this.setSize(500,500);//definir taille de fenetre
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
this.setLocationRelativeTo(null);
this.setContentPane(c);
this.setVisible(true);
LblInfo.setBounds(new Rectangle(30, 20, 300, 10));
LblCode.setBounds(new Rectangle(30,60, 50, 10));
TxtCode.setBounds(new Rectangle(175, 60, 80, 24));
LblNom.setBounds(new Rectangle(30, 90, 60, 10));
TxtNom.setBounds(new Rectangle(175, 90, 125, 24));
LblPrenom.setBounds(new Rectangle(30, 127, 85, 10));
TxtPrenom.setBounds(new Rectangle(175, 127, 120, 24));
LblPays.setBounds(new Rectangle(30, 155, 120, 20));
TxtPays.setBounds(new Rectangle(175, 155, 110, 24));
BtnEnregistrer.setBounds(new Rectangle(200,430, 100, 25));
c.add( LblInfo);
c.add(LblCode);
c.add(TxtCode);
c.add(LblNom);
c.add(TxtNom);
c.add(TxtPrenom);
c.add(LblPrenom);
c.add(TxtPays);
c.add(LblPays);
c.add(BtnEnregistrer); Boutonevent btev = new Boutonevent();
BtnEnregistrer.addActionListener( btev);
}
public class Boutonevent implements ActionListener{
public void actionPerformed(ActionEvent e)
Connection conn=null;
Statement state=null;
try{
Class.forName("org.postgresql.Driver");
System.out.println("DRIVER OK ! ");
String url= "jdbc:postgresql://localhost:5432/postgres";
String user = "postgres";
String passwd = "cccccc";
conn = DriverManager.getConnection(url, user, passwd);
state = conn.createStatement();
System.out.println("Connection effective !");
state.executeUpdate("INSERT INTO etudiantinf(code,nom,prenom,pays)VALUES(('+TxtCode .getText()+','+TxtNom.getText()+','+TxtPrenom .getText()+','+TxtPays.getText()+')");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException m) {
// TODO Auto-generated catch block
m.printStackTrace();
}
finally {
try{
if(state!= null)
state.close();
if( conn!= null)
conn.close();
}
catch(SQLException ex){
ex.printStackTrace();
}
}
}}
public static void main(String args[])// méhode main
{
Etudiant form = new Etudiant();
}// fin main
resultat et l'erreur
DRIVER OK !
Connection effective !
org.postgresql.util.PSQLException: ERREUR: erreur de syntaxe à la fin de l'entrée
Position : 145
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2062)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1795)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:479)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:353)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:299)
at Etudiant$Boutonevent.actionPerformed(Etudiant.java:88)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
aide moiiiiiii merci d'avance
Partager