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

学生选课系统----CourseDAO

时间:2016-12-10 00:24:57      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:exce   next   value   nec   exception   uri   dblink   port   rom   

package com.csms.dao;

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

import javax.swing.JTextField;

import com.csms.DBLink;
import com.csms.entity.Course;
import com.csms.entity.Teacher;

/**
 * @author 李佩
 * 
 * @version v1
 * 
 * @time 2016/12/6 18:31
 * 
 * @program  课程信息与数据库进行操作的类
 *
 */

public class CourseDAO {
	// 查询课程信息
	public List<Course> searchCourseInformation() {
		Course course = null;
		Connection conn = DBLink.getConn();
		String sql = "SELECT courseID,courseName,courseTeacher,courseType FROM course";
		List<Course> list = new ArrayList<Course>();
		Statement stm = null;
		ResultSet rs = null;

		try {
			stm = conn.createStatement();
			rs = stm.executeQuery(sql);
			while (rs.next()) {
				course = new Course();
				course.setCourID(rs.getString("courseID"));
				course.setCourName(rs.getString("courseName"));
				course.setCourTeacher(rs.getString("courseTeacher"));
				course.setCourType(rs.getString("courseType"));
				list.add(course);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (stm != null)
					stm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

		return list;
	}

	// 插入课程
	public void insertCourseInformation(List<JTextField> list, Teacher teacher) {
		Connection conn = DBLink.getConn();
		String sql = "INSERT INTO course (courseID,courseName,courseCridits,courseTime,courseType,courseTeacher,courseNumber,teacherID) VALUES (?,?,?,?,?,?,?,?)";

		PreparedStatement ptm = null;
		ResultSet rs = null;

		try {
			ptm = conn.prepareStatement(sql);
			ptm.setString(1, list.get(0).getText());
			ptm.setString(2, list.get(1).getText());
			ptm.setInt(3, Integer.parseInt(list.get(2).getText()));
			ptm.setInt(4, Integer.parseInt(list.get(3).getText()));
			ptm.setString(5, list.get(4).getText());
			ptm.setString(6, list.get(5).getText());
			ptm.setInt(7, Integer.parseInt(list.get(6).getText()));
			ptm.setString(8, teacher.getTeacID());
			ptm.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {

		}

	}

	// 查找老师的课程
	public List<Course> searchCourse(String id) {
		List<Course> list = new ArrayList<Course>();
		Course course = null;
		Connection conn = DBLink.getConn();
		String sql = "SELECT courseID,courseName,courseCridits,courseTime,courseNumber FROM course WHERE teacherID=?";

		PreparedStatement ptm = null;
		ResultSet rs = null;

		try {
			ptm = conn.prepareStatement(sql);
			ptm.setString(1, id);
			rs = ptm.executeQuery();

			while (rs.next()) {
				course = new Course();
				course.setCourID(rs.getString("courseID"));
				course.setCourName(rs.getString("courseName"));
				course.setCourCridits(rs.getInt("courseCridits"));
				course.setCourTimes(rs.getInt("courseTime"));
				course.setCourNumber(rs.getInt("courseNumber"));
				list.add(course);
			}
			System.out.println("List size:" + list.size());
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ptm != null)
					ptm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	// 删除课程信息
	public void deleteCourseInformation(String id) {
		Connection conn = DBLink.getConn();
		String sql = "DELETE FROM course WHERE courseID=?";
		System.out.println(id);
		PreparedStatement ptm = null;
		try {
			ptm = conn.prepareStatement(sql);
			ptm.setString(1, id);
			ptm.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ptm != null)
					ptm.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}

  

学生选课系统----CourseDAO

标签:exce   next   value   nec   exception   uri   dblink   port   rom   

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

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