我正在尝试将用户在jtexfield
中写入的内容插入到我的数据库中,但我的代码不起作用,
即使它没有显示哪里出错,
但是我发现gettext()
没有回复,请帮助我
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();
}
}
的 的 __ _ __ _ ___ 第二课,我创建连接数据库并查询
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());
}
}
}
答案 0 :(得分:1)
很抱歉,但是您滥用继承来解决不能通过继承解决的问题。不要让数据库类扩展GUI,而是将消息(调用方法)从一个对象发送到另一个对象。
更多:...
答案 1 :(得分:0)
感谢您的编辑!
尝试这样的事情:
...
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();
}
}
在你的第二堂课中:
public void ajt(String t1, String, t2, String t3){
try {
...
String nom = t1;
String prenom = t2;
String filiere = t3;
...
}