【javaweb期末项目】从零开始制作网上学生成绩管理系统(报告格式+附demo)_任务赚钱网

admin 2019-09-04 11:25 阅读:0

门生网上怎么样获利目录: 一、 前提与摘要 二、 课程计划的目标以及请求 三、 系统分析与计划 四、 实现与测试 五、 重要源代码 六、 碰到的题目以及解决方法 七、 存在的缺少和改造的思路

『18.12.28更新』 文末更新下载方法,供参考进修交换 『18.12.19更新』 优化界面; 优化均匀结果和总结果获患上方法; 优化主页跳转方式; 新增回到顶部小火箭; 登录.png 操持员登录.png 操持员界面.png 局部成绩 门生登录.png 学生登录成功.png 学生信息录入界面.png 查找列表界面.png

—————————————————————————————————————————————— 一、前提与摘要

学生成绩管理系统是一个教诲单位不可缺少的部分,它的内容对于付学校的教师、学生和管理者来说都相当紧张。本论文叙述到的学生成绩管理系统是用JSP网页编程+SQL查问语言实现的。重点介绍了学生成绩管理系统的实现进程:包罗系统分析与设计、数据库设计、系统成果设计、系统实现、系统测试等。本系统重要成果有教师信息管理、学生信息管理、成绩管理,包罗功能需要设计、数据库设计等内容。 如今的社会一日千里,发展的十分快。学校的发展也在加快,教师和学生做为学校的最主要的管理东西,教师和学生的信息管理系统就成为了学校信息化管理系统中不可缺少的部分,它的内容对于付学校的构造管理相当紧张。但不停以来人们利用传统家养的方式进行学生的档案管理,这种管理方式存在着很多缺点,如:服从低,轻易堕落,格局不范例。此外工夫一长,不轻易进行统计和分析。 随沉迷信技艺的不断提高,盘算机迷信日渐成熟,它已经进入人类社会的各个范畴并发挥着越来越重要的感化。作为盘算机使用的一部分,利用计算机对学生档案进行管理,具备手工管理所无法比较的长处。比方:检索敏捷、查找便利、牢靠性高、存储量年夜、寿命长、本钱高等。这些长处能够极年夜地提高学校学生档案管理的服从。因此,开辟多么一套管理软件成为颇有须要的事变。而且只要软件的设计公道,可觉患上学校供给公道的管理形式。 摘要:学生成绩管理系统、JSP网页编程、SQL 二、课程设计的目标和请求

2.1 目的:

该系统的具体任务便是设计一个学生成绩的数据库管理系统,由计算机来代替身工执 行一系列诸如对教师、学生信息和学生成绩的增加、删除了、查问和改正的处理惩罚操纵,以 便利对以上信息的管理构造事变。 创立学生成绩管理系统,采取计算机对学生成绩进行管理,进一步提高办学效益和现 代化程度。帮忙严惩教师提高事情效率,实现学生成绩信息管理工作流程的系统化、范例 化和主动化

2.2 要求:

网上学生成绩管理系统 实现学生信息的输入 实现课程的输入 实现成绩的输入 实现成绩的查询(按照姓名、学号等信息查询) 实现成绩的统计(均匀分的统计、总成绩的统计) 三、 系统分析与设计 3.1 数据库设计

学生表的设计:(学号,姓名,密码,性别,家庭住址,成绩,备注) 学生数据

管理员表的设计:(id,名字,密码) 管理员数据 3.2 营业流程设计 营业流程设计.jpg 3.3 用例图设计 用例.jpg 3.4 类与页面逻辑对应关连图设计 mulu 3.5 名目截图 名目截图.png 四、 实现与测试 4.1 实现步调: 4.1.1 配置数据库

1️⃣首先在mysql内创立毗邻并记取端口号和数据库用户名和账号 屏幕快照 2018-12-15 下午8.59.12.png

2️⃣创建数据库test(注:本项目只用到了stu和mgr两个表,user是测试用) 数据库建立.png

