简易在线选课系统 基于servlet+jsp+mysql

标题 :简易在线选课系统 基于servlet+jsp+mysql

图片: 在这里插入图片描述注册在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
在这里插入图片描述 用户表

字段名类型
usernamevarchar
passwordvarchar
realnamevarchar
rolechar

选课表

字段名类型
usernamevarchar
course_idvarchar

课程信息表

字段名类型
course_idvarchar
usernamevarchar
teachervarchar
pointint
time_1varchar
time_2varchar
locationvarchar
limitedint

代码

package com.sms.utils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBManager {
	
	private static String username;
	private static String password;
	private static String url;
	private static String driver;
	
	static {
		Properties properties = new Properties();
		try {
			properties.load(DBManagerTest.class.getClassLoader().getResourceAsStream("db.properties"));
			username = properties.getProperty("mysql.username");
			password = properties.getProperty("mysql.password");
			url = properties.getProperty("mysql.url");
			driver = properties.getProperty("mysql.driver");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection() {
		
		try {
			Class.forName(driver);
			
		return	DriverManager.getConnection(url,username,password);
				
		}catch (SQLException e) {
				e.printStackTrace();
			}
		 catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	
		}
	
	public static void close(AutoCloseable...autoCloseables) {
		if(autoCloseables!=null) {
		for(AutoCloseable autoCloseable:autoCloseables) {
			if(autoCloseable!=null) {
				try {
					autoCloseable.close();
				} catch (Exception e) {
					e.printStackTrace();
					}
				}
			}
		}
	}
	}


package com.sms.utils;

import java.io.NotSerializableException;
import java.lang.reflect.Field;
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.sms.entity.Course;
import com.sms.entity.User;

public class DBUtils <T> {
	
	// 插入工具
	public static int commInsert(String sql,Object...param) {
		
		// 获取连接
		Connection connection = DBManager.getConnection();
		PreparedStatement pstmt = null;
		// 获取处理对象
		try {
			 pstmt = connection.prepareStatement(sql);
			 // 给占位符赋值
			 for(int i=0;i<param.length;i++) {
				 pstmt.setObject(i+1, param[i]);
			 }
			 
			 // 返回结果集
			 return  pstmt.executeUpdate();
			 
			 
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBManager.close(pstmt,connection);
		}
		return 0;
	}
	
	// 查询工具
	
	public  List<T> commSelect(String sql ,Object... param) throws InstantiationException, IllegalAccessException,NotSerializableException{
		
		List<T> list = new ArrayList<>();
		// 获取连接对象
		Connection connection = DBManager.getConnection();
		PreparedStatement pstmt = null;
		ResultSet reslut = null;
		try {
		
			// 获取处理对象
			 pstmt = connection.prepareStatement(sql);
			 
			// 给占位符赋值
			 if(param!=null) {
				 for(int i=0;i<param.length;i++) {
						pstmt.setObject(i+1,param[i]);
					}
			 }
			 reslut = pstmt.executeQuery();

			
			 while(reslut.next()) {
				
				 Course course = new Course();
				 Class<Course > class1 = (Class<Course>) course.getClass();
				 
				 Field[] Fields = class1.getDeclaredFields();
				 
				 for (Field  field : Fields) {
					 
					 field.setAccessible(true);
					 String name = field.getName();
					 
					 Object value = reslut.getObject(name);
					 field.set(course, value);
				}
				 list.add( (T) course);
			 }
			 return list;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			DBManager.close(reslut,pstmt,connection);
		}
		return null;
	}
	
	// 登入验证工具
	public int login(String sql , Object ...param) {
		
		Connection connection = DBManager.getConnection();
		PreparedStatement pstmt = null;
		ResultSet result = null;
		int flag = -1;
		try {
			 pstmt = connection.prepareStatement(sql);
			 if(param !=null) {
				 for(int i=0;i<param.length;i++) {
					 pstmt.setObject(i+1, param[i]);
				 }
			 }
			 
			 result = pstmt.executeQuery();
			 if(result.next()) {
				flag = result.getInt(1);
			 }
			 if(flag >0) {
				 return 1;
			 }else {
				 return 0;// 账户密码错误
			 }
			
		} catch (SQLException e) {
			e.printStackTrace();
			return -1; // 系统异常
		}finally {
			DBManager.close(result,pstmt,connection);
		}
		
	}
}

package com.sms.dao;

import java.util.List;

import com.sms.entity.Course;
import com.sms.entity.Elective;
import com.sms.entity.User;

public interface ISmsDao {
	
	// 学生添加课程
	public int addCourse(Elective elective) throws Exception;
	
	// 显示学生课程
	public List<Course> selectCourse(User user) throws Exception;
	
	// 教师添加课程
	public int insertCourse(Course course) throws Exception;
	
	// 教师显示所有课程
	public List<Course> selectAll() throws Exception;
	
	// 按照编号进行查询课程
	public List<Course> selectCourseById(Course course) throws Exception;
	
	// 按照编号进行删除课程
	public int deleteCourseById(Course course) throws Exception;
	
	// 按照编号进行删除学生已选的课程
	public int deleteStudentCourseById(Elective elective) throws Exception;
	
	// 按照编号进行修改课程
	public int updataCourseById(Course course) throws Exception;
	
	// 登入验证
	public  int login(User user);
	
	// 注册新用户
	public int addUser(User user);
}

package com.sms.dao.impl;

import java.io.NotSerializableException;
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.sms.dao.ISmsDao;
import com.sms.entity.Course;
import com.sms.entity.Elective;
import com.sms.entity.User;
import com.sms.utils.DBManager;
import com.sms.utils.DBUtils;

public class SmsDaoImpl extends DBUtils implements ISmsDao {
	
	
	
	@Override
	public int addCourse(Elective elective) {
		String sql = "insert into elective values (?,?)";
		return commInsert(sql, elective.getUsername(),elective.getCourse_id());
	}

	@Override
	public List<Course> selectCourse(User user) throws Exception {
		String sql = "select c.* from course c join elective e on c.course_id = e.course_id where username=?";
		return commSelect(sql ,user.getUsername());
	}

	@Override
	public int insertCourse(Course course) {
		String sql = "insert into course values(?,?,?,?,?,?,?,?)";
		return commInsert(sql, course.getCourse_id(),course.getCourse_name(),course.getTeacher(),
		course.getPoint(),course.getTime1(),course.getTime2(),course.getLocation(),course.getLimited());
	}

	@Override
	public List<Course> selectAll() throws NotSerializableException, InstantiationException, IllegalAccessException {
		String sql = "select * from course ";
		return commSelect(sql );
	}
	
	// 登入验证
		public  int login(User user) {
			String sql = "select count(*) from user where username=? and password=? and role=?";
		return login(sql, user.getUsername(),user.getPassword(),user.getRole());
	}

	// 注册功能
	@Override
	public int addUser(User user) {
		String sql = "insert into user values (?,?,?,?);";
		return commInsert(sql, user.getUsername(),user.getPassword(),user.getRealname(),user.getRole());
	}

	@Override
	public List<Course> selectCourseById(Course course) throws Exception{
		String sql = "select * from course where course_id=?";
		return commSelect(sql, course.getCourse_id());
	}

	@Override
	public int deleteCourseById(Course course) throws Exception {
		String sql ="delete from course where course_id=?";
		return commInsert(sql, course.getCourse_id());
	}

	@Override
	public int updataCourseById(Course course) throws Exception {
		String sql = "update course set course_name=?,teacher=?,point=?,time1=?,time2=?,location=?,limited=?" + 
				" where course_id=?";
		return commInsert(sql, course.getCourse_name(),course.getTeacher(),course.getPoint(),course.getTime1(),course.getTime2(),course.getLocation(),course.getLimited(),course.getCourse_id());
	}

	@Override
	public int deleteStudentCourseById(Elective elective) throws Exception {
		String sql = "delete from elective where course_id=?";
		return commInsert(sql, elective.getCourse_id());
	}
}

package com.sms.service;

import java.util.List;

import com.sms.entity.Course;
import com.sms.entity.Elective;
import com.sms.entity.User;

public interface ISmsserivce {

	// 学生添加课程
	public int addCourse(Elective elective) throws Exception;
	
	// 显示学生课程
	public List<Course> selectCourse(User user) throws Exception;
	
	// 教师添加课程
	public int insertCourse(Course course) throws Exception;
	
	// 教师显示所有课程
	public List<Course> selectAll() throws Exception;
	
	// 根据编号查询
	public List<Course> selectCourseById(Course course) throws Exception;
	
	// 按照编号进行删除课程
	public int deleteCourseById(Course course) throws Exception;
	
	// 按照编号进行删除学生已选的课程
	public int deleteStudentCourseById(Elective elective) throws Exception;
	
	// 按照编号进行修改课程
	public int updataCourseById(Course course) throws Exception;
	
	// 登入验证
	public  int login(User user) throws Exception;
	
	// 注册新用户
	public int addUser(User user);
}

package com.sms.service.impl;

import java.util.List;

import com.sms.dao.ISmsDao;
import com.sms.dao.impl.SmsDaoImpl;
import com.sms.entity.Course;
import com.sms.entity.Elective;
import com.sms.entity.User;
import com.sms.service.ISmsserivce;


public class SmsserivceImpl  implements ISmsserivce{
	
	ISmsDao sms = new SmsDaoImpl();
	// 学生添加课程
	public int addCourse(Elective elective) throws Exception {
		return sms.addCourse(elective);
	}
	
	// 显示学生课程
	public List<Course> selectCourse(User user) throws Exception {
		return sms.selectCourse(user);
	}
	
	// 教师添加课程
	public int insertCourse(Course course) throws Exception {
		return sms.insertCourse(course);
	}
	
	// 教师显示所有课程
	public List<Course> selectAll() throws Exception {
		return sms.selectAll();
	}
	
	// 登入验证
	public  int login(User user) {
		return sms.login(user);
		
	}

	// 注册
	@Override
	public int addUser(User user) {
		return sms.addUser(user);
	}

	// 根据编号进行查询
	@Override
	public List<Course> selectCourseById(Course course) throws Exception {
		return sms.selectCourseById(course);
	}

	// 根据编号进行删除
	@Override
	public int deleteCourseById(Course course) throws Exception {
		return sms.deleteCourseById(course);
	}

	// 根据编号进行更新
	@Override
	public int updataCourseById(Course course) throws Exception {
		return sms.updataCourseById(course);
	}

	@Override
	public int deleteStudentCourseById(Elective elective) throws Exception {
		return sms.deleteStudentCourseById(elective);
	}
}

关注本人,获取资源
链接: 链接:https://pan.baidu.com/s/1WrsSe3j34SVymObTyjTL2w
提取码:7dz4

  • 11
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值