current position:Home>MySQL connector 5.1.25 and JDBC, the deletion syntax is correct, but there is no prompt that the deletion is successful, and the program does not report an error.

MySQL connector 5.1.25 and JDBC, the deletion syntax is correct, but there is no prompt that the deletion is successful, and the program does not report an error.

2022-02-03 02:09:27 CSDN Q & A

The phenomenon and background of the problem

The code can be in mysql connencter8.0 On the perfect run , But in 5.1.25 On , The delete function cannot be used

    public void del_stu() {        JFrame jf = new JFrame(" Dormitory information deletion ------>");        JLabel jl2 = new JLabel(" Student student id :");        JTextField jt2 = new JTextField("",15);        JButton jb1 = new JButton(" Delete ");        jf.setLayout(new FlowLayout(FlowLayout.CENTER));        jb1.addActionListener(new ActionListener() {                        @Override            public void actionPerformed(ActionEvent e) {                try {                    String stuid = jt2.getText();                    Class.forName("com.mysql.jdbc.Driver");// load mysql drive                     String URL = "jdbc:mysql://localhost:3306/dor_information";                    Connection connection = Connect.getConnection();                    Statement statement = connection.createStatement();                    String sql = "delete from dor_stu where ' Student number ' = ?";                    PreparedStatement ptmt = connection.prepareStatement(sql);                    ptmt.setString(1, stuid);                    ptmt.executeUpdate();                    ptmt.execute();                    ResultSet rs = statement.executeQuery("select *from dor_stu");                        while(rs.next()) {                        if(!rs.getString(2).equals(stuid)) {                            System.out.println(" Delete successful ");                        }                    }                }catch(Exception e1) {                    e1.printStackTrace();                }finally {                    try {                        Connect.close();                    } catch (Exception e2) {                        // TODO  Automatically generated  catch  block                         e2.printStackTrace();                    }                            }                            }        });                jf.setBounds(400, 400, 400, 300);        jf.setVisible(true);        jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);        jf.add(jl2);        jf.add(jt2);        jf.add(jb1);            }
Operation results and error reporting contents

If it is String sql = "delete from dor_stu where ' Student number ' = ?"; Words , Will run , But there is no prompt that the deletion is successful
If it is String sql = "delete from dor_stu where Student number = ?"; Words , Will be in sql There's a mistake , The content of the error report is mainly the student number = ? here , It will report an error '??=?'

My solution ideas and tried methods
What I want to achieve

The database can be deleted




Refer to the answer 1:

Didn't meet the conditions , So the result is 0
Your deletion judge why it is written like this ?
Whether the deletion is normal or not will directly return to the status 0 or 1 Of , There's no need to check and compare .

    public void del_stu() {        JFrame jf = new JFrame(" Dormitory information deletion ------>");        JLabel jl2 = new JLabel(" Student student id :");        JTextField jt2 = new JTextField("",15);        JButton jb1 = new JButton(" Delete ");        jf.setLayout(new FlowLayout(FlowLayout.CENTER));        jb1.addActionListener(new ActionListener() {            @Override            public void actionPerformed(ActionEvent e) {                try {                    String stuid = jt2.getText();                    Class.forName("com.mysql.jdbc.Driver");// load mysql drive                     String URL = "jdbc:mysql://localhost:3306/dor_information";                    Connection connection = Connect.getConnection();                    Statement statement = connection.createStatement();                    String sql = "delete from dor_stu where ' Student number ' = ?";                    PreparedStatement ptmt = connection.prepareStatement(sql);                    ptmt.setString(1, stuid);                    int result=ptmt.executeUpdate();                   if(result==1){                      System.out.println(" Delete successful ");                    }                }catch(Exception e1) {                    e1.printStackTrace();                }finally {                    try {                        Connect.close();                    } catch (Exception e2) {                        // TODO  Automatically generated  catch  block                         e2.printStackTrace();                    }            }            }        });        jf.setBounds(400, 400, 400, 300);        jf.setVisible(true);        jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);        jf.add(jl2);        jf.add(jt2);        jf.add(jb1);    }



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030209248507.html

Random recommended