3️⃣输入如下代码创建学生表stu:(同理创建管理员表mgr,其中insert values的值本身定义)

use test;
CREATE TABLE `stu` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `pwd` varchar(255) NOT NULL,
  `sex` varchar(255) NOT NULL,
  `home` varchar(255) NOT NULL,
  `grades` varchar(255),
  `info` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
INSERT INTO `stu` VALUES ('1', 'stu1', '123', 'men', 'here', '0','i am the data-manerger');
INSERT INTO `stu` VALUES ('2', 'stu2','123','women','there','100','she is a good girl');






管理员数据库.png






学生数据库.png

4.1.2 在webroot下的WEB-INF下的lib中导入mysql的驱动jar包 jar包.png 导入jar.png 4.1.2 类的编写

创建一下五个类包: 类包 五个类包感化: ①dao:数据库操纵的东西,用于使相干数据库操作更简单(无关Dao形式的文章dragon_dai_2017/article/details/76937553)

②entity:相称于Javabean,用于抽象数据布局

③filter:过滤器,用于过滤字符(固然也能够本身编写过滤IP等操作,详见:p/1cf4ab2f7e21)

④servlet:业务逻辑实体类,用于实行对应JSP页面的业务逻辑,完成MVC中C的相干操作

⑤util:用于毗邻数据库(关连系接数据库的操作详见yanglong_blog_/article/details/73733176)

在五个类包中编写相关代码: 1️⃣dao包中: 创建一个接口类 StuDao.java,用于规范化数据库相关操作

//StuDao.java
package com.dao;

import java.util.List;
import com.entity.Stu;


public interface StuDao {
    
    
    //1.注册
    public boolean register(Stu stu);
    
    //2.登录
    public boolean stulogin(String name,String pwd);
    public boolean mgrlogin(String name,String pwd);
        
    //3.前往学生信息聚集
    public List getStuAll();
    
    //4.按照用户名前往某个用户信息聚集
    public List getStuByName(String namestr);
    
    //5.按照学号返回某个用户信息集合
    public List getStuById(String IdStr);
    
    //6.根据id删除了用户
    public boolean delete(int id) ;
    
    //7.更新用户信息
    public boolean update(int id,String name, String pwd,String sex, String home, String grades,String info);
    
    //8.求成绩总和
    public int sum();
    
    //9.求成绩平均
    public int avg();
}

再创建StuDao的实现类StuDaoImpl.java(主动疏忽我的相关测试用的输出语句哈哈)

package com.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
import com.entity.Stu;
import com.util.DBconn;
 
