public class CategoryDaoImpl implements CategoryDao { @Override public void add(Category category){ try{ QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "insert into category(id,name) values(?,?)"; Object params[] = {category.getId(), category.getName()}; runner.update(sql, params); } catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); } } /* (non-Javadoc) * @see dao.impl.CategoryDao#find(java.lang.String) */ @Override public Category find(String id){ try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "select * from category where id=?"; return (Category)runner.query(sql, id, new BeanHandler(Category.class)); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } /* (non-Javadoc) * @see dao.impl.CategoryDao#getAll() */ @Override public List<Category> getAll(){ try { QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "select * from category"; return (List<Category>)runner.query(sql, new BeanListHandler(Category.class)); } 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 category where id=?",id); } catch (SQLException e) { throw new RuntimeException(e);//抛出运行异常 } return 1;//删除成功返回1表示结束 } @Override public void update(Category record) { try{ QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); String sql = "update category set name=?,where id=? "; Object params[] = { record.getName(), record.getId()}; runner.update(sql, params); } catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); } } }帮我加一下注释
时间: 2024-02-07 20:02:27 浏览: 125
```
public class CategoryDaoImpl implements CategoryDao {
// 添加分类
@Override
public void add(Category category){
try{
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 创建QueryRunner对象
String sql = "insert into category(id,name) values(?,?)"; // SQL插入语句
Object params[] = {category.getId(), category.getName()}; // 插入参数
runner.update(sql, params); // 执行插入操作
} catch(Exception e){
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
// 根据分类ID查找分类信息
@Override
public Category find(String id){
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 创建QueryRunner对象
String sql = "select * from category where id=?"; // SQL查询语句
return (Category)runner.query(sql, id, new BeanHandler(Category.class)); // 执行查询并返回结果
} catch (SQLException e) {
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
// 获取所有分类信息
@Override
public List<Category> getAll(){
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 创建QueryRunner对象
String sql = "select * from category"; // SQL查询语句
return (List<Category>)runner.query(sql, new BeanListHandler(Category.class)); // 执行查询并返回结果
} catch (SQLException e) {
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
// 根据分类ID删除分类信息
@Override
public int delete(String id) {
try {
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 创建QueryRunner对象
runner.update("delete from category where id=?",id); // 执行删除的SQL语句
} catch (SQLException e) {
throw new RuntimeException(e); // 抛出运行时异常
}
return 1; // 删除成功返回1表示结束
}
// 更新分类信息
@Override
public void update(Category record) {
try{
QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource()); // 创建QueryRunner对象
String sql = "update category set name=?,where id=? "; // SQL更新语句
Object params[] = { record.getName(), record.getId()}; // 更新参数
runner.update(sql, params); // 执行更新操作
} catch(Exception e){
e.printStackTrace(); // 打印异常信息
throw new RuntimeException(e); // 抛出运行时异常
}
}
}
```
这段代码实现了对分类信息的增、删、改、查功能。其中,每个方法都调用了QueryRunner对象执行SQL语句,并返回结果或抛出异常。方法名和注释说明了各个方法的具体功能。
阅读全文