概述
标题 :简易在线选课系统 基于servlet+jsp+mysql
图片:
用户表
字段名 | 类型 |
---|---|
username | varchar |
password | varchar |
realname | varchar |
role | char |
选课表
字段名 | 类型 |
---|---|
username | varchar |
course_id | varchar |
课程信息表
字段名 | 类型 |
---|---|
course_id | varchar |
username | varchar |
teacher | varchar |
point | int |
time_1 | varchar |
time_2 | varchar |
location | varchar |
limited | int |
代码
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
最后
以上就是炙热泥猴桃为你收集整理的简易在线选课系统 基于servlet+jsp+mysql的全部内容,希望文章能够帮你解决简易在线选课系统 基于servlet+jsp+mysql所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复