JavaWeb jsp+servlet+jdbc 开发的学生选课管理系统

JavaWeb jsp+servlet+jdbc 开发的学生选课管理系统

  • 基于jsp+servlet+jdbc写的一个简单的学生选课管理系统
    • 学生选课信息的界面
    • 登陆成功界面
    • 主要增删改逻辑代码
      • 数据库设计
      • 总结

基于jsp+servlet+jdbc写的一个简单的学生选课管理系统

这学期刚刚学完javaweb于是期末的时间大作业边做了一个学生选课系统
技术选型:
1.Web容器:Tomca
2.编程语言:java
3.数据库连接池:Druid
4.数据库操作:DBUtils
5.网页设计:jsp
6.静态网页html+css+javascript
7.数据库:MySql8.0.2
8.操作系统:win10

学生选课信息的界面

JavaWeb jsp+servlet+jdbc 开发的学生选课管理系统_第1张图片
这个是登陆界面 处理登录的主要业务代码如下

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.liyuan.pojo.Admin;
import com.liyuan.service.AdminService;
import com.liyuan.service.impl.AdminServiceImpl;
import com.liyuan.utils.CookieUtils;



/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AdminService adminService = new AdminServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//获取用户输入的账号密码
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		Admin admin = adminService.login(username, password);
		if(admin!=null) {
			HttpSession session = request.getSession();
			session.setAttribute("user", admin);
			session.setAttribute("nowTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
			
			//保存登陆账号密码
			CookieUtils.addCookie("cookie_username_password",request,response,7*24*60*60,username,password);
			
			request.getRequestDispatcher("IndexServlet").forward(request, response);
		}else {
		//登陆失败
			request.setAttribute("msg","账号或密码错误!");
			request.setAttribute("username", username);
			request.getRequestDispatcher("login.jsp").forward(request, response);	
		}
	}

}

登陆成功界面

JavaWeb jsp+servlet+jdbc 开发的学生选课管理系统_第2张图片
左侧列表可以对不同的信息进行管理 上图
选课
JavaWeb jsp+servlet+jdbc 开发的学生选课管理系统_第3张图片
课程
JavaWeb jsp+servlet+jdbc 开发的学生选课管理系统_第4张图片

主要增删改逻辑代码

package com.liyuan.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liyuan.pojo.Course;
import com.liyuan.service.CourseService;
import com.liyuan.service.impl.CourseServiceImpl;

/**
 * Servlet implementation class CourseBuildServlet
 */
