java sql查询

时间:2012-03-05 21:44:56

标签: java sql swing

我有一个查询。我有一个数据库,我正在尝试编写代码,以便可以从java软件创建数据库中的记录。

我有一个连接器类连接到数据库,然后是一个registerStudent类,它允许用户键入2个文本字段的值。那么这些值应该用于在数据库表中创建记录。

当我点击提交按钮时,它会给我这个错误代码:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at RegisterStudent$2.actionPerformed(RegisterStudent.java:99)

仅供参考 - 第99行代码=

con.stmt.executeUpdate("INSERT INTO staff (Name, Profession)"+"VALUES"+"("+"'"+name+"',"+"'"+profession+"')");

这是我的registerStudent类的代码:

import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;


public class RegisterStudent
{

    public RegisterStudent() {
        initialize();
    }

    public JFrame frmRegisterStudent;

    Connector con;
    private JTextField textField_1;
    private JTextField textField_2;

    // initialise the frame
    private void initialize() {
        frmRegisterStudent = new JFrame();
        frmRegisterStudent.setTitle("LEC AdminPro: RegisterStudents");
        frmRegisterStudent.setBounds(100, 100, 413, 225);
        frmRegisterStudent.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        frmRegisterStudent.setLocationRelativeTo(null);


        Border border = LineBorder.createGrayLineBorder();
        frmRegisterStudent.getContentPane().setLayout(null);
        con = new Connector();

        JPanel panel_1 = new JPanel();
        panel_1.setBounds(0, 0, 397, 189);
        frmRegisterStudent.getContentPane().add(panel_1);
        panel_1.setBorder(border);
        panel_1.setLayout(null);

        JLabel lblRegister = new JLabel("Register Student");
        lblRegister.setFont(new Font("Tahoma", Font.BOLD, 12));
        lblRegister.setBounds(10, 11, 124, 20);
        panel_1.add(lblRegister);

        JLabel lblSurname = new JLabel("Name");
        lblSurname.setFont(new Font("Arial", Font.PLAIN, 11));
        lblSurname.setBounds(10, 63, 69, 14);
        panel_1.add(lblSurname);

        JLabel lblDob = new JLabel("Profession");
        lblDob.setFont(new Font("Arial", Font.PLAIN, 11));
        lblDob.setBounds(10, 88, 69, 14);
        panel_1.add(lblDob);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(104, 63, 266, 20);
        panel_1.add(textField_1);

        textField_2 = new JTextField();
        textField_2.setColumns(10);
        textField_2.setBounds(104, 88, 266, 20);
        panel_1.add(textField_2);


        JButton btnNewButton = new JButton("Cancel");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Object[] options = {"Yes", "No"};
                Component form = null;
                int n = JOptionPane.showOptionDialog(form, "Would you like to cancel the new Registration?", "Exit Confirmation", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,  null, options, options);
                if(n == JOptionPane.YES_OPTION) {
                frmRegisterStudent.setVisible(false);
            }

        }
        });
        btnNewButton.setBounds(280, 119, 89, 23);
        panel_1.add(btnNewButton);

        JButton button = new JButton("Submit");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {


            String name = textField_1.getText();    
            String profession = textField_1.getText();


                try {
            con.stmt.executeUpdate("INSERT INTO staff (Name, Profession)"+"VALUES"+"("+"'"+name+"',"+"'"+profession+"')");
            JOptionPane.showMessageDialog(frmRegisterStudent, "New Record has been added");

            } catch (SQLException e) {
            System.out.println("Record couldn't be added!");
            e.printStackTrace();
            }
            }
            });



        button.setBounds(181, 119, 89, 23);
        panel_1.add(button);


    }




    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                    try {
                RegisterStudent window = new RegisterStudent();
                window.frmRegisterStudent.setVisible(true);
                } catch (Exception e) {
                e.printStackTrace();
                }
            }
        });

        public void setVisible(boolean b) {
        frmRegisterStudent.setVisible(true);

}    
}

2 个答案:

答案 0 :(得分:0)

问题出现在Connector类中,其中stmt字段未初始化。

使用此代替NullPointerException

的行
con.stmt = con.conn.prepareStatement("insert into staff (name, profession) values (?, ?)");
con.stmt.setString(1, name);
con.stmt.setString(2, profession);
con.stmt.executeUpdate();

但请注意,这是非常糟糕的设计。

答案 1 :(得分:0)

你的连接器是否为空?我看到stmt是连接器中的一个字段,你初始化了吗? 顺便说一句,使用预准备语句会更好,因为它会逃避字符串(例如,如果它们中有“'”)。

PreparedStatement pstmt = connection.prepareStatement("insert into staff (name, profession) values (?, ?)");
pstmt.setString(1, name);
pstmt.setString(2, profession);
pstmt.executeUpdate();