JDBC练习-删除

 

 /*
     * 删除
     * 1.sql语句
            delete from tb_brand where id = ?
     * 2.参数:需要id信息
     * 3.结果:boolean
     * */
        @Test
        public void testBrand3() throws Exception {
            //模拟用户输入内容
            int id = 4;
            //1.导入jar包
            //2.定义配置文件
            //3.加载配置文件
            Properties prop = new Properties();
            //获取数据库的位置信息,加载数据库
            prop.load(new FileInputStream("F:\\Develop\\code\\Demo2\\src\\druid.txt"));
            //4.获取连接池对象
            DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
            //5.获取数据库连接Connection
            Connection connection = dataSource.getConnection();
            //6.定义SQL
            String sql = "delete from tb_brand where id = ?";
            //7.获取pstmt对象
            PreparedStatement pstmt = connection.prepareStatement(sql);
            //8.设置参数
            pstmt.setInt(1, id);
            //9.执行sql
            int update = pstmt.executeUpdate();//返回受影响行数
            //10.判断返回值
            if (update > 0) {
                System.out.println("执行成功");
            } else {
                System.out.println("执行失败");
            }
            //11.释放资源
            connection.close();
            pstmt.close();

        }

 

posted @ 2023-06-15 17:24  Karlshell  阅读(25)  评论(0)    收藏  举报