-4

我似乎无法弄清楚如何从通过 java 创建的 sqlite 数据库中删除记录。我试图用主 ID 识别记录,但我只是不断收到一个空指针异常。这是我的代码:

  package userinterface;

    import javax.swing.JFrame;

    import java.awt.FlowLayout;

    import javax.swing.JButton;

    import javax.swing.JDialog;

    import javax.swing.JLabel;

    import javax.swing.JOptionPane;

    import javax.swing.JTextField;

    import backend.SQLiteDb;

    import business.Person;

    import java.awt.event.ActionListener;

    import java.awt.event.ActionEvent;

    import java.awt.BorderLayout;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    public class GUI {

        static SQLiteDb db;

        public static void main(String[] args) {

            final JFrame jframe = new JFrame();

            JButton btnEnterInfo = new JButton("Enter Info");

            jframe.getContentPane().add(btnEnterInfo, BorderLayout.CENTER);

            jframe.setVisible(true);

            jframe.setSize(300, 300);

            btnEnterInfo.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {

                    JLabel fullName = new JLabel("Full Name: ");

                    JTextField nameField = new JTextField();

                    JLabel age = new JLabel("Age: ");

                    JTextField ageField = new JTextField();

                    JLabel address = new JLabel("Address: ");

                    JTextField addressField = new JTextField();

                    JLabel salary = new JLabel("Salary: ");

                    JTextField salaryField = new JTextField();

                    Object[] ob = { fullName, nameField, age, ageField, address,
                            addressField,

                            salary, salaryField };

                    int result = JOptionPane.showConfirmDialog(null, ob, "Part 5",

                    JOptionPane.OK_CANCEL_OPTION);

                    jframe.getContentPane().setLayout(new FlowLayout());

                    if (result == JOptionPane.OK_OPTION) {

                        db = new SQLiteDb();

                        db.createPersonnelTable();
                        String fullName1 = nameField.getText();
                        String userAge = ageField.getText();
                        String userAddress = addressField.getText();
                        String userSalary = salaryField.getText();
                        Person person = new Person(fullName1, Integer
                                .parseInt(userAge), userAddress,

                        Double.parseDouble(salaryField.getText()));

                        db.addPerson(person);

                        String sql = "SELECT * FROM COMPANY";

                        Statement stmt = null;

                        try {
                            java.sql.Statement s;

                            Connection connection = DriverManager
                        .getConnection("JDBC:sqlite:Andy.db");
                            stmt = connection.createStatement();
                            ResultSet rs = stmt.executeQuery(sql);

                            while (rs.next()) {
                                int id_col = rs.getInt("ID");
                                String name = rs.getString("name");
                                int age1 = rs.getInt("Age");
                                String address1 = rs.getString("Address");
                                double salary1 = rs.getInt("Salary");

                                String output = id_col + " " + name + " " + age1
                                        + " " + address1 + " " + salary1;
                                System.out.println(output);

                            }
                            delete(3);
                            rs.close();
                        } catch (SQLException ee) {
                            System.out.println("ee error");
                            ee.printStackTrace();

                        } finally {
                            if (stmt != null) {
                                try {
                                    stmt.close();

                                } catch (SQLException e1) {
                                    // TODO Auto-generated catch block
                                    System.out.println("e1 error");
                                    e1.printStackTrace();

                                }
                            }

                        }

                    }

                }
            });

        }

        public static void viewTable(Connection con, String dbName)
                throws SQLException {

        }

        public static void delete(int id) {
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            try {
                // connection = DriverManager.getConnection();
                preparedStatement = connection.prepareStatement("DELETE FROM person WHERE id = ?");
                preparedStatement.setInt(1, id);
                preparedStatement.executeUpdate();

                System.out.println("DELETE FROM person WHERE id = ?");
            } catch (Exception eeee) {
                eeee.printStackTrace();

            } finally {
                if (preparedStatement != null) {
                    try {
                        preparedStatement.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }

                }
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();

                    }

                }
            }}}
4

1 回答 1

1

connection在您的删除方法中为空,为什么您不会得到 NullPointerException?

 Connection connection = null;
    PreparedStatement preparedStatement = null;
    try {
        // connection = DriverManager.getConnection(); <--- !!!! This line is commented for some reason, so it is not executed!
        preparedStatement = connection.prepareStatement("DELETE FROM person WHERE id = ?");
        preparedStatement.setInt(1, id);
        preparedStatement.executeUpdate();

您删除方法中的其余代码看起来没问题,您只需要将连接变量初始化为某些东西。

一种可能的解决方案是,当您在方法中对其进行初始化时,将此变量存储在方法字段而不是局部变量中actionPerformed

this.connection = DriverManager
                            .getConnection("JDBC:sqlite:Andy.db");

然后你可以使用:

preparedStatement = this.connection.prepareStatement("DELETE FROM person WHERE id = ?");

这将重新使用先前初始化的连接。(重用连接对象是个好主意)

于 2013-07-29T23:13:35.020 回答