码迷,mamicode.com
首页 > 其他好文 > 详细

学生选课系统----SelectCourseDAO

时间:2016-12-10 00:06:51      阅读:369      评论:0      收藏:0      [点我收藏+]

标签:stack   each   pack   orm   let   对象   操作   stat   lock   

/**
 * @author 李佩
 * 
 * @version v1
 * 
 * @time 2016/12/5 22:03
 * 
 * @program  已选课程信息与数据库进行操作的类
 *
 */

package com.csms.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 com.csms.DBLink;
import com.csms.entity.Course;
import com.csms.entity.SelectCourse;
import com.csms.entity.Student;

public class SelectCourseDAO {
	// 向数据库中添加一条学生选课的信息
	public int addStudentSelectCourseInformation(String studentID, String courseID) {
		// 取得数据库表的该学生选课的门数
		int count = 0;

		// 1.创建数据库连接对象
		Connection conn = DBLink.getConn();

		// 2.写出添加数据的sql语句
		String sql = "INSERT INTO selectcourse (courseID,studentID,courseGrade) VALUES (?,?,0)";

		// 设置连接数据库的变量
		PreparedStatement ptm = null;

		try {
			ptm = conn.prepareStatement(sql);
			ptm.setString(1, courseID);
			ptm.setString(2, studentID);
			ptm.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ptm != null)
					ptm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return count;
	}

	// 删除已选课程
	public void removeCourse(String courseID) {
		Connection conn = DBLink.getConn();
		String sql = "DELETE FROM selectcourse WHERE courseID=?";
		PreparedStatement ptm = null;
		try {
			ptm = conn.prepareStatement(sql);
			ptm.setString(1, courseID);
			ptm.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (ptm != null)
					ptm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	// 联合三表查询,学生表,课程表,选课表;查询课程id,课程名字,任课老师,学生id,学生姓名,学生成绩

	public List<Object> searchUnionInformation(String courseID) {
		// TODO Auto-generated method stub
		Connection conn = DBLink.getConn();
		String sql = "SELECT C.courseID,C.courseName,C.courseTeacher,S.stuID,S.stuName,SC.courseGrade FROM course C,student S,selectcourse SC WHERE C.courseID=SC.courseID AND S.stuID=SC.studentID AND SC.courseID=?";
		List<Object> list = new ArrayList<Object>();

		PreparedStatement ptm = null;
		ResultSet rs = null;
		Course course = null;
		Student student = null;
		SelectCourse sc = null;

		try {
			ptm = conn.prepareStatement(sql);
			ptm.setString(1, courseID);
			rs = ptm.executeQuery();
			while (rs.next()) {
				course = new Course();
				student = new Student();
				sc = new SelectCourse();
				course.setCourID(rs.getString("courseID"));
				course.setCourName(rs.getString("courseName"));
				course.setCourTeacher(rs.getString("courseTeacher"));
				student.setStuID(rs.getString("stuID"));
				student.setStuName(rs.getString("stuName"));
				sc.setStudentGrade(rs.getInt("courseGrade"));
				list.add(course);
				list.add(student);
				list.add(sc);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ptm != null)
					ptm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return list;
	}
	
	// 录入学生的成绩
	public void addStudentGrade(String stuID, String courID, float grade){
		Connection conn = DBLink.getConn();
		String sql = "UPDATE selectcourse SET courseGrade=? WHERE studentID=? AND courseID=?";
	
		PreparedStatement ptm = null;
		try{
			ptm = conn.prepareStatement(sql);
			ptm.setFloat(1, grade);
			ptm.setString(2, stuID);
			ptm.setString(3, courID);
			ptm.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			try{
				if(ptm!=null)
					ptm.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
	}
}

  

学生选课系统----SelectCourseDAO

标签:stack   each   pack   orm   let   对象   操作   stat   lock   

原文地址:http://www.cnblogs.com/geore/p/6151915.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!