public class StuDaoImpl implements StuDao{
    //1.注册
        public boolean register(Stu stu) {
            boolean flag = false;
            DBconn.init();
            int i =DBconn.addUpdDel("insert into stu(name,pwd,sex,home,grades,info) " +
                    "values('"+stu.getName()+"','"+stu.getPwd()+"','"+stu.getSex()+"','"+stu.getHome()+"','"+stu.getGrades()+"','"+stu.getInfo()+"')");
            if(i>0){
                flag = true;
            }
            DBconn.closeConn();
            return flag;
        }
        //2.登录(学生与管理员的差别是查找的表纷比方样)
        //(学生)
        public boolean stulogin(String name, String pwd) {
            boolean flag = false;
            try {
                    DBconn.init();
                    ResultSet rs = DBconn.selectSql("select * from stu where name='"+name+"' and pwd='"+pwd+"'");
                    while(rs.next()){
                        if(rs.getString("name").equals(name) && rs.getString("pwd").equals(pwd)){
                            flag = true;
                        }
                    }
                    DBconn.closeConn();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return flag;
        }
      //(管理员)
        public boolean mgrlogin(String name, String pwd) {
            boolean flag = false;
            try {
                    DBconn.init();
                    ResultSet rs = DBconn.selectSql("select * from mgr where name='"+name+"' and pwd='"+pwd+"'");
                    while(rs.next()){
                        if(rs.getString("name").equals(name) && rs.getString("pwd").equals(pwd)){
                            flag = true;
                        }
                    }
                    DBconn.closeConn();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return flag;
        }
      //3.返回用户信息集合
        public List getStuAll() {
            List list = new ArrayList();
            try {
                DBconn.init();
                ResultSet rs = DBconn.selectSql("select * from stu");
                
                while(rs.next()){
                    Stu stu = new Stu();
                    stu.setId(rs.getInt("id"));
                    stu.setName(rs.getString("name"));
                    stu.setPwd(rs.getString("pwd"));
                    stu.setSex(rs.getString("sex"));
                    stu.setHome(rs.getString("home"));
                    stu.setGrades(rs.getString("grades"));
                    stu.setInfo(rs.getString("info"));
                    list.add(stu);
                }
                DBconn.closeConn();
                return list;
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.print("过错");
            }
            return null;
        }
        
        //4.根据用户名返回某个用户信息集合
        public List getStuByName(String namestr)
        {
            List list = new ArrayList();
            try {
                DBconn.init();
                ResultSet rs = DBconn.selectSql("select * from stu where name='"+namestr+"'");
                while(rs.next()){
                    Stu stu = new Stu();
                    stu.setId(rs.getInt("id"));
                    stu.setName(rs.getString("name"));
                    stu.setPwd(rs.getString("pwd"));
                    stu.setSex(rs.getString("sex"));
                    stu.setHome(rs.getString("home"));
                    stu.setGrades(rs.getString("grades"));
                    stu.setInfo(rs.getString("info"));
                    list.add(stu);
                }
                //DBconn.closeConn();
                return list;
            } catch (SQLException e) {
                System.out.println("查询的用户大约不存在!");
                e.printStackTrace();
                
            }finally{
                DBconn.closeConn();
            }
            return null;
            
        }
        
        //5.根据学号返回某个用户信息集合
           public List getStuById(String IdStr)
                {
                    List list = new ArrayList();
                    try {
                        DBconn.init();
                        ResultSet rs = DBconn.selectSql("select * from stu where id='"+IdStr+"'");
                        while(rs.next()){
                            Stu stu = new Stu();
                            stu.setId(rs.getInt("id"));
                            stu.setName(rs.getString("name"));
                            stu.setPwd(rs.getString("pwd"));
                            stu.setSex(rs.getString("sex"));
                            stu.setHome(rs.getString("home"));
                            stu.setGrades(rs.getString("grades"));
                            stu.setInfo(rs.getString("info"));
                            list.add(stu);
                        }
                        DBconn.closeConn();
                        return list;
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    return null;
                    
                }
                
           
        //6.更新用户信息
        public boolean update(int id,String name, String pwd,String sex, String home, String grades,String info) {
            boolean flag = false;
            DBconn.init();
            String sql ="update stu set name ='"+name
                    +"' , pwd ='"+pwd
                    +"' , sex ='"+sex
                    +"' , home ='"+home
                    +"' , grades ='"+grades
                    +"' , info ='"+info+"' where id = "+id;
            int i =DBconn.addUpdDel(sql);
            if(i>0){
                flag = true;
            }
            DBconn.closeConn();
            return flag;
        }
        
        //7.根据id删除用户
        public boolean delete(int id) {
            boolean flag = false;
            DBconn.init();
            String sql = "delete  from stu where id="+id;
            int i =DBconn.addUpdDel(sql);
            if(i>0){
                flag = true;
            }
            DBconn.closeConn();
            return flag;
        }
        
        //8.求成绩总和
        public int sum(){
            int sum = 0;
            try {
                    DBconn.init();
                    
                    ResultSet rs = DBconn.selectSql("SELECT SUM(grades) FROM stu");
                    while(rs.next()){
                        System.out.print("进来了1");
                        if(rs.getInt("SUM(grades)")!=0){
                            System.out.print("进来了2");
                            sum = rs.getInt("SUM(grades)");
                        }
                    }
                    
                    DBconn.closeConn();
                    return sum; 
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            return 0;
        }
        
        //9.求成绩平均
        public int avg(){
            int avg = 0;
            try {
                    DBconn.init();
                    
                    ResultSet rs = DBconn.selectSql("SELECT AVG(grades) FROM stu");
                    while(rs.next()){
                        System.out.print("进来了1");
                        if(rs.getInt("AVG(grades)")!=0){
                            System.out.print("进来了2");
                            avg = rs.getInt("AVG(grades)");
                        }
                    }
                    
                    DBconn.closeConn();
                    return avg; 
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            return 0;
        }
        
        
        

}

②entity中: 创建Stu.java(功能便是javabean)

package com.entity;

public class Stu {
     private int id;
        private String name;
        private String pwd;
        private String sex;
        private String home;
        private String info;
        private String grades;
        
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPwd() {
            return pwd;
        }
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        public String getHome() {
            return home;
        }
        public void setHome(String home) {
            this.home = home;
        }
        public String getInfo() {
            return info;
        }
        public void setInfo(String info) {
            this.info = info;
        }
        public String getGrades() {
            return grades;
        }
        public void setGrades(String grades) {
            this.grades = grades;
        }
        
}

③filter中: 创建过滤器EncodingFilter

//字符编码的过滤器
package com.filter;
 
import java.io.IOException;
 
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
 
public class EncodingFilter implements Filter{
    public EncodingFilter(){
        System.out.println("过滤器布局");
    }
    public void destroy() {
        System.out.println("过滤器销毁");
    }
    public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8"); //将编码改为utf-8
        response.setContentType("text/html;charset=utf-8");
        chain.doFilter(request, response);
    }
 
    public void init(FilterConfig arg0) throws ServletException {
        System.out.println("过滤器初始化");
    }
 
}

④servlet中创建一下类: AddupServlet(用于录退学生信息)

package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.StuDao;
import com.dao.StuDaoImpl;
import com.entity.Stu;

/**
 * Servlet implementation class AddupServlet
 */
@WebServlet("/AddupServlet")
public class AddupServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String name = request.getParameter("name"); //获得jsp页面传过去的参数
        String pwd = request.getParameter("pwd");
        String sex = request.getParameter("sex");
        String home = request.getParameter("home");
        String grades = request.getParameter("grades");
        String info = request.getParameter("info");
        
        Stu stu = new Stu(); //实例化一个对象,组装属性
        stu.setName(name);
        stu.setPwd(pwd);
        stu.setSex(sex);
        stu.setHome(home);
        stu.setGrades(grades);
        stu.setInfo(info);
        
        StuDao ud = new StuDaoImpl();
        
        if(ud.register(stu)){
            request.setAttribute("username", name);  //向request域中安排参数
            request.setAttribute("message", "录入成功");
            request.getRequestDispatcher("/mgrsuccess.jsp").forward(request, response);  //转发到登录页面
        }else{
            
            response.sendRedirect("error.jsp");//重定向到首页
        }
    }

}


DeleteServlet(在管理局部学生的界面实行删除某个学生记录的操作)

package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.StuDao;
import com.dao.StuDaoImpl;

/**
 * Servlet implementation class UpdateServlet
 */
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String id = request.getParameter("id");
        int userId = Integer.parseInt(id);
        
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        String sex = request.getParameter("sex");
        String home = request.getParameter("home");
        String grades = request.getParameter("grades");
        String info = request.getParameter("info");
        
        System.out.println("------------------------------------"+userId);
        
        StuDao ud = new StuDaoImpl();//数据库的操作对象
        if(ud.update(userId, name, pwd, sex, home, grades,info)){
            request.setAttribute("message", "更新成功");
            request.getRequestDispatcher("/SearchallServlet").forward(request, response);
        }else{
            response.sendRedirect("error.jsp");
        }
    }
}


