user3170343
user3170343

Reputation: 1

insert values of jtextfield into data base ,

I am trying to insert what user will write in jtexfield into my database , but my code dosn't work , even it don't show where's the mistake , but i found that gettext() return nothing , please help me

import java.awt.BorderLayout;
import java.awt.Button;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class frame_ajouter extends JFrame { 

public  JTextField T1;
public  JTextField T2;
public  JTextField T3;




     public  JTextField T1() {   return T1;  }
     public  JTextField T2() {   return T2;  }
     public  JTextField T3() {   return T3;  }

     /**
     *
     */


    public static void afficher() {


 frame_ajouter jFrame = new frame_ajouter();
  Panel p_ajouter = new Panel();
   final JTextField T1 = new JTextField();
   final JTextField T2 = new JTextField();
   final JTextField T3 = new JTextField();


  jFrame.setSize(new Dimension(500, 200));
  jFrame.setTitle("                                                   /**** AJOUTER ****/");
  p_ajouter.setLayout(new GridLayout(4,2));

  p_ajouter.add(new Label("                           NOM D'ETUDIANT    :"));
  p_ajouter.add(T1);
  p_ajouter.add(new Label("                           PRENOM ETUDIANT   :"));
  p_ajouter.add(T2);
  p_ajouter.add(new Label("                           FILIERE ETUDIANT  :"));
  p_ajouter.add(T3);
  jFrame.add(p_ajouter, BorderLayout.NORTH );


  Panel p_under_ajout = new Panel();
  p_under_ajout.setLayout(new FlowLayout());
  Button b1 = new Button("AJOUTER");
  Button b2 = new Button("ANNULER");
  p_under_ajout.add(b1);
  p_under_ajout.add(b2);
  jFrame.add(p_under_ajout, BorderLayout.SOUTH );

  b1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
               req_ajouter cp = new req_ajouter();
               cp.ajt();

            }

        });




  jFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} });

  jFrame.setVisible(true);

    }





    public static void main(String[] args) {



   afficher();





    }

}

_________ second class where i creat connection data base and query

public class req_ajouter extends frame_ajouter{
 public void ajt(){
     try { 


         String nom = T1.getText();

         String prenom = T2.getText();
         String filiere = T3.getText();
         String req = "insert into etud values(0,'"+T1.getText()+"','"+T2.getText()+"','"+T3.getText()+"')";


         connexion op = new connexion();
        op.connect();
       Statement st = op.cn.createStatement();
       st.executeUpdate(req);
             JOptionPane.showMessageDialog(null,"ajouté avec succée , consulter votre tableau d'etudiant");
       op.deconnect();
     } catch (Exception ex) {
   JOptionPane.showMessageDialog(null,ex.getMessage());

     }

   } 
}

Upvotes: 0

Views: 112

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

Sorry to say but you are misusing inheritance to solve a problem that shouldn't be solved with inheritance. Don't have your database class extend the GUI but rather send messages (call methods) from one object to the other.


More:...

  • By having your database class inherit from the GUI, you actually create two GUI objects, one displayed that has the information you want, and the other not displayed (that is part of the inherited class) that is not displayed and has no information.
  • Instead pass a reference of the database class into the GUI and all a method off of it in the GUI button's ActionListener.
  • You should read up on and comply with Java naming standards. Class names should begin with an upper case letter and method and variable names with a lower-case letter.
  • Get a basic Java tutorial or textbook and go through it as the basics are always very good to know and will only help you in your GUI coding.

Upvotes: 1

Manu Zi
Manu Zi

Reputation: 2370

Thanks for the edit!

try something like this:

...
public class frame_ajouter extends JFrame { 

public  JTextField T1;
public  JTextField T2;
public  JTextField T3;

     public  JTextField T1() {   return this.T1;  }
     public  JTextField T2() {   return this.T2;  }
     public  JTextField T3() {   return this.T3;  }

    public static void afficher() {


 frame_ajouter jFrame = new frame_ajouter();
  Panel p_ajouter = new Panel();
   T1 = new JTextField();
   T2 = new JTextField();
   T3 = new JTextField();


  jFrame.setSize(new Dimension(500, 200));
  jFrame.setTitle("                                                   /**** AJOUTER ****/");
  p_ajouter.setLayout(new GridLayout(4,2));

  p_ajouter.add(new Label("                           NOM D'ETUDIANT    :"));
  p_ajouter.add(T1);
  p_ajouter.add(new Label("                           PRENOM ETUDIANT   :"));
  p_ajouter.add(T2);
  p_ajouter.add(new Label("                           FILIERE ETUDIANT  :"));
  p_ajouter.add(T3);
  jFrame.add(p_ajouter, BorderLayout.NORTH );


  Panel p_under_ajout = new Panel();
  p_under_ajout.setLayout(new FlowLayout());
  Button b1 = new Button("AJOUTER");
  Button b2 = new Button("ANNULER");
  p_under_ajout.add(b1);
  p_under_ajout.add(b2);
  jFrame.add(p_under_ajout, BorderLayout.SOUTH );

  b1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
               req_ajouter cp = new req_ajouter();
               cp.ajt(T1.getText(), T2.getText(), T3.getText());

            }

        });

  jFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} });

  jFrame.setVisible(true);

    }

public static void main(String[] args) {
   afficher();
    }
}

And in your second class:

public void ajt(String t1, String, t2, String t3){
     try { 
...
         String nom = t1;
         String prenom = t2;
         String filiere = t3;
...
} 

Upvotes: 0

Related Questions