学生选课系统----SelectCourseDAO
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学生选课系统----SelectCourseDAO相关的知识,希望对你有一定的参考价值。
/** * @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的主要内容,如果未能解决你的问题,请参考以下文章