UpdateServlet(在管理全部学生的界面执行更新某个学生记录的操作)

package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.StuDao;
import com.dao.StuDaoImpl;

/**
 * Servlet implementation class UpdateServlet
 */
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String id = request.getParameter("id");
        int userId = Integer.parseInt(id);
        
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        String sex = request.getParameter("sex");
        String home = request.getParameter("home");
        String grades = request.getParameter("grades");
        String info = request.getParameter("info");
        
        System.out.println("------------------------------------"+userId);
        
        StuDao ud = new StuDaoImpl();//数据库的操作对象
        if(ud.update(userId, name, pwd, sex, home, grades,info)){
            request.setAttribute("message", "更新成功");
            request.getRequestDispatcher("/SearchallServlet").forward(request, response);
        }else{
            response.sendRedirect("error.jsp");
        }
    }
}


FindByIdServlet(经过学生Id来查找学生相关信息)

package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.StuDao;
import com.dao.StuDaoImpl;

/**
 * Servlet implementation class FindByIdServlet
 */
@WebServlet("/FindByIdServlet")
public class FindByIdServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FindByIdServlet() {
        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
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

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

        String id = request.getParameter("id");
        
        StuDao ud = new StuDaoImpl();
        
        if(id!= "") {
            if(ud.getStuById(id).isEmpty()) 
            {
                System.out.println("查询到的为空值");
                response.sendRedirect("error.jsp");
            }else 
            {
                request.setAttribute("findlist", ud.getStuById(id));  //向request域中安排参数
                request.setAttribute("message", " 查找成功");
                request.getRequestDispatcher("/findlist.jsp").forward(request, response);  //转发到登录页面
            }
        }else {
            response.sendRedirect("error.jsp");
        }


}
}


