📄 coursedao.java
字号:
package bit.jeffy.service;
import java.util.Iterator;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapperResultReader;
import bit.jeffy.entity.Course;
import bit.jeffy.entity.Student;
import bit.jeffy.springdb.CourseRowMapper;
public class CourseDao{
private JdbcTemplate jdbcTemplate;
private String sql = "select count(*) from Course where COURSE_NO=?";
private String sql_1 = "insert into Course(COURSE_NO,COURSE_NAME,TEACHER_ID) values(?,?,?)";
private String sql_2 = "delete from Course where ID=?";
private String sql_3 = "select * from Course";
private String sql_4 = "select * from Course where course_no=?";
private String sql_5 = "select * from Course where Course.ID not in(select selectcourse.COURSE_ID from selectcourse where STUDENT_ID=?)";
private String sql_6 = "select * from Course where ID=?";
public boolean validate(Course course){
String course_no = course.getCourse_no();
if( course_no.equals("") || course_no==null){
return false;
}
Object[] ob = new Object[]{course_no};
int nCount = 0;
nCount = jdbcTemplate.queryForInt(sql,ob);
if( nCount>0 ){
return true;
}else{
return false;
}
}
//老师开课
public boolean save(Course c){
String c_no = c.getCourse_no();
String c_name = c.getCourse_name();
long tid = c.getTeacher_id();
if( c_no.equals("") || c_no==null || c_name.equals("") || c_name==null ||tid<0){
return false;
}
Object ob[] = new Object[]{c_no,c_name,new Long(tid)};
try{
jdbcTemplate.update(sql_1,ob);
return true;
}catch(Exception e){
return false;
}
}
//老师删除开课
public boolean delete(Course c){
long cid = c.getId();
if(cid < 0){
return false;
}
Object ob[] = new Object[]{new Long(cid)};
try{
jdbcTemplate.update(sql_2,ob);
return true;
}catch(Exception e){
return false;
}
}
//读取课程集
public List getAll(){
return jdbcTemplate.query(sql_3,new RowMapperResultReader(new CourseRowMapper()));
}
//取出学生没有选过的课
public List getAllByTid(Student stu){
long sid = stu.getId();
Object[] ob = new Object[]{new Long(sid)};
return jdbcTemplate.query(sql_5,ob,new RowMapperResultReader(new CourseRowMapper()));
}
//按course_no号读取单门课程,在SelectCourseDao的LogScore中被调用
public Course read(Course c){
String c_no = c.getCourse_no();
List list = null;
Iterator it = null;
Course rt = null;
if(c_no.equals("") || c_no==null){
return null;
}
Object[] ob = new Object[]{c_no};
try{
list = jdbcTemplate.query(sql_4,ob,new RowMapperResultReader(new CourseRowMapper()));
it = list.iterator();
if( it.hasNext()){
rt = (Course)it.next();
}
}catch(Exception e){
return null;
}
return rt;
}
//按ID号取出课程
public Course readById(Course c){
long cid = c.getId();
List list = null;
Iterator it = null;
Course rt = null;
if(cid<0){
return null;
}
Object[] ob = new Object[]{new Long(cid)};
try{
list = jdbcTemplate.query(sql_6,ob,new RowMapperResultReader(new CourseRowMapper()));
it = list.iterator();
if( it.hasNext()){
rt = (Course)it.next();
}
}catch(Exception e){
return null;
}
return rt;
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -