简单的学生管理系统

许久没写博客了。。中间发生了许多事情。。重新开始吧。

1.简介

用所学的一点知识写了一个简单的学生管理系统,实际上非常简陋,只有简单的增删查的功能,涉及到的只是有HTML,jsp,Servlet等,不嫌弃的话就随意看一下吧。

2.架构

非常简单的结构,这是一个dynamic web project,重要的结构一个是上面的src包含各种Servlet 来接受请求和转发数据,以及一个student类作为Model 还有一个StudentDao作为Dao层来连接数据库进行数据库的操作,还有一个比较重要的是下面的WebContent文件夹,有web.xml,各种各样的jar包和jsp文件。

3.增删查的实现原理

3.1共同原理

可以看到在工程中我们将整个工程分成了三个层次,进行了分层开发,第一层jsp向servlet层发送参数 并接受由其传回来的数据并在页面上显示,第二层servlet层只是作为一个中间层进行数据的接受与转发,第三层dao层就是连接数据库进行数据库操作,获取数据。

3.2以查找为例详解。

查找这个功能算是包含了其他操作的完整流程,而又有其他操作不具备的流程,所以以此为例来进行一下详解。

3.2.1查找的jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.zzxtit.stu.Student" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
	Object o=request.getAttribute("stulist");
	List<Student> stulist=new ArrayList<Student>();
	if(o!=null){
		stulist=(List<Student>)o;
	}
	Student stu=(Student)request.getAttribute("student");
	String contextpath=request.getContextPath();

%>
<body>
	<div align="center">
		<form action="<%=contextpath %>/searchstu" method="post" >
			<div>学生管理系统</div><br>
			<div>学生姓名</div><input name="stuName" type="text" value="<%=stu.getStuName()!=null?stu.getStuName():" "%>">
			<div>学生编号</div><input name="stuNo" type="text" value="<%=stu.getStuNo()!=null?stu.getStuNo():" "%>">
			<input type="submit" placeholder="提交">
		</form>
		<hr>
		<table cellspacing="0" border="1px solid black">
			<tr>
				<th>学生姓名</th><th>学生编号</th><th>学生性别</th><th>操作</th>
			</tr>
			<%for(Student student:stulist){ %>
			<tr>
				<th><%=student.getStuName() %></th><th><%=student.getStuNo() %></th><th><%=student.getGender() %></th><th><a href="DelStuServlet?stuNumber=<%=student.getStuNo() %>">删除</a><a>修改</a></th>
			</tr>
			<% }%>
		</table>
	</div>

</body>
</html>

在这个jsp中,比较重要的主要有两点,一是用form表单提交数据,而使用表格来呈现获取的数据。提交数据用的是form表单,其中的action属性值为提交的“目的地”,method属性的值为提交的方法。而要获取数据的话,要用到内置对象request,然后在table表格中用Java代码来逐行显示。

3.2.2查找的servlet

package com.zzxtit.stu;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
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;

@WebServlet("/searchstu")
public class SearchStuServlet extends HttpServlet{
	
	private static StudentDao stuDao=new StudentDao();
	
	protected void  doGet(HttpServletRequest request,HttpServletResponse response) {
		try {
			doPost(request,response);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	protected void  doPost(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
		request.setCharacterEncoding("utf-8");
		String	stuName=request.getParameter("stuName");
		String stuNo=request.getParameter("stuNo");
		List stulist=new ArrayList<Student>();
		stulist=stuDao.searchStu(stuName, stuNo);
		Student student=new Student();
		student.setStuNo(stuNo);
		student.setStuName(stuName);
		try {
			request.setAttribute("stulist", stulist);
			request.setAttribute("student", student);
			request.getRequestDispatcher("stu/student.jsp").forward(request, response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

可以看出,这是非常垃圾的代码。。这个servlet中最主要的就是doPost中的方法,在这个方法中用request来获取jsp中的参数,但是返回的变量都是String类型,个别变量要进行转化,然后用Dao类型的变量调用相关方法进行数据获取,然后再装入request中并进行页面跳转。

3.3.3查找的Dao层方法

	
	public List<Student>  searchStu(String stuName,String stuNo) {
		String user="root";
		String password="root";
		String url="jdbc:mysql://localhost:3306/xtdb";
		String sql="select * from student where 1=1";
		if(stuName!=null && !"".equals(stuName)) {
			sql+=" and stuName='"+stuName+"'";
			System.out.println("------"+sql);
		}
		if(stuNo!=null && !"".equals(stuNo)) {
			sql+=" and stuNo='"+stuNo+"'";
			System.out.println("------"+sql);
		}
		Connection conn=null;
		Statement stat=null;
		ResultSet rs=null;
		List stulist=new ArrayList<Student>();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection(url, user, password);
			stat=conn.createStatement();
			rs=stat.executeQuery(sql);
			while(rs.next()){
				Student student=new Student();
				student.setStuName(rs.getString(1));
				student.setStuNo(rs.getString(2));
				student.setGender(rs.getInt(3));
				stulist.add(student);
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(rs!=null) {
					rs.close();
				}
				if(stat!=null) {
					stat.close();
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return stulist;
	}

这是一个平平无奇的search方法,链接数据库已经是老生常谈,加载驱动,创建连接,创建statement并执行数据库操作,关闭资源。但这里有几个注意点,1.关于sql语句的拼接,因为在这里传进来的参数有两个,所以要进行sql语句的拼接的话,要提前对这两个变量进行判断是否为空或者是否只是空格。2.返回数据时不要用Resultset类型的数据,因为会被关闭,最后会无法获取。

4一些小问题


1.servlet中别忘记转码
2.request.getParameter通过标签的name属性获取
studentdao:
3.statement执行sql语句的方法要恰当
4.类名与报名不要有重复的地方
5.sql语句 string类型的变量拼接时不要忘了加上单引号。
6.servlet中用的是request.getRequestDispatcher("stu/student.jsp").forward(request, response);
然后在界面的form表单的标签中 action属性用的是相对路径 在第二次提交的时候就会找不到网页。
而采用response。sendRedirect来跳转 是不能通过request.getAttribute("username");  来得到admin的。

这是因为上个页面请求已经结束,也就是 request这个对象已经消亡了。所以也就得不到admin了

5.总结

其实这个博文早该写出来了。。但是遇到了很多事情搁置了好长时间,我也只是一个普通在校大学生,希望可以越来越好,不足之处肯定有很多的,还是请大家多多见谅,虽然我知道肯定没人看我的啦。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值