FindServlet(经过学生姓名查找相关信息)

package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.StuDao;
import com.dao.StuDaoImpl;

/**
 * Servlet implementation class FindServlet
 */
@WebServlet("/FindServlet")
public class FindServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FindServlet() {
        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
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        
        String name = request.getParameter("name"); 
        
        StuDao ud = new StuDaoImpl();
        
        if(name != "") {
            if(ud.getStuByName(name).isEmpty()) 
            {
                System.out.println("查询到的为空值");
                response.sendRedirect("error.jsp");
            }else 
            {
                request.setAttribute("findlist", ud.getStuByName(name));  //向request域中放置参数
                request.setAttribute("message", " 查找成功");
                request.getRequestDispatcher("/findlist.jsp").forward(request, response);  //转发到登录页面
            
            }       
        }else {
            response.sendRedirect("error.jsp");
        }
    
        
    }

}


MgrLoginServlet(管理员登录的业务逻辑)

package com.servlet;
 
import java.io.IOException;
//import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.dao.StuDao;
import com.dao.StuDaoImpl;
 
@WebServlet("/MgrLoginServlet")
//用来实现对用户登录的操作
public class MgrLoginServlet extends HttpServlet {  
    private static final long serialVersionUID = 1L;
    //必要继承HttpServlet  侧重写doGet  doPost方法
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);  //将信息使用doPost方法执行   对应jsp页面中的form表单中的method
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String name = request.getParameter("name"); //得到jsp页面传过去的参数
        String pwd = request.getParameter("pwd");
        
        StuDao ud = new StuDaoImpl();
        
        if(ud.mgrlogin(name, pwd)){
            request.setAttribute("message", "欢迎管理员"+name); //向request域中放相信息
            request.getRequestDispatcher("/mgrsuccess.jsp").forward(request, response);//转发到成功页面
        }else{
            response.sendRedirect("error.jsp"); //重定向到error页面
        }
    }
 
}



SearchallServlet(管理员管理所有学生的类)

package com.servlet;

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

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

import com.dao.StuDao;
import com.dao.StuDaoImpl;
import com.entity.Stu;

/**
 * Servlet implementation class SearchallServlet
 */
