package com.ischoolbar.programmer.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.ischoolbar.programmer.model.Clazz;
import com.ischoolbar.programmer.model.Course;
import com.ischoolbar.programmer.model.Page;
import com.ischoolbar.programmer.util.StringUtil;
/**
*
* @author llq
*课程数据库操作类
*/
public class CourseDao extends BaseDao {
public boolean addCourse(Course course){
String sql = "insert into s_course values(null,'"+course.getName()+"',"+course.getTeacherId()+",'"+course.getCourseDate()+"',0,"+course.getMaxNum()+",'"+course.getInfo()+"') ";
return update(sql);
}
public List<Course> getCourseList(Course course,Page page){
List<Course> ret = new ArrayList<Course>();
String sql = "select * from s_course ";
if(!StringUtil.isEmpty(course.getName())){
sql += "and name like '%" + course.getName() + "%'";
}
if(course.getTeacherId() != 0){
sql += " and teacher_id = " + course.getTeacherId() + "";
}
sql += " limit " + page.getStart() + "," + page.getPageSize();
ResultSet resultSet = query(sql.replaceFirst("and", "where"));
try {
while(resultSet.next()){
Course cl = new Course();
cl.setId(resultSet.getInt("id"));
cl.setName(resultSet.getString("name"));
cl.setTeacherId(resultSet.getInt("teacher_id"));
cl.setCourseDate(resultSet.getString("course_date"));
cl.setSelectedNum(resultSet.getInt("selected_num"));
cl.setMaxNum(resultSet.getInt("max_num"));
cl.setInfo(resultSet.getString("info"));
ret.add(cl);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ret;
}
public int getCourseListTotal(Course course){
int total = 0;
String sql = "select count(*)as total from s_course ";
if(!StringUtil.isEmpty(course.getName())){
sql += "and name like '%" + course.getName() + "%'";
}
if(course.getTeacherId() != 0){
sql += " and teacher_id = " + course.getTeacherId() + "";
}
ResultSet resultSet = query(sql.replaceFirst("and", "where"));
try {
while(resultSet.next()){
total = resultSet.getInt("total");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return total;
}
public boolean editCourse(Course course) {
// TODO Auto-generated method stub
String sql = "update s_course set name = '"+course.getName()+"',teacher_id = "+course.getTeacherId()+",course_date = '"+course.getCourseDate()+"',max_num = "+course.getMaxNum()+" ,info = '"+course.getInfo()+"' where id = " + course.getId();
return update(sql);
}
public boolean deleteCourse(String ids) {
// TODO Auto-generated method stub
String sql = "delete from s_course where id in("+ids+")";
return update(sql);
}
/**
* 检查该课程是否已选满
* @param courseId
* @return
*/
public boolean isFull(int courseId){
boolean ret = false;
String sql = "select * from s_course where selected_num >= max_num and id = " + courseId;
ResultSet query = query(sql);
try {
if(query.next()){
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ret;
}
/**
* 更新课程已选人数
* @param courseId
*/
public void updateCourseSelectedNum(int courseId ,int num){
String sql = "";
if(num > 0){
sql = "update s_course set selected_num = selected_num + "+ num + " where id = " + courseId;
}else{
sql = "update s_course set selected_num = selected_num - " + Math.abs(num) + " where id = " + courseId;
}
update(sql);
}
/**
* 获取制定id范围内的课程列表
* @param ids
* @return
*/
public List<Course> getCourse(String ids){
List<Course> ret = new ArrayList<Course>();
String sql = "select * from s_course where id in("+ids+")";
ResultSet query = query(sql);
try {
while(query.next()){
Course cl = new Course();
cl.setId(query.getInt("id"));
cl.setName(query.getString("name"));
cl.setTeacherId(query.getInt("teacher_id"));
cl.setCourseDate(query.getString("course_date"));
cl.setSelectedNum(query.getInt("selected_num"));
cl.setMaxNum(query.getInt("max_num"));
cl.setInfo(query.getString("info"));
ret.add(cl);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ret;
}
/**
* 获取指定的课程
* @param id
* @return
*/
public Course getCourse(int id){
Course course = null;
String sql = "select * from s_course where id = " + id;
ResultSet query = query(sql);
try {
while(query.next()){
course = new Course();
course.setId(query.getInt("id"));
course.setName(query.getString("name"));
course.setTeacherId(query.getInt("teacher_id"));
course.setCourseDate(query.getString("course_date"));
course.setSelectedNum(query.getInt("selected_num"));
course.setMaxNum(query.getInt("max_num"));
course.setInfo(query.getString("info"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return course;
}
}
最近下载更多
李清清 LV2
2024年11月6日
lxm2440226414 LV2
2024年6月10日
云着殇 LV9
2024年6月5日
lzx602 LV3
2024年4月15日
微信网友_6906962132258816 LV7
2024年3月16日
zhangjilu LV18
2024年1月1日
磊哥哥哥哥 LV13
2023年12月26日
heweimin LV13
2023年12月12日
hapilong LV6
2023年12月11日
蹇金金 LV7
2023年10月31日
最近浏览更多
Maomaoyun
6月10日
暂无贡献等级
zxy111111
6月8日
暂无贡献等级
微信网友_7520905278033920 LV1
5月22日
xianyu091012 LV5
2024年12月26日
言123456
2024年12月24日
暂无贡献等级
3375276400
2024年12月24日
暂无贡献等级
TTThai LV1
2024年12月17日
微信网友_7298641941385216 LV1
2024年12月16日
Daima000 LV4
2024年12月5日
李清清 LV2
2024年11月6日

