current position:Home>JDBC connects to the database for data deletion. The syntax is correct, but the data in the database cannot be deleted

JDBC connects to the database for data deletion. The syntax is correct, but the data in the database cannot be deleted

2022-02-03 00:57:21 CSDN Q & A

The phenomenon and background of the problem
public void del_stu() {        JFrame jf = new JFrame(" Dormitory information deletion ------>");        JLabel jl1 = new JLabel(" The student's name :");        JTextField jt1 = new JTextField("",10);        JLabel jl2 = new JLabel(" Student student id :");        JTextField jt2 = new JTextField("",15);        JButton jb1 = new JButton(" Delete ");        jf.setLayout(new FlowLayout(FlowLayout.CENTER));        String stuname = jt1.getText();        String stuid = jt2.getText();        jb1.addActionListener(new ActionListener() {                        @Override            public void actionPerformed(ActionEvent e) {                try {                    Class.forName("com.mysql.cj.jdbc.Driver");// load mysql drive                     String URL = "jdbc:mysql://localhost:3306/dor_information?&useSSL=false&serverTimezone=UTC&serverTimezone=GMT%2B8";                    Connection connection = Connect.getConnection();                    Statement statement = connection.createStatement();                    String sql = "delete from dor_stu where  Student number  =? ";                    PreparedStatement pstmt = connection.prepareStatement(sql);                    pstmt.setString(1, stuid);                    pstmt.executeUpdate();                    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();                }            }        });
Operation results and error reporting contents

What is reported here is that the deletion is successful

My solution ideas and tried methods
What I want to achieve

The database can delete the data

1.




Refer to the answer 1:



Refer to the answer 2:



Refer to the answer 3:

Execute deleted sql After need commit Commit transaction




Refer to the answer 4:

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

Random recommended