Reputation: 1
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
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:...
Upvotes: 1
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