Java:为什么只删除所选行时才会删除所有行?

时间:2016-10-30 07:59:20

标签: java mysql swing

当我从JTable中选择一行并点击按钮BtnDelete1时,我试图从数据库中删除数据。该操作应删除不同表中的行。但是当我单击删除按钮时,所有表中的选定行都将被删除,除了" EMAIL_ADDRESSES"表,其中所有行都被删除。

这是我删除fnumber所在行的存储过程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `delete_contact`(IN fnumber int(2))
BEGIN

DELETE FROM PHONE_NUMBERS
WHERE F_number = fnumber;

DELETE FROM PHYSICAL_ADDRESS
WHERE F_number = fnumber;

DELETE FROM EMAIL_ADDRESSES
WHERE FNumber = fnumber;

DELETE FROM APPOINTMENTS
WHERE Fr_No = fnumber;

DELETE FROM FRIEND
WHERE F_no = fnumber;

END

这是我的按钮BtnDelete1代码:

public class BtnDelete1 extends AbstractAction {


    private FirstSwingApp mainGui;

    public BtnDelete1(FirstSwingApp mainGui) {
        super("Press Me");
        putValue(MNEMONIC_KEY, KeyEvent.VK_P);
        this.mainGui = mainGui;
    }

    public void actionPerformed(ActionEvent e) {

        Object cell = mainGui.getSelectedCell();

        int fnumber = ((int) noText.getText().charAt(0));

        CallableStatement dstmt = null;
        CallableStatement cstmt = null;

        ResultSet rs;


        try {

            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Contact_Manager?user=root");


            String SQL = "{call delete_contact (?)}";

            String disQuery = "\n" +
                    "select FRIEND.F_Name, FRIEND.F_no,FRIEND.Birth_date,FRIEND.Gender,FRIEND.City,FRIEND.State, PHONE_NUMBERS.Extension, PHONE_NUMBERS.Ph_No, PHYSICAL_ADDRESS.Address_Line1, PHYSICAL_ADDRESS.Address_Line2, EMAIL_ADDRESSES.Email_Address, EMAIL_ADDRESSES.Platform, APPOINTMENTS.Notes, APPOINTMENTS.Meeting,APPOINTMENTS.Calls\n" +
                    "from FRIEND,PHONE_NUMBERS, PHYSICAL_ADDRESS, EMAIL_ADDRESSES, APPOINTMENTS\n" +
                    "where FRIEND.F_no = PHONE_NUMBERS.F_number  = PHYSICAL_ADDRESS.F_number = EMAIL_ADDRESSES.FNumber = APPOINTMENTS.Fr_No;\n";

            dstmt = conn.prepareCall(disQuery);
            cstmt = conn.prepareCall(SQL);

            cstmt.setInt(1, fnumber);

            cstmt.executeQuery();

            rs = dstmt.executeQuery();

            ResultSetMetaData metaData = rs.getMetaData();

            // names of columns
            Vector<String> columnNames = new Vector<String>();
            int columnCount = metaData.getColumnCount();
            for (int column = 1; column <= columnCount; column++) {
                columnNames.add(metaData.getColumnName(column));
            }

            // data of the table
            Vector<Vector<Object>> data = new Vector<Vector<Object>>();
            while (rs.next()) {
                Vector<Object> vector = new Vector<Object>();
                for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                    vector.add(rs.getObject(columnIndex));
                }
                data.add(vector);
            }

            // It creates and displays the table


            model.setDataVector(data, columnNames);

            // Closes the Connection

            dstmt.close();
            System.out.println("Success!!");
        } catch (SQLException ex) {

            System.out.println("Error in connection: " + ex.getMessage());
        }


    }

}

这是我的代码,当我点击JTable table中的特定行时,数据会在文本字段中填充:

table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            int row = table.getSelectedRow();
            System.out.println("Selecte table row = " + row);
            if (row != -1) {
                int modelRow = table.convertRowIndexToModel(row);
                System.out.println("Selecte model row = " + row);

                Vector data = (Vector) ((DefaultTableModel) table.getModel()).getDataVector().get(modelRow);

                nameText.setText(data.get(0).toString());
                noText.setText(data.get(1).toString());
                bdateText.setText(data.get(2).toString());
                sexText.setText(data.get(3).toString());
                cityText.setText(data.get(4).toString());
                stateText.setText(data.get(5).toString());
                extText.setText(data.get(6).toString());
                phoneText.setText(data.get(7).toString());
                add1Text.setText(data.get(8).toString());
                add2Text.setText(data.get(9).toString());
                emailText.setText(data.get(10).toString());
                platText.setText(data.get(11).toString());
                notesText.setText(data.get(12).toString());
                meetText.setText(data.get(13).toString());
                callText.setText(data.get(14).toString());

            }
        }
    });

1 个答案:

答案 0 :(得分:5)

DELETE FROM EMAIL_ADDRESSES
WHERE FNumber = fnumber;

对于标识符,SQL不区分大小写。因此FNumber = fnumber 总是为真。因此,这会删除每一行。

最好的办法是使用与表中列名不同的变量名。