public class CopyOfStudentDaoImpl implements StudentDao {
private JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
public void setDataSource(DataSource dataSource) {
System.out.println(dataSource);
jdbcTemplate = new JdbcTemplate(dataSource);
namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
System.out.println(namedParameterJdbcTemplate);
}
public Cource StudentbyId(String id) {
String sql = "select Tstudent.studentid,Tstudent.username,Tstudent.password,Tstudent.address,course.studentid,course.courseid,course.cName from course join Tstudent on course.studentid=Tstudent.studentid and course.studentid = ? ";
Cource cource=jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Cource>(Cource.class), id);
return cource;
}
public int addStudent(Student student) {
String sql = "insert into Tstudent (username , password , address ,time) values ( ? , ? , ? , ?)";
return jdbcTemplate.update(sql, new BeanPropertyRowMapper<Student>(Student.class));
}
public int deleteStudent(String id) {
String sql = "delete from Tstudent where studentid = ? ";
return jdbcTemplate.update(sql, id);
}
public List<Student> queryStudent() {
String sql = "select * from Tstudent";
List<Student> list=jdbcTemplate.query(sql, new BeanPropertyRowMapper<Student>(Student.class));
return list;
}
public int updateStudent(Student student) {
String sql = "update Tstudent set username = ? , password = ? , address = ? where studentid = ? ";
return jdbcTemplate.update(sql, new BeanPropertyRowMapper<Student>(Student.class));
}
public int count() {
String sql = "select count(*) from Tstudent";
return jdbcTemplate.queryForInt(sql);
}
public class StudentDaoImpl implements StudentDao {
private JdbcTemplate jdbcTemplate;
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
public Cource StudentbyId(String id) {
String sql = "select c.studentid ,c.cName ,c.courseid ,t.username,t.password from course as c join "
+ " Tstudent as t on c.studentid = t.studentid and c.studentid= ? ";
return jdbcTemplate.queryForObject(sql,
new BeanPropertyRowMapper<Cource>(Cource.class), id);
}
public int addStudent( final Student student) {
int result = 0;
final String sql = "insert into Tstudent (username , password , address , time) values ( ? , ? , ? , ?)";
KeyHolder holder = new GeneratedKeyHolder();
result = namedParameterJdbcTemplate.update(sql,
new BeanPropertySqlParameterSource(student), holder);
System.out.println(holder.getKey().intValue());
return result;
}
public int deleteStudent(String id) {
int result = 0;
String sql = "delete from Tstudent where studentid =:id ";
Map<String, String> map = new HashMap<String, String>();
map.put("id", id);
result = namedParameterJdbcTemplate.update(sql, map);
return result;
}
public List<Student> queryStudent() {
String sql = "select * from Tstudent";
RowMapper<Student> mapper = new BeanPropertyRowMapper<Student>(
Student.class);
List<Student> list = jdbcTemplate.query(sql, mapper);
return list;
}
public int updateStudent(Student student) {
int result = 0;
String sql = "update Tstudent set username =:username , password =:password , address =:address where studentid =:studentid";
result = namedParameterJdbcTemplate.update(sql,new BeanPropertySqlParameterSource(student));
return result;
}
public int count() {
String sql = "select count(*) from Tstudent";
return namedParameterJdbcTemplate.getJdbcOperations().queryForInt(sql);
}
}