@WebServlet("/SearchallServlet")
public class SearchallServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        
        StuDao ud = new StuDaoImpl();//定义数据库操作类的对象
        List stuAll = ud.getStuAll();//获取到所有的对象而且存储到list里
        request.setAttribute("stuAll", stuAll);
        
        //求和
        int sum = ud.sum();
        request.setAttribute("sum", sum);
        
        //求平均
        int avg = ud.avg();
        request.setAttribute("avg", avg);
        
        request.getRequestDispatcher("/mgrall.jsp").forward(request, response);
    }
}


StuLoginServlet(学生登录的页面)

package com.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.StuDao;
import com.dao.StuDaoImpl;

/**
 * Servlet implementation class StuLoginServlet
 */
@WebServlet("/StuLoginServlet")
public class StuLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    //必要继承HttpServlet  侧重写doGet  doPost方法
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);  //将信息使用doPost方法执行   对应jsp页面中的form表单中的method
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String name = request.getParameter("name"); //得到jsp页面传过来的参数
        String pwd = request.getParameter("pwd");
        
        StuDao ud = new StuDaoImpl();
        
        if(ud.stulogin(name, pwd)){
            request.setAttribute("message", "欢迎学生用户"+name); //向request域中放相信息
            request.setAttribute("name", name);
            request.getRequestDispatcher("/stusuccess.jsp").forward(request, response);//转发到成功页面
        }else{
            response.sendRedirect("error.jsp"); //重定向到error页面
        }
    }
}


⑤util包中创建: DBconn.java(链接数据库的方法,留意其中的用户名密码要和你以前配置数据库的要同等)

//数据库连接的类
package com.util;
 
import java.sql.*;
 
public class DBconn {
    static String url = "jdbc:mysql://localhost:3306/test?useunicuee=true& characterEncoding=utf8"; 
    static String username = "root"; 
    static String password = "363316495"; 
    static Connection  conn = null;
    static ResultSet rs = null;
    static PreparedStatement ps =null;
    public static void init(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            System.out.println("init [SQL驱动步伐初始化失利!]");
            e.printStackTrace();
        }
    }
    public static int addUpdDel(String sql){
        int i = 0;
        try {
            PreparedStatement ps =  conn.prepareStatement(sql);
            i =  ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("sql数据库增删改十分");
            e.printStackTrace();
        }
        
        return I;
    }
    public static ResultSet selectSql(String sql){
        try {
            ps =  conn.prepareStatement(sql);
            rs =  ps.executeQuery(sql);
        } catch (SQLException e) {
            System.out.println("sql数据库查询非常");
            e.printStackTrace();
        }
        return rs;
    }
    public static void closeConn(){
        try {
            conn.close();
        } catch (SQLException e) {
            System.out.println("sql数据库封闭异常");
            e.printStackTrace();
        }
    }
}

4.1.3 页面的编写

addup.jsp (学生信息录入)





学生信息录入




       输入用户名:
       输入密码:
       挑选性别:女
       挑选故乡:
       
           ShangHai
           Beijing
           ChangSha
        
        输入成绩:
        填写个人书息:
       
       




error.jsp(过错页面)





错误页面



~(~ ̄▽ ̄)~堕落啦!请实行检查一下输入大约页面逻辑呦
回到首页



findlist.jsp(查找学生列表)

     
    
 




查找列表


  ${message} --%>

    
        
            ID
            姓名
            性别
            密码
            故乡
            成绩
            备注
        

      
     
       
      
        
           
           
           
           
           
           
           
         
       
     
   
    
     
    

    




index.jsp (主页进口)





欢迎登陆zyz的学生管理系统



欢迎登陆zyz的学生管理系统
管理员登录
学生登录



mgrall.jsp(管理所有学生表)

     
    
 




查找列表


  ${message} --%>

    
        
            ID
            姓名
            性别
            密码
            家乡
            成绩
            备注
        

      
     
       
      
        
           
           
           
           
           
           
           
         
       
     
   
    
     
    

    




mgrlogin.jsp (管理员登录界面)





管理员登录页面



管理员登录页面
  
        用户名:
        密码:  
                    
  



