SQL30 使用子查询的方式找出属于Action分类的所有电影对应的title,description

本文解析SQL练习题,通过子查询和左连接的方法,展示如何从电影数据库中筛选出Action分类的电影,包括title和description。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
使用子查询的方式找出属于Action分类的所有电影对应的title,description

SQL练习题30(中等):

--子查询
select a.title,a.description
from film a
where a.film_id in 
        (select b.film_id 
             from film_category b 
         where b.category_id in (
                    select c.category_id 
                         from category c 
                     where c.name='Action')
        );
--左连接
select a.title,a.description
from film a
left join film_category b
on a.film_id=b.film_id 
left join category c
on b.category_id=c.category_id
where c.name='Action';
package com.rain.dao; import com.rain.bean.Categoriesbean; import util.DBUtil; import java.sql.*; import java.util.ArrayList; import java.util.List; public class Categorydao { /** * 添加图书分类 * @throws ClassNotFoundException */ public boolean addCategory(Categoriesbean category) throws ClassNotFoundException { String sql = "INSERT INTO categories (NAME, description, parent_id) VALUES (?, ?, ?)"; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; // 声明ResultSet try { conn = DBUtil.getConnection(); pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); // 返回生成的主键 pstmt.setString(1, category.getName()); pstmt.setString(2, category.getDescription()); if (getParentId() != null) { pstmt.setInt(3, category.getParent_id()); } else { pstmt.setNull(3, Types.INTEGER); } int rowsAffected = pstmt.executeUpdate(); // 获取自动生成的主键并设置到对象中 if (rowsAffected > 0) { rs = pstmt.getGeneratedKeys(); // 赋值ResultSet if (rs.next()) { category.setId(rs.getInt(1)); // 设置生成的 id } } return rowsAffected > 0; } catch (SQLException e) { e.printStackTrace(); return false; } finally { DBUtil.CloseDB(rs, pstmt, conn); // 确保关闭所有资源 } } private Object getParentId() { // TODO 自动生成的方法存根 return null; } /** * 获取所有图书分类 * @throws ClassNotFoundException */ public List<Categoriesbean> getAllCategories() throws ClassNotFoundException { List<Categoriesbean> categories = new ArrayList<>(); String sql = "SELECT * FROM categories"; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = DBUtil.getConnection(); pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { Categoriesbean category = new Categoriesbean(); category.setId(rs.getInt("id")); category.setName(rs.getString("NAME")); category.setDescription(rs.getString("description")); category.setParent_id(rs.getObject("parent_id", Integer.class)); categories.add(category); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.CloseDB(rs, pstmt, conn); } return categories; } /** * 根据 ID 获取分类 * @throws ClassNotFoundException */ public Categoriesbean getCategoryById(int id) throws ClassNotFoundException { String sql = "SELECT * FROM categories WHERE id = ?"; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; Categoriesbean category = null; try { conn = DBUtil.getConnection(); pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); rs = pstmt.executeQuery(); if (rs.next()) { category = new Categoriesbean(); category.setId(rs.getInt("id")); category.setName(rs.getString("NAME")); category.setDescription(rs.getString("description")); category.setParent_id(rs.getObject("parent_id", Integer.class)); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.CloseDB(rs, pstmt, conn); } return category; } /** * 更新分类信息 * @throws ClassNotFoundException */ public boolean updateCategory(Categoriesbean category) throws ClassNotFoundException { String sql = "UPDATE categories SET NAME = ?, description = ?, parent_id = ? WHERE id = ?"; Connection conn = null; PreparedStatement pstmt = null; try { conn = DBUtil.getConnection(); pstmt = conn.prepareStatement(sql); pstmt.setString(1, category.getName()); pstmt.setString(2, category.getDescription()); if (getParentId() != null) { pstmt.setInt(3, category.getParent_id()); } else { pstmt.setNull(3, Types.INTEGER); } pstmt.setInt(4, category.getId()); int rowsAffected = pstmt.executeUpdate(); return rowsAffected > 0; } catch (SQLException e) { e.printStackTrace(); return false; } finally { DBUtil.CloseDB(null, pstmt, conn); } } /** * 删除分类 * @throws ClassNotFoundException */ public boolean deleteCategory(int id) throws ClassNotFoundException { String sql = "DELETE FROM categories WHERE id = ?"; Connection conn = null; PreparedStatement pstmt = null; try { conn = DBUtil.getConnection(); pstmt = conn.prepareStatement(sql); pstmt.setInt(1, id); int rowsAffected = pstmt.executeUpdate(); return rowsAffected > 0; } catch (SQLException e) { e.printStackTrace(); return false; } finally { DBUtil.CloseDB(null, pstmt, conn); } } /** * 查询所有顶级分类(parent_id 为 NULL) * @throws ClassNotFoundException */ public List<Categoriesbean> getTopLevelCategories() throws ClassNotFoundException { List<Categoriesbean> categories = new ArrayList<>(); String sql = "SELECT * FROM categories WHERE parent_id IS NULL"; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = DBUtil.getConnection(); pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); while (rs.next()) { Categoriesbean category = new Categoriesbean(); category.setId(rs.getInt("id")); category.setName(rs.getString("NAME")); category.setDescription(rs.getString("description")); category.setParent_id(rs.getObject("parent_id", Integer.class)); categories.add(category); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.CloseDB(rs, pstmt, conn); } return categories; } /** * 根据父分类 ID 查询分类 * @throws ClassNotFoundException */ public List<Categoriesbean> getSubCategoriesByParentId(int parentId) throws ClassNotFoundException { List<Categoriesbean> categories = new ArrayList<>(); String sql = "SELECT * FROM categories WHERE parent_id = ?"; Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = DBUtil.getConnection(); pstmt = conn.prepareStatement(sql); pstmt.setInt(1, parentId); rs = pstmt.executeQuery(); while (rs.next()) { Categoriesbean category = new Categoriesbean(); category.setId(rs.getInt("id")); category.setName(rs.getString("NAME")); category.setDescription(rs.getString("description")); category.setParent_id(rs.getObject("parent_id", Integer.class)); categories.add(category); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.CloseDB(rs, pstmt, conn); } return categories; } } 写出对应的servlet类
最新发布
07-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值