包结构
数据库表结构
entity
1 package com.jdbc.dao; 2 3 import java.sql.SQLException; 4 import java.util.List; 5 6 import org.apache.commons.dbutils.QueryRunner; 7 import org.apache.commons.dbutils.handlers.BeanHandler; 8 import org.apache.commons.dbutils.handlers.BeanListHandler; 9 10 import com.chinasofti.jdbc.TxQueryRunner; 11 import com.jdbc.entity.Course; 12 import com.jdbc.entity.Student; 13 14 public class StudentDao { 15 /* 16 * 获取一个学生的所有选修课 17 */ 18 19 public Student getStuByCourse(int sid) throws SQLException { 20 QueryRunner qr =new TxQueryRunner(); 21 String sql = "select * from student where sid = ?"; 22 Student student = qr.query(sql, new BeanHandler<Student>(Student.class),sid); 23 sql ="select * from course c,score sc where sc.cid =c.cid and sid = ?"; 24 List<Course> courses = qr.query(sql, new BeanListHandler<Course>(Course.class),student.getSid()); 25 student.setCourses(courses); 26 return student; 27 28 } 29 /* 30 * 获取所有学生 31 */ 32 public List<Student> getStusByCourses() throws SQLException{ 33 QueryRunner qr =new TxQueryRunner(); 34 String sql = "select * from student"; 35 List<Student> students = qr.query(sql, new BeanListHandler<Student>(Student.class)); 36 for(Student stu:students) { 37 sql ="select * from course c,score sc where sc.cid =c.cid and sid = ?"; 38 List<Course> courses = qr.query(sql, new BeanListHandler<Course>(Course.class),stu.getSid()); 39 stu.setCourses(courses); 40 } 41 return students; 42 43 } 44 }