public class CourseBuildServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       CourseService courService = new CourseServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CourseBuildServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//static/html/admin-list.jsp
		//获取参数
		String action = request.getParameter("action");
		System.out.println("action:"+action);
		if(action.equals("list")) {
			//
			getAll(request, response);
		}	
		//回传参数sava
		else if(action.equals("save")) {
			//获取回传参数
			String coursename = request.getParameter("coursename");
			Integer tid = Integer.parseInt(request.getParameter("tid"));
			//设置添加  修改
			String value = request.getParameter("flagvalue");
			System.out.println("falgvalue:"+value);
			Course course = new Course(coursename,tid);
			//判断是添加还是修改
			if(value.equals("添加")) {
			//判断是否添加过
				if(courService.existsCourse(coursename,tid)) {
					request.setAttribute("msg", "当前课程已经添加了!");
					request.getRequestDispatcher("static/html/courseAdd.jsp").forward(request, response);
				}else {
					
					//判断
					
					request.setAttribute("flag", true);
					int count = courService.addCourse(course);
					System.out.println("count:"+count);
					if(count>0) {
						request.setAttribute("msg", "添加成功!");
						request.getRequestDispatcher("static/html/courseAdd.jsp").forward(request, response);
					}else {
						request.setAttribute("msg", "添加失败!");
						request.getRequestDispatcher("static/html/courseAdd.jsp").forward(request, response);
					}
				}
			}else if(value.equals("修改")) {
				Course newCourse = courService.getOne(coursename);
				newCourse.setTid(tid);
				System.out.println("newCourse:"+newCourse );				
				int count = courService.update(newCourse);
				System.out.println(count);
				if(count>0) {
					request.setAttribute("msg", "修改成功!");
					request.getRequestDispatcher("static/html/courseAdd.jsp").forward(request, response);
				}else {
					request.setAttribute("msg", "修改失败!");
					request.setAttribute("course", course);
					request.getRequestDispatcher("static/html/courseAdd.jsp").forward(request, response);
				}
			}
		}
				//回传参数addCourse
		else if(action.equals("addCourse")) {
			request.setAttribute("flag", true);
			request.getRequestDispatcher("static/html/courseAdd.jsp").forward(request, response);
		}
		//回传参数delete
		else if(action.equals("delete")) {
			Integer courseid = Integer.parseInt(request.getParameter("courseid"));
			courService.deleteById(courseid);
			getAll(request, response);
		}
		//回传参数update
		else if(action.equals("update")) {
			Integer courseid = Integer.parseInt(request.getParameter("courseInfor"));
			
			Course course = courService.getOne(courseid);
			if(course!=null) {
				//将获取到的course对象存储到request域对象中  然后在courseAdd.jsp页面中  用于修改界面的数据回传
				request.setAttribute("course", course);
				request.setAttribute("readonly","readonly");
				request.getRequestDispatcher("static/html/courseAdd.jsp").forward(request, response);
			}
				
		}
			//回传参数searchByTid
		else if(action.equals("searchByTid")) {
			Integer tid = Integer.parseInt(request.getParameter("tid"));
			System.out.println("tid"+tid);
			List list = courService.getAllCourse(tid);
			request.setAttribute("list", list);
			request.getRequestDispatcher("static/html/admin-list.jsp").forward(request, response);
		}
		
	}
	//提取的获取学生列表方法  
	private void getAll(HttpServletRequest request, HttpServletResponse response) {
		List list = courService.getAllCourse();
		request.setAttribute("list", list);
		try {
			request.getRequestDispatcher("static/html/admin-list.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

对应的jsp页面代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
	<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> 


  
  
  	 <%
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName() + ":" +     
        request.getServerPort() + path + "/";
	%>
	
    
    欢迎页面-L-admin1.0
    
    
    
    
    
    
    
    
    
    
    
  
  
  
    
    
共有数据:${fn:length(list)} 条
选课编号 学生学号 学生姓名 课程号 课程名 课程成绩 操作
${item.scid } ${item.stuid } ${item.stuname } ${item.courseid } ${item.coursename } ${item.score }
1 2 3 489

数据库设计

创建数据库 学生表

CREATE DATABASE StudentTeacher;
USE StudentTeacher;

DROP TABLE Student;

CREATE TABLE Student(
	stuid INT PRIMARY KEY AUTO_INCREMENT,
	stuname VARCHAR(20) NOT NULL,
	stugender VARCHAR(2) NOT NULL DEFAULT('男'),
	stunum VARCHAR(20) UNIQUE,
	stupassword VARCHAR(30) NOT NULL
);
DESC Student;

创建教师表

CREATE TABLE Teacher(
	tid INT PRIMARY KEY AUTO_INCREMENT,
	tname VARCHAR(20) NOT NULL,
	tnum VARCHAR(20) UNIQUE,
	tpassword VARCHAR(30) NOT NULL
);
ALTER TABLE teacher ADD tgender VARCHAR(2) DEFAULT('男');

创建管理员表

CREATE TABLE Admin(
	id INT PRIMARY KEY AUTO_INCREMENT,
	`name` VARCHAR(20) NOT NULL,
	username VARCHAR(20) UNIQUE,
	`password` VARCHAR(30) NOT NULL
);

创建course表

CREATE TABLE Course(
	courseid INT PRIMARY KEY AUTO_INCREMENT,
	coursename VARCHAR(20),
	tid INT,
	enabled  ENUM('0','1') DEFAULT '1',
	FOREIGN KEY (tid) REFERENCES Teacher(tid),
	CHECK (enabled=1 OR enabled=2)
);
DESC Course;

创建选课信息表

CREATE TABLE SC_Information(
	scid INT,
	stuid INT,
	stuname VARCHAR(20),
	courseid INT,
	coursename VARCHAR(20),
	score int,
	
	FOREIGN KEY (stuid) REFERENCES student(stuid),
	FOREIGN KEY (courseid) REFERENCES course(courseid ),
);

总结

1.emmmmm 目前我才大二会的也不是很多 不足之处请大家多多指正欢迎大家评论。。。完整的代码我放到百度云盘了 链接:学生选课管理系统
提取码:lynb
四个模块的代码基本逻辑都是一样的,只要会一个剩下的基本都会写了 所以我只写了选课表的增删改差 剩下的课程教师学生只写了个查询列表的功能 。。。。好了这个学期学的还是可以的,希望本文能对大家有点帮助。。。。加油!

你可能感兴趣的:(笔记,jsp,java,servlet,mysql,web)