JDBC的CRUD操作之删除操作

1.1.1 删除操作的代码实现

[AppleScript] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

@Test

        /**

         * 删除操作的代码实现

         */

        public void demo3(){

                Connection conn = null;

                Statement stmt = null;

                try{

                        // 注册驱动:

                        Class.forName("com.mysql.jdbc.Driver");

                        // 获得连接:

                        conn = DriverManager.getConnection("jdbc:mysql:///web_test3", "root", "abc");

                        // 创建执行SQL语句对象:

                        stmt = conn.createStatement();

                        // 编写SQL:

                        String sql = "delete from user where id = 5";

                        // 执行SQL:

                        int num = stmt.executeUpdate(sql);

                        if(num > 0){

                                System.out.println("删除用户成功!!!");

                        }

                }catch(Exception e){

                        e.printStackTrace();

                }finally{

                        // 资源释放:

                        if(stmt != null){

                                try {

                                        stmt.close();

                                } catch (SQLException e) {

                                        e.printStackTrace();

                                }

                                 

                                stmt = null;

                        }

                        if(conn != null){

                                try {

                                        conn.close();

                                } catch (SQLException e) {

                                        e.printStackTrace();

                                }

                                conn = null;

                        }

                }

        }

猜你喜欢

转载自blog.csdn.net/qq_40208605/article/details/88308885