package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import domain.Course;
import domain.Student;
import util.ConnectionUtil;

public class StudentDao {

	public boolean addStudent(Student student){
		String sql = "insert into information values(?,?,?,?,?,?,?,?)";
		Object[] params = {
				student.getId(),
				student.getName(),
				student.getAge(),
				student.getSex(),
				student.getcourse_num(),
				student.getAchievements(),
				student.getClasses(),
				student.getSystem()
		};
		return ConnectionUtil.executeUpdate(sql, params);
	}

	public List<Student> queryAllStudent(){
		String sql = "select * from information";
		Connection conn = ConnectionUtil.getConection();
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			Student u = null;
			List<Student> students = new ArrayList<Student>();
			while(rs.next()){
				int id = rs.getInt("id");
				String name = rs.getString("name");
				int age = rs.getInt("age");
				String sex = rs.getString("sex");
				int course_num = rs.getInt("course_num");
				double achievements = rs.getDouble("achievements");
				String classes = rs.getString("classes");
				String system = rs.getString("system");
				u = new Student(id, name, age, sex, course_num, achievements, classes, system);
				students.add(u);
			}
			return students;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	public boolean deleteStudent(int id){
		String sql = "delete from information where id = ?";
		Object[] params = {
				id
		};
		return ConnectionUtil.executeUpdate(sql, params);
	}
	public Student queryStudent(Integer id){
		String sql = "select * from information where id = ?";
		Connection conn = ConnectionUtil.getConection();
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			Student u = null;
			if(rs.next()){
				id = rs.getInt("id");
				String name = rs.getString("name");
				int age = rs.getInt("age");
				String sex = rs.getString("sex");
				int course_num = rs.getInt("course_num");
				double achievements = rs.getDouble("achievements");
				String classes = rs.getString("classes");
				String system = rs.getString("system");
				u = new Student(id ,name, age, sex, course_num, achievements, classes, system);
			}
			return u;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	public boolean updateStudent(Student student,int num){
		String sql = "update information set id = ?,name = ?,age = ?,sex = ?,course_num= ?,achievements= ?,classes = ?,system = ? where id = ?";
		Object[] params = {		
				student.getId(),
				student.getName(),
				student.getAge(),
				student.getSex(),
				student.getcourse_num(),
				student.getAchievements(),
				student.getClasses(),
				student.getSystem(),
				num
				
		};
		return ConnectionUtil.executeUpdate(sql, params);
	}
	
	//查询学号为num的学生选择的课程
	public List<Course> queryCourse(Integer num){
		String sql = "select * from course where Id = ?";
		Connection conn = ConnectionUtil.getConection();
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, num);
			ResultSet rs = ps.executeQuery();
			Course u = null;
			List<Course> courses = new ArrayList<Course>();
			while(rs.next()){
				int cid = rs.getInt("cid");
				String cname = rs.getString("cname");
				double mark = rs.getDouble("mark");
				double time = rs.getDouble("time");
				int Id = rs.getInt("Id");
				double originalscore = rs.getDouble("originalscore");
				double finallyscore = rs.getDouble("finallyscore");
				u = new Course(cid, cname, mark, time, Id, originalscore, finallyscore);
				courses.add(u);
			}
			return courses;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
}
最近下载更多
呵呵喝  LV4 3月11日
杨豫川  LV12 1月12日
asddwh  LV13 2023年12月29日
微信网友_6680567232876544  LV8 2023年10月8日
hongdongdong  LV14 2023年6月27日
tianyuboy111  LV3 2023年5月20日
liuchang183  LV5 2023年4月22日
蹇金金  LV7 2023年3月14日
北方菜  LV11 2022年12月30日
Korol27  LV3 2022年12月16日
最近浏览更多
xiaoaitx  LV2 前天
angaochong  LV5 10月16日
breeza 10月9日
暂无贡献等级
时光海  LV2 6月30日
破剑茶寮  LV4 6月26日
liyan54188  LV2 6月25日
zzk3085000785 6月20日
暂无贡献等级
TY0165  LV20 6月19日
慵懒的小橘猫  LV11 6月8日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友