package com.mobileserver.dao;

import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.mobileserver.domain.Student_QQ287307421;
import com.mobileserver.util.DB;

public class Student_QQ287307421DAO {

	public List<Student_QQ287307421> QueryStudent_QQ287307421(String studentNumber,String studentName,String studentClassNumber,Timestamp studentBirthday) {
		List<Student_QQ287307421> student_QQ287307421List = new ArrayList<Student_QQ287307421>();
		DB db = new DB();
		String sql = "select * from Student_QQ287307421 where 1=1";
		if (!studentNumber.equals(""))
			sql += " and studentNumber like '%" + studentNumber + "%'";
		if (!studentName.equals(""))
			sql += " and studentName like '%" + studentName + "%'";
		if (!studentClassNumber.equals(""))
			sql += " and studentClassNumber = '" + studentClassNumber + "'";
		if(studentBirthday!=null)
			sql += " and studentBirthday='" + studentBirthday + "'";
		try {
			ResultSet rs = db.executeQuery(sql);
			while (rs.next()) {
				Student_QQ287307421 student_QQ287307421 = new Student_QQ287307421();
				student_QQ287307421.setStudentNumber(rs.getString("studentNumber"));
				student_QQ287307421.setStudentName(rs.getString("studentName"));
				student_QQ287307421.setStudentPassword(rs.getString("studentPassword"));
				student_QQ287307421.setStudentSex(rs.getString("studentSex"));
				student_QQ287307421.setStudentClassNumber(rs.getString("studentClassNumber"));
				student_QQ287307421.setStudentBirthday(rs.getTimestamp("studentBirthday"));
				student_QQ287307421.setStudentState(rs.getString("studentState"));
				student_QQ287307421.setStudentPhoto(rs.getString("studentPhoto"));
				student_QQ287307421.setStudentTelephone(rs.getString("studentTelephone"));
				student_QQ287307421.setStudentEmail(rs.getString("studentEmail"));
				student_QQ287307421.setStudentQQ(rs.getString("studentQQ"));
				student_QQ287307421.setStudentAddress(rs.getString("studentAddress"));
				student_QQ287307421.setStudentMemo(rs.getString("studentMemo"));
				student_QQ287307421List.add(student_QQ287307421);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.all_close();
		}
		return student_QQ287307421List;
	}
	/* 传入学生信息对象，进行学生信息的添加业务 */
	public String AddStudent_QQ287307421(Student_QQ287307421 student_QQ287307421) {
		DB db = new DB();
		String result = "";
		try {
			/* 构建sql执行插入新学生信息 */
			String sqlString = "insert into Student_QQ287307421(studentNumber,studentName,studentPassword,studentSex,studentClassNumber,studentBirthday,studentState,studentPhoto,studentTelephone,studentEmail,studentQQ,studentAddress,studentMemo) values (";
			sqlString += "'" + student_QQ287307421.getStudentNumber() + "',";
			sqlString += "'" + student_QQ287307421.getStudentName() + "',";
			sqlString += "'" + student_QQ287307421.getStudentPassword() + "',";
			sqlString += "'" + student_QQ287307421.getStudentSex() + "',";
			sqlString += "'" + student_QQ287307421.getStudentClassNumber() + "',";
			sqlString += "'" + student_QQ287307421.getStudentBirthday() + "',";
			sqlString += "'" + student_QQ287307421.getStudentState() + "',";
			sqlString += "'" + student_QQ287307421.getStudentPhoto() + "',";
			sqlString += "'" + student_QQ287307421.getStudentTelephone() + "',";
			sqlString += "'" + student_QQ287307421.getStudentEmail() + "',";
			sqlString += "'" + student_QQ287307421.getStudentQQ() + "',";
			sqlString += "'" + student_QQ287307421.getStudentAddress() + "',";
			sqlString += "'" + student_QQ287307421.getStudentMemo() + "'";
			sqlString += ")";
			db.executeUpdate(sqlString);
			result = "学生信息添加成功!";
		} catch (Exception e) {
			e.printStackTrace();
			result = "学生信息添加失败";
		} finally {
			db.all_close();
		}
		return result;
	}
	/* 删除学生信息 */
	public String DeleteStudent_QQ287307421(String studentNumber) {
		DB db = new DB();
		String result = "";
		try {
			String sqlString = "delete from Student_QQ287307421 where studentNumber='" + studentNumber + "'";
			db.executeUpdate(sqlString);
			result = "学生信息删除成功!";
		} catch (Exception e) {
			e.printStackTrace();
			result = "学生信息删除失败";
		} finally {
			db.all_close();
		}
		return result;
	}

	/* 根据学号获取到学生信息 */
	public Student_QQ287307421 GetStudent_QQ287307421(String studentNumber) {
		Student_QQ287307421 student_QQ287307421 = null;
		DB db = new DB();
		String sql = "select * from Student_QQ287307421 where studentNumber='" + studentNumber + "'";
		try {
			ResultSet rs = db.executeQuery(sql);
			if (rs.next()) {
				student_QQ287307421 = new Student_QQ287307421();
				student_QQ287307421.setStudentNumber(rs.getString("studentNumber"));
				student_QQ287307421.setStudentName(rs.getString("studentName"));
				student_QQ287307421.setStudentPassword(rs.getString("studentPassword"));
				student_QQ287307421.setStudentSex(rs.getString("studentSex"));
				student_QQ287307421.setStudentClassNumber(rs.getString("studentClassNumber"));
				student_QQ287307421.setStudentBirthday(rs.getTimestamp("studentBirthday"));
				student_QQ287307421.setStudentState(rs.getString("studentState"));
				student_QQ287307421.setStudentPhoto(rs.getString("studentPhoto"));
				student_QQ287307421.setStudentTelephone(rs.getString("studentTelephone"));
				student_QQ287307421.setStudentEmail(rs.getString("studentEmail"));
				student_QQ287307421.setStudentQQ(rs.getString("studentQQ"));
				student_QQ287307421.setStudentAddress(rs.getString("studentAddress"));
				student_QQ287307421.setStudentMemo(rs.getString("studentMemo"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			db.all_close();
		}
		return student_QQ287307421;
	}
	/* 更新学生信息 */
	public String UpdateStudent_QQ287307421(Student_QQ287307421 student_QQ287307421) {
		DB db = new DB();
		String result = "";
		try {
			String sql = "update Student_QQ287307421 set ";
			sql += "studentName='" + student_QQ287307421.getStudentName() + "',";
			sql += "studentPassword='" + student_QQ287307421.getStudentPassword() + "',";
			sql += "studentSex='" + student_QQ287307421.getStudentSex() + "',";
			sql += "studentClassNumber='" + student_QQ287307421.getStudentClassNumber() + "',";
			sql += "studentBirthday='" + student_QQ287307421.getStudentBirthday() + "',";
			sql += "studentState='" + student_QQ287307421.getStudentState() + "',";
			sql += "studentPhoto='" + student_QQ287307421.getStudentPhoto() + "',";
			sql += "studentTelephone='" + student_QQ287307421.getStudentTelephone() + "',";
			sql += "studentEmail='" + student_QQ287307421.getStudentEmail() + "',";
			sql += "studentQQ='" + student_QQ287307421.getStudentQQ() + "',";
			sql += "studentAddress='" + student_QQ287307421.getStudentAddress() + "',";
			sql += "studentMemo='" + student_QQ287307421.getStudentMemo() + "'";
			sql += " where studentNumber='" + student_QQ287307421.getStudentNumber() + "'";
			db.executeUpdate(sql);
			result = "学生信息更新成功!";
		} catch (Exception e) {
			e.printStackTrace();
			result = "学生信息更新失败";
		} finally {
			db.all_close();
		}
		return result;
	}
}
