PreparedStatement的删除操做

删除操做的代码实现sql

@Test
        /**
         * 删除操做
         */
        public void demo3(){
                Connection conn = null;
                PreparedStatement pstmt  = null;
                try{
                        // 得到链接:
                        conn = JDBCUtils.getConnection();
                        // 编写SQL语句:
                        String sql = "delete from user where id = ?";
                        // 预编译SQL
                        pstmt = conn.prepareStatement(sql);
                        // 设置参数:
                        pstmt.setInt(1, 4);
                        // 执行SQL:
                        int num = pstmt.executeUpdate();
                        if(num > 0){
                                System.out.println("删除成功!");
                        }
                }catch(Exception e){
                        e.printStackTrace();
                }finally{
                        JDBCUtils.release(pstmt, conn);
                }
        }
相关文章
相关标签/搜索