项目地址:
链接: https://pan.baidu.com/s/1Ul97vJQ6dVRqCAw6i5jHhg
提取码: mpwt
1.设计任务书
1.1 简单的学生管理体统
本人计划编写一个简单的学生管理系统,主要从两个身份来说——老师和学生,对于老师来说,带验证码的登录注册,对学生进行增删改查,以分页的形式展示学生的所有信息;对于学生来说,带验证码的登录注册,以及查询自己的信息,修改自己的信息,以及查询其他学生的信息除密码以外。具体要求如下:
1、实现数据库连接,数据库有两个表,一个教师表,一个学生表教师表只有简单的username和password两个属性,学生表有sid name password gender birthday address等属性;
2、学生信息的增删改查;
3、写出老师及学生的登录注册的代码;
4、用代码生成一张验证码;
5、对学生信息进行分页
6、在网页上具体操作,并显示结果
1.2 本系统涉及的知识点
函数、数据库、jsp、servlet、面向对象、动画
2.功能设计(左对齐,宋体粗四号)
2.1 算法设计
本系统需要实现的功能要求:
教师登陆成功后会有以下选择如图1所示:
(1) 选择要操作:
(2) 选择5后,进入到如图2所示的菜单,输入要查询学生的学号,SelectServlet拿到相应的数据后,通过创建对象调用Student类的select方法进行查询,然后SelectServlet把查到的数据传给selectstudent.jsp在网页上显示出来如图3所示的主菜单
(3) 根据所选菜单编写相应代码:
求查询函数:
2.2 部分模块流程图
3.程序代码设计
(1) 数据库连接(需要引入jar包)
public class DBO {
static String url="jdbc:mysql://localhost:3306/dl";
tatic String user="root";
static String password="123";
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn=null;
try {
conn=DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
(2) 老师和学生类
a.老师类
public class Teacher {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
b.学生类
id sid name password gender birthday address属性以及相应的get set方法(与上面相同)
(3) 学生登录注册以及老师要操作的增删改查
3.1.学生相关操作的接口
public interface StudentManager {
//登录时
public Student CheckLogin(String name,String sid,String password);
//添加学生信息
public boolean addStudent(Student student);
//更新学生信息
public boolean updateStudent(String sid,Student student);
//删除学生信息
public boolean deleteStudent(String sid);
//查询所有的学生信息
public List getAllStudent(String sid);
}
3.2.学生相关操作的接口的实现类
public class StudentManagerImpl implements StudentManager {
public Student CheckLogin(String name, String sid,String password) {
Student s=null;
Connection conn=null;
Statement st=null;
ResultSet rs=null;
String sql="select * from student";
conn=DBO.getConnection();
try {
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next()) {
String nm=rs.getString("name");
String id=rs.getString("sid");
String psd=rs.getString("password");
if(nm.equals(name) && id.equals(sid) && psd.equals(password)) {
s= new Student();
s.setName(nm);
s.setSid(id);
s.setPassword(psd);
s.setGender(rs.getString("gender"));
s.setAddress(rs.getString("address"));
s.setId(rs.getInt("id"));
s.setBirthday(rs.getString("birthday"));
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}
public boolean addStudent(Student student) {
// TODO Auto-generated method stub
boolean flag=false;
String sql="insert into student(sid,name,password,gender,birthday,address) values(?,?,?,?,?,?)";
Connection conn=null;
PreparedStatement pst=null;
conn=DBO.getConnection();
try {
pst=conn.prepareStatement(sql);
pst.setString(1,student.getSid());
pst.setString(2,student.getName());
pst.setString(3, student.getPassword());
pst.setString(4,student.getGender());
pst.setString(5,student.getBirthday());
pst.setString(6,student.getAddress());
pst.executeUpdate();
flag=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
public boolean updateStudent(String sid, Student student) {
// TODO Auto-generated method stub
boolean flag=false;
String sql="update student set name=?,password=?,gender=?,birthday=?,address=? where sid='"+sid+"'";
Connection conn=null;
PreparedStatement pst=null;
conn=DBO.getConnection();
try {
pst=conn.prepareStatement(sql);
pst.setString(1,student.getName());
pst.setString(2,student.getPassword());
pst.setString(3,student.getGender());
pst.setString(4,student.getBirthday());
pst.setString(5,student.getAddress());
pst.executeUpdate();
flag=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
public boolean deleteStudent(String sid) {
// TODO Auto-generated method stub
boolean flag=false;
String sql="delete from student where sid='"+sid+"'";
System.out.println(sql);
Connection conn=null;
PreparedStatement pst=null;
conn=DBO.getConnection();
try {
pst=conn.prepareStatement(sql);
int row=pst.executeUpdate();
if(row>0)
flag=true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
public List getAllStudent(String sid) {
// TODO Auto-generated method stub
List list=new ArrayList();
String sql="select * from student where sid='"+sid+"'";
Connection conn=null;
Statement st=null;
ResultSet rs=null;
conn=DBO.getConnection();
try {
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next()) {
Student s=new Student();
s.setSid(sid);
s.setName(rs.getString("name"));
s.setPassword(rs.getString("password"));
s.setGender(rs.getString("gender"));
s.setBirthday(rs.getString("birthday"));
s.setAddress(rs.getString("address"));
list.add(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}
3.3登陆的servlet
public class CheckLogin extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String name=request.getParameter("username");
String sid=request.getParameter("sid");
String password=request.getParameter("password");
String checkcode=request.getParameter("checkcode");
String piccode=(String) request.getSession().getAttribute("piccode");
StudentManager smg = new StudentManagerImpl();
Student s=new Student();
s=smg.CheckLogin(name, sid,password);
HttpSession session=request.getSession();
session.setAttribute("s", s);
if(s!=null && checkcode.equalsIgnoreCase(piccode)) {
response.sendRedirect("person.jsp");
}else{
response.sendRedirect("error.jsp");
}
}
}
3.4注册和登录的servlet道理相似
(4) 老师的登录注册与学生的相似(就已不在写了)
(5) 验证吗
public class PicCodeGenerator extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
BufferedImage bi=new BufferedImage(68,22,BufferedImage.TYPE_INT_BGR);
Graphics g=bi.getGraphics();
Color color=new Color(224,205,19);
g.setColor(color);
g.fillRect(0, 0, 68,22);
char [] c="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
Random random= new Random();
int len=c.length;
int temp;
StringBuffer sb=new StringBuffer(4);
for(int i = 0 ; i<4 ; i++){
char index = c[(int)(0+Math.random()*c.length)];
Font font = new Font("幼圆",Font.BOLD,25);
Color clo = new Color(random.nextInt(255));
g.setFont(font);
g.setColor(clo);
g.drawString(index+"",i*15,18);
g.setColor(new Color(random.nextInt(150),random.nextInt(220),random.nextInt(180)));
sb.append(index);
}
for(int i=0;i<5;i++) {
g.drawLine(random.nextInt(68),random.nextInt(22),random.nextInt(68), random.nextInt(22));
}
request.getSession().setAttribute("piccode", sb.toString());
ImageIO.write(bi, "JPG", response.getOutputStream());
}
}
(6) 分页
6.1servlet
public class StudentServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
StudentService studentService = new StudentService();
String uri = request.getRequestURI()+"?"+request.getQueryString();
int index=uri.indexOf("StudentServlet");
if(index==-1)
uri="http://localhost:8080/student/StudentServlet?null";
index = uri.lastIndexOf("&pc=");
if(index != -1 )
uri = uri.substring(0,index); //获取当前页页码
int pc = 1;
String param = request.getParameter("pc");
if(param != null && !param.trim().isEmpty()){
//将string类型强制转化为Int类型
pc = Integer.parseInt(param);
}
PageBean pageBean = studentService.findByPageBean(pc);
//pageBean.setPc(pc);
pageBean.setUrl(uri);
//将数据放到Request域
request.setAttribute("pb", pageBean);
//转发
request.getRequestDispatcher("Partner.jsp").forward(request, response);
}
}
4.界面及运行效果、测试工具与测试效果
4.1身份选择(碰到教师和学生,这两个词会转动,老师这个小人会左右移动)
4.11选择教师后,进入老师登录界面,下面有超链接返回和注册
(1)填入正确的username和password以及验证码吗后进入
(2)对学生进行增删改查操作
(3)用分页查询所有学生(上下一页可以跳转)
4.12选择学生后,进入学生登录界面,下面有超链接返回和注册
(1) 学生登陆成功的界面
(1) 学生登陆成功的界面
5.设计总结
1.本人在程序设计中遇到了非常多的困难,通过找他人请教,百度,以及翻阅书籍,一点一点解决错误,收获颇多,学到了非常多的知识,以及更加巩固我以前的知识
2.调试程序:先判断是哪一步程序出错,缩小范围在具体修改
比如:登陆失败时
6.致谢
通过这次的实践,我不仅对学到的东西加以综合,巩固,以及学到许多新的知识,收获颇多,这也是我第一次做的小项目,虽然比较简单,但感到非常充实,也更加的熟悉如何调试程序,等等。
参考文献
《Java基础教程》
http://www.imooc.com/
http;www.baidu.com/
《传智播客_2015年java基础视频-深入浅出版》
等等