如何从JTextField输入Info到sql数据库?

时间:2014-04-23 04:31:53

标签: sql jtextfield

我建造了一张桌子。我想从活动窗口添加数据。但是a.getTabNum()和其他返回的vatiables的值为 null 这是我的代码:

JButton add = new JButton();
    add.setText("+");
    add.setHorizontalAlignment(SwingConstants.TRAILING);
    panel.add(add);
    //When user clicks he can add new employee 
    add.addActionListener(new ActionListener() {


        public void actionPerformed(ActionEvent event) {
            AddEmp a = new AddEmp();
            a.setSize(400,140);
            a.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            a.setVisible(true);
            String post = a.getPost();
            String nameSurname = a.getNameSurname();
            String tabNum= a.getTabNum();
            Connection connect = null;
            PreparedStatement statement1 = null;
            try {


                connect = DriverManager
                        .getConnection("jdbc:mysql://localhost/tabel?user=root&password=4499669");
                statement1 = connect
                        .prepareStatement("insert into employee(tab_num, name_surname, position, id_dept) values(?, ?, ?, ?)");
                int a1= Integer.parseInt(tabNum);
                statement1.setInt(1, a1);
                statement1.setString(2,nameSurname);
                statement1.setString(3, post);
                statement1.setInt(4, num);
                statement1.executeUpdate();

                // / List of departments

                // ////////////////////////////////////////////////////////////////

            } catch (Exception ex) {
                ex.printStackTrace();

            } finally {
                try {
                    connect.close();

                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        }
    });

这里是ma AddEmp类:`public class AddEmp extends JFrame {     private static final long serialVersionUID = 1L;     JTextField a = new JTextField(30);     JTextField b = new JTextField(27);     JTextField c = new JTextField(23);     AddEmp self = this;     JLabel a1 =新JLabel(“Ф.И.О”);     JLabel b1 =新JLabel(“Должность”);     JLabel c1 =新JLabel(“Табельныйномер”);     JButton o = new JButton(“OK”);

public AddEmp(){

    super("Добавление сотрудника");
    setResizable(false);
    JPanel p = new JPanel();
    p.add(a1);
    p.add(a);
    p.add(b1);
    p.add(b);
    p.add(c1);
    p.add(c);
    o.setHorizontalAlignment(SwingConstants.RIGHT);
    p.add(o);
    getContentPane().add(p, BorderLayout.CENTER);

    o.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent arg0) {
            try{
                JOptionPane.showMessageDialog(null, "Сотрудник добавлен");

                self.setVisible(false);
                self.dispose();
            }catch(Exception e){
                e.printStackTrace();
            }

        }
    });
}

public String getPost() {

    return b.getText();
}

public String getNameSurname() {

    return a.getText();
}

public String getTabNum() {
    return c.getText();
}

}`

0 个答案:

没有答案