![](https://csdnimg.cn/release/download_crawler_static/86369810/bg9.jpg)
/**
创建
queryRunner */
QueryRunner queryRunner = new QueryRunner(comboPooledDataSource);
/**
根据课程名搜索
*/
public Course search(String name) throws SQLException {
Course course = null;
String sql = "select * from 课程 where name = ?";
// SQL
查询 返回一个
Bean
对象
course = queryRunner.query(sql, new BeanHandler<Course>(Course.class), name);
return course;
}
/**
根据课程号搜索
*/
public Course search(int id) throws SQLException {
Course course = null;
String sql = "select * from 课程 where id = ?";
// SQL
查询 返回一个
Bean
对象
course = queryRunner.query(sql, new BeanHandler<Course>(Course.class), id);
return course;
}
/**
查询所有的课程记录
*/
public List<Course> searchAll() throws SQLException {
List<Course> courses;
String sql = "select * from 课程";
// SQL
查询 返回
List
容器
courses = queryRunner.query(sql, new BeanListHandler<Course>(Course.class));
return courses;
}
/**
增加一条课程记录
*/
public boolean addCourse(Course course) throws SQLException {
String sql = "insert into 课程 (id, name, studyTime, studyGrade) values (?, ? , ?, ?)";
// SQL
查询 返回受影响的行数
int row =
queryRunner.update(sql, course.getId(), course.getName(), course.getStudyTime(),
course.getStudyGrade());
//
如果受影响的行数为正整数,那么添加记录成功
if (row > 0) {
return true;
} else {
return false;
}
}
/**
根据
id
删除一条课程记录
*/
public boolean deleteCourse(int id) throws SQLException {