public List<Movie> getPageData(int startindex, int pagesize, String category_id){ try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "select * from movie where category_id=? limit ?,?"; Object params[] = {category_id, startindex, pagesize}; return (List<Movie>)runner.query(sql, params, new BeanListHandler(Movie.class)); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } public int getTotalRecord(String category_id){ try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "select count(*) from movie where category_id=?"; long totalrecord = (Long)runner.query(sql, category_id, new ScalarHandler()); return (int)totalrecord; } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } //(id,name,author,price,image,description,category_id) @Override public void update(Movie movie) { try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "update movie set name=?,actor=?,price=?,poster=?,intro=?,category_id=? where id=?"; Object params[] = { movie.getName(), movie.getActor(), movie.getPrice(), movie.getPoster(), movie.getIntro(), movie.getCategory_id(),movie.getId()}; runner.update(sql, params); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } @Override public int delete(String id) { try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); //执行删除的sql runner.update("delete from movie where id=?",id); } catch (SQLException e) { throw new RuntimeException(e);//抛出运行异常 } return 1;//删除成功返回1表示结束 }帮我加一下注释
时间: 2024-01-25 14:02:27 浏览: 29
好的,以下是代码的注释:
```
// 获取指定类别下的电影列表(分页)
public List<Movie> getPageData(int startindex, int pagesize, String category_id){
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 获取数据库连接
String sql = "select * from movie where category_id=? limit ?,?"; // 查询语句
Object params[] = {category_id, startindex, pagesize}; // 查询参数
return (List<Movie>)runner.query(sql, params, new BeanListHandler(Movie.class)); // 执行查询并返回结果
} catch (Exception e) {
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
// 获取指定类别下的电影总数
public int getTotalRecord(String category_id){
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 获取数据库连接
String sql = "select count(*) from movie where category_id=?"; // 查询语句
long totalrecord = (Long)runner.query(sql, category_id, new ScalarHandler()); // 执行查询并返回结果
return (int)totalrecord; // 将结果转换为整型并返回
} catch (Exception e) {
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
// 更新电影信息
@Override
public void update(Movie movie) {
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 获取数据库连接
String sql = "update movie set name=?,actor=?,price=?,poster=?,intro=?,category_id=? where id=?"; // 更新语句
Object params[] = { movie.getName(), movie.getActor(), movie.getPrice(), movie.getPoster(), movie.getIntro(), movie.getCategory_id(),movie.getId()}; // 更新参数
runner.update(sql, params); // 执行更新操作
} catch (SQLException e) {
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
// 删除电影信息
@Override
public int delete(String id) {
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 获取数据库连接
//执行删除的sql语句
runner.update("delete from movie where id=?",id); // 删除操作
} catch (SQLException e) {
throw new RuntimeException(e); // 抛出运行时异常
}
return 1; // 删除成功返回1表示结束
}
```