mgrsuccess.jsp(管理员登录成功后的操作页面)





  
    管理员登录成功
    
  
  
            ${message}   
            检查所有学生信息表
            
            录退学生信息

       
       输入用户名(按照用户名查找):
       
       
       

       
       输入Id号(按照学号查找):
       
       
            
  



stulogin.jsp(学生登录页面)





学生登录页面



学生登录页面

        用户名:
        密码:  
               




stusuccess.jsp(学生登录成功后的查找我的成绩页面)





学生登录成功页面



学生登录成功页面
${message} 








            



4.2 测试:





主页.png






管理员登陆成功操作界面.png






管理所有学生表(包括更新和删除).png






管理员登录界面.png






学生信息录入.png






按名Id查找.png






学生登录界面.png






学生登陆成功操作界面.png






表现该学生成绩的页面 .png






错误.png

五、     主要源代码

详见四: 其中最重要的是相关数据的操作:(有大概屏幕前的你用的不是Mysql,那末你的sql语句可能能不太同样,如今你自己的数据库中试好了再来编写java代码)

//1.注册
        public boolean register(Stu stu) {
            boolean flag = false;
            DBconn.init();
            int i =DBconn.addUpdDel("insert into stu(name,pwd,sex,home,grades,info) " +
                    "values('"+stu.getName()+"','"+stu.getPwd()+"','"+stu.getSex()+"','"+stu.getHome()+"','"+stu.getGrades()+"','"+stu.getInfo()+"')");
            if(i>0){
                flag = true;
            }
            DBconn.closeConn();
            return flag;
        }
        //2.登录(学生与管理员的差别是查找的表纷比方样)
        //(学生)
        public boolean stulogin(String name, String pwd) {
            boolean flag = false;
            try {
                    DBconn.init();
                    ResultSet rs = DBconn.selectSql("select * from stu where name='"+name+"' and pwd='"+pwd+"'");
                    while(rs.next()){
                        if(rs.getString("name").equals(name) && rs.getString("pwd").equals(pwd)){
                            flag = true;
                        }
                    }
                    DBconn.closeConn();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return flag;
        }
      //(管理员)
        public boolean mgrlogin(String name, String pwd) {
            boolean flag = false;
            try {
                    DBconn.init();
                    ResultSet rs = DBconn.selectSql("select * from mgr where name='"+name+"' and pwd='"+pwd+"'");
                    while(rs.next()){
                        if(rs.getString("name").equals(name) && rs.getString("pwd").equals(pwd)){
                            flag = true;
                        }
                    }
                    DBconn.closeConn();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return flag;
        }
      //3.返回用户信息集合
        public List getStuAll() {
            List list = new ArrayList();
            try {
                DBconn.init();
                ResultSet rs = DBconn.selectSql("select * from stu");
                
                while(rs.next()){
                    Stu stu = new Stu();
                    stu.setId(rs.getInt("id"));
                    stu.setName(rs.getString("name"));
                    stu.setPwd(rs.getString("pwd"));
                    stu.setSex(rs.getString("sex"));
                    stu.setHome(rs.getString("home"));
                    stu.setGrades(rs.getString("grades"));
                    stu.setInfo(rs.getString("info"));
                    list.add(stu);
                }
                DBconn.closeConn();
                return list;
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.print("错误");
            }
            return null;
        }
        
        //4.根据用户名返回某个用户信息集合
        public List getStuByName(String namestr)
        {
            List list = new ArrayList();
            try {
                DBconn.init();
                ResultSet rs = DBconn.selectSql("select * from stu where name='"+namestr+"'");
                while(rs.next()){
                    Stu stu = new Stu();
                    stu.setId(rs.getInt("id"));
                    stu.setName(rs.getString("name"));
                    stu.setPwd(rs.getString("pwd"));
                    stu.setSex(rs.getString("sex"));
                    stu.setHome(rs.getString("home"));
                    stu.setGrades(rs.getString("grades"));
                    stu.setInfo(rs.getString("info"));
                    list.add(stu);
                }
                //DBconn.closeConn();
                return list;
            } catch (SQLException e) {
                System.out.println("查询的用户可能不存在!");
                e.printStackTrace();
                
            }finally{
                DBconn.closeConn();
            }
            return null;
            
        }
        
        //5.根据学号返回某个用户信息集合
           public List getStuById(String IdStr)
                {
                    List list = new ArrayList();
                    try {
                        DBconn.init();
                        ResultSet rs = DBconn.selectSql("select * from stu where id='"+IdStr+"'");
                        while(rs.next()){
                            Stu stu = new Stu();
                            stu.setId(rs.getInt("id"));
                            stu.setName(rs.getString("name"));
                            stu.setPwd(rs.getString("pwd"));
                            stu.setSex(rs.getString("sex"));
                            stu.setHome(rs.getString("home"));
                            stu.setGrades(rs.getString("grades"));
                            stu.setInfo(rs.getString("info"));
                            list.add(stu);
                        }
                        DBconn.closeConn();
                        return list;
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    return null;
                    
                }
                
           
        //6.更新用户信息
        public boolean update(int id,String name, String pwd,String sex, String home, String grades,String info) {
            boolean flag = false;
            DBconn.init();
            String sql ="update stu set name ='"+name
                    +"' , pwd ='"+pwd
                    +"' , sex ='"+sex
                    +"' , home ='"+home
                    +"' , grades ='"+grades
                    +"' , info ='"+info+"' where id = "+id;
            int i =DBconn.addUpdDel(sql);
            if(i>0){
                flag = true;
            }
            DBconn.closeConn();
            return flag;
        }
        
        //7.根据id删除用户
        public boolean delete(int id) {
            boolean flag = false;
            DBconn.init();
            String sql = "delete  from stu where id="+id;
            int i =DBconn.addUpdDel(sql);
            if(i>0){
                flag = true;
            }
            DBconn.closeConn();
            return flag;
        }
        
        //8.求成绩总和
        public int sum(){
            int sum = 0;
            try {
                    DBconn.init();
                    
                    ResultSet rs = DBconn.selectSql("SELECT SUM(grades) FROM stu");
                    while(rs.next()){
                        System.out.print("进来了1");
                        if(rs.getInt("SUM(grades)")!=0){
                            System.out.print("进来了2");
                            sum = rs.getInt("SUM(grades)");
                        }
                    }
                    
                    DBconn.closeConn();
                    return sum; 
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            return 0;
        }
        
        //9.求成绩平均
        public int avg(){
            int avg = 0;
            try {
                    DBconn.init();
                    ResultSet rs = DBconn.selectSql("SELECT AVG(grades) FROM stu");
                    while(rs.next()){
                        System.out.print("进来了1");
                        if(rs.getInt("AVG(grades)")!=0){
                            System.out.print("进来了2");
                            avg = rs.getInt("AVG(grades)");
                        }
                    }
                    
                    DBconn.closeConn();
                    return avg; 
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            return 0;
        }

六、 碰到的题目和解决方法

进程中中主要遇到的小问题很多,来由起因主如果jsp域相关的问题。遇到的比力顺手的问题主如果: 数据库语句编写错误到时数据库的增删改查错误 屏幕快照 2018-12-12 上午9.16.22.png

屏幕快照 2018-12-12 上午9.24.04.png 办理:

"select * from mgr where name='"+name+"' and pwd='"+pwd+"'"

肯定要留意其中""与''和+等标记之间的干系,因为这里的aql语句是一个String,其中又用到了传入的参数,字符串拼接的工夫肯定要注意! 七、 存在的缺少和改造的思路

1、界面的问题:办理思路是导入的一个css的包,而后在jsp页面援用了一下 2、查询时未输入也能够录入的问题:思路是增加一个录入判定 3、数据的中文问题:思路是变动字符编码

项面前目今载:(更新下载方式) 链接:download/weixin_40486026/10884068