javaee入门笔记2
开发基于Mysql、Servlet、JSP的作业管理系统;
实现功能:
老师:添加作业,添加学生。查询学生作业;
学生:根据老师添加的作业,进行作业提交;
简要思路实现:
- 首先先用Mysql对数据库进行设计;
- 然后创建JAVAEE WEB项目,通过JDBC连接数据库并编写Dao层对数据库进行操作;
- 接下来编写servlet提供服务器接口;
- 最后编写JSP提供网页端可视化管理。
#
步骤
1.数据库设计(利用powerdesigner设计)![](/assets/blank.gif)
teacher表:包括老师id,老师姓名以及密码
student表:包括学生id,学生姓名,学生密码
homework表:包括作业id,作业名称,作业内容,截止日期
**assign表:**包括老师创建作业的时间
submit表:包括学生提交作业时间,提交内容,学生作业状态(提交为1,未提交为0)
mysql语句:
create table assign
(
hid int not null,
tid int not null,
create_time datetime,
primary key (hid, tid)
);
create table homework
(
hid int not null,
title varchar(50),
acquirement varchar(500),
deadline datetime,
primary key (hid)
);
create table student
(
sid int not null,
sname char(10) not null,
spassword varchar(8),
primary key (sid)
);
create table submit
(
hid int not null,
sid int not null,
submit_time datetime,
content varchar(1000),
status int,
primary key (hid, sid)
);
create table teacher
(
tid int not null,
tname char(10) not null,
tpassword varchar(8),
primary key (tid)
);
alter table assign add constraint FK_about foreign key (hid)
references homework (hid) on delete restrict on update restrict;
alter table assign add constraint FK_design foreign key (tid)
references teacher (tid) on delete restrict on update restrict;
alter table submit add constraint FK_get foreign key (hid)
references homework (hid) on delete restrict on update restrict;
alter table submit add constraint FK_sub foreign key (sid)
references student (sid) on delete restrict on update restrict;
2.创建javaee项目
建立项目详见笔记1
新建项目并且按照如图所示设置
3.部分源码
homework.java
package db;public class homework {public static final int SUBMIT_STATUS_DOING = 0;public static final int SUBMIT_STATUS_SUBMITTED = 1;private int id;private String title;private String requirement;private String deadline;public homework(){}public homework(int id, String title, String requirement,String deadline) {this.id = id;this.title = title;this.requirement = requirement;this.deadline=deadline;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getRequirement() {return requirement;}public void setRequirement(String requirement) {this.requirement = requirement;}public String getDeadline(){ return deadline;}public void setDeadline(String deadline) {this.deadline = deadline;}}
student.java
package db;public class student {private int id;private String name;private String password;public student(){}public student(int id, String name, String password) {this.id = id;this.name = name;this.password = password;}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 getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}**teacher.java**```java
在这里插入代码片
```package db;public class teacher {private int id;private String name;private String password;public teacher(){}public int getId() {return id;}public teacher(int id, String name, String password) {this.id = id;this.name = name;this.password = password;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}
jdbc.java
package jdbc;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;public class jdbc {//单实例Connectionprivate static Connection connection;//JDBC驱动名private static final String driverName = "com.mysql.cj.jdbc.Driver";//连接数据库的urlprivate static final String url = "jdbc:mysql://127.0.0.1:3306/homework?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true";//数据库用户名private static final String user = "root";//数据库用户的密码private static final String password = "qq137139";//私有constructor,防止实例化private jdbc(){ }public static Connection getConnection() {if(connection == null) {try {Class.forName(driverName);connection = DriverManager.getConnection(url, user, password);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();try {connection.close();} catch (SQLException e1) {e.printStackTrace();}}}return connection;}//测试获取connection是否成功public static void main(String[] args) {Connection connection = jdbc.getConnection();if (connection != null)System.out.println("连接成功");elseSystem.out.println("连接失败");}
}
servlet
public class assignservlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {int hid = Integer.parseInt(req.getParameter("hid"));String[] studentIDs = req.getParameter("students").split(";");submitdao submitDao = new submitdao1();for (String studentId : studentIDs) {int sid = Integer.parseInt(studentId);submitDao.addSubmit(sid, hid);}req.getRequestDispatcher("/successful.jsp").forward(req, resp);}
}
public class createservlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {int tid = Integer.parseInt(req.getParameter("tid"));String title = req.getParameter("title");String requirement = req.getParameter("requirement");homeworkdao homeworkDao = new homeworkdao1();if (homeworkDao.addHomework(title, requirement)) {int hid = homeworkDao.getHomeworkByTitle(title).getId();assigndao assignDao = new assigndao1();assignDao.assign(tid, hid);req.setAttribute("homeworkList", homeworkDao.getHomeworkOfTeacher(tid));req.getRequestDispatcher("/assign_homework.jsp").forward(req, resp);} else {req.getRequestDispatcher("/fail.jsp").forward(req, resp);}}
}
public class loginservlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String userType = req.getParameter("user_type");String name = req.getParameter("name");String password = req.getParameter("password");if(userType.equals("teacher")){teacherdao teacherDao = new teacherdao1();teacher teacher = teacherDao.getTeacher(name);if(password.equals(teacher.getPassword())){req.setAttribute("teacher", teacher);req.getRequestDispatcher("create_homework.jsp").forward(req, resp);}else{resp.getWriter().println("用户名或密码错误");}}else if(userType.equals("student")){studentdao studentDao = new studentdao1();student student = studentDao.getStudent(name);if(password.equals(student.getPassword())){req.setAttribute("sid", student.getId());List<homework> homeworkList = new homeworkdao1().getHomeworkOfStudent(student.getId());req.setAttribute("homeworkList", homeworkList);req.getRequestDispatcher("submit_homework.jsp").forward(req, resp);}else{resp.getWriter().println("用户名或密码错误");}}}
}public class registerservlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String userType = req.getParameter("user_type");String name = req.getParameter("name");String password = req.getParameter("password");if(userType.equals("teacher")){teacherdao teacherDao = new teacherdao1();if(teacherDao.addTeacher(name, password)){req.getRequestDispatcher("index.jsp").forward(req, resp);}else{resp.getWriter().println("注册失败");}}else{studentdao studentDao = new studentdao1();if(studentDao.addStudent(name, password)){req.getRequestDispatcher("index.jsp").forward(req, resp);}else{resp.getWriter().println("注册失败");}}}
}public class submitservlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {int sid = Integer.parseInt(req.getParameter("sid"));int hid = Integer.parseInt(req.getParameter("hid"));String content = req.getParameter("content");submitdao submitDao = new submitdao1();submitDao.submit(sid, hid, content);req.getRequestDispatcher("/successful.jsp").forward(req, resp);}
}
项目源码
https://github.com/1goddness2/javaee.git
javaee入门笔记2相关推荐
- Shiro 入门笔记,整合SpringBoot,Redis
Shiro 入门笔记 视频地址:https://www.bilibili.com/video/BV1uz4y197Zm 感谢编程不良人的教程 1. 权限管理 权限管理包括用户 身份认证 和 授权 两部 ...
- vue router 入门笔记
vue router 入门笔记 tips: components优先级大于component,即当一个route对象里同时配置了component和components时component视为无效 即 ...
- 十年公务员转行IT,自学AI三年,他淬炼出746页机器学习入门笔记
整理 | Jane 编辑 | Just 出品 | AI科技大本营(ID:rgznai100) 近期,梁劲传来该笔记重大更新的消息.<机器学习--从入门到放弃>这本笔记的更新没有停止,在基于 ...
- html缩进快捷键_HTML 入门笔记
HTML 入门笔记 HTML (HyperText Markup Language) 不是一门编程语言,而是一种用来告知浏览器如何组织页面的标记语言. HTML 可复杂.可简单,一切取决于开发者. 它 ...
- Python3入门笔记(1) —— windows安装与运行
Python3入门笔记(1) -- windows安装与运行 Python的设计哲学是"优雅"."明确"."简单".这也是我喜欢Python ...
- [Java入门笔记] 面向对象编程基础(二):方法详解
2019独角兽企业重金招聘Python工程师标准>>> 什么是方法? 简介 在上一篇的blog中,我们知道了方法是类中的一个组成部分,是类或对象的行为特征的抽象. 无论是从语法和功能 ...
- React.js入门笔记
# React.js入门笔记 核心提示 这是本人学习react.js的第一篇入门笔记,估计也会是该系列涵盖内容最多的笔记,主要内容来自英文官方文档的快速上手部分和阮一峰博客教程.当然,还有我自己尝试的 ...
- python3入门与进阶笔记_我的Python3萌新入门笔记
Python3萌新入门笔记是一系列真实的自学笔记. 当然,它也是比较全面的入门教程,共包括54篇笔记. 从第一篇笔记开始,大半年的时间我都是在自学和组织教程内容. 我觉得有必要,把我自己的学习过程和大 ...
- MySql入门笔记二~悲催的用户
这些是当年小弟的MySql学习笔记,木有多么复杂的结构操作,木有多炫丽的语句开发,木有...总之就是木有什么技术含量... 日复一日,彪悍的人生伴随着彪悍的健忘,运维操作为王,好记性不如烂笔头,山水有 ...
最新文章
- 数据窗口retrieve查询结果生成新表_SQL系列之窗口函数及经典使用场景,如topN排名问题...
- springmvc二十一:自定义类型转换器
- ClickHouse数据分析列式数据库概述
- 华为发布最强 AI 处理器昇腾 910,全场景 AI 框架 MindSpore 将开源
- iis web服务扩展_Web服务器系统都有哪些类型?都有什么优点呢?
- Atcoder Grand Contest 036 D - Negative Cycle
- Unity3D 保姆级安装教程与收费方案和版本、下载地址,看不会算我输
- linux u盘 随身,教你安装CentOS到U盘,制作随身Linux系统
- matlab 直流-直流变换器毕业论文,基于MATLAB直流-直流变换器的研究毕业论文.docx-资源下载在线文库www.lddoc.cn...
- hass智能 小米扫地机器人_Siri能控制小米扫地机器人吗_小米智能家居控制系统...
- 每日英语好文翻译(11)
- NEON intrinsics 函数模式介绍
- excel插件开发,Smartbi免费版安装流程
- S3C6410 SD Card一键烧写 WINCE 6.0
- 3DMAX - 使用编辑多边形的小技巧
- Linux系统双显示器4K分辨率配置
- BeatBox终极版——Head first java 650
- 准大三学生暑期社会实践真实感悟
- 项目开发SOP前端执行SOP
- 激光点云系列之一:详解激光雷达点云数据的处理过程