Javaweb——一个简单的学生管理系统(crud)

先来看效果:

1.展示和添加页面

2.登录

        

        这是我学习javaEE时用原生技术写的一个简单的增、删、改、查的demo,如有错误请大家指正!

需要用到的jar包:

    

这里红框框住的是自己做的原生的BaseDao的jar包,下一篇文章会详细说明。

CRUD:

7d9c056591e557a0975924964531f60d1f4.jpg

就是对数据的一系类操作:前端页面获取数据,后台处理数据,到数据库查询,返回数据给前端页面。

下面上代码:

前端代码:

index.jsp

<!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>源代码学生信息管理</title>
</head>
<style>
.row1 {
	background-color: #E0FFFF
}

.row2 {
	background-color: #7FFFD4
}
</style>
<body>
	<table border="1" cellpadding="0" align="center" cellspacing="0"
		width="600px" height="250px" style="margin: 0 auto">
		<caption>
			<font size="10px" color="#00BFFF">学生信息管理</font>(<a
				href="addStudent.jsp">添加学生</a>)
		</caption>
		<tr>
			<th>编号</th>
			<th>姓名</th>
			<th>年龄</th>
			<th>性别</th>
			<th>专业</th>
			<th>成绩</th>
			<th>操作</th>
		</tr>
		<c:forEach items="${listStudent }" var="student" varStatus="i">
			<tr class="row${i.index%2+1 }" align="center">
				<td>${student.id }</td>
				<td>${student.name }</td>
				<td>${student.age }</td>
				<td>${student.sex }</td>
				<td>${student.major }</td>
				<td>${student.grade }</td>
				<td><a href="delete.do?id=${student.id }"
					onclick="return confirm('是否确认删除${student.name }的信息?')"><font
						size="4px" color="#FF0000">删除</font></a>&nbsp;&nbsp;&nbsp;<a
					href="load.do?id=${student.id }"><font size="4px"
						color="#1E90FF">修改</font></a></td>
			</tr>
		</c:forEach>
	</table>
	<div style="width: 300px; margin: 0 auto">
		<c:choose>
			<c:when test="${page.pageNum<=1 }">
				<a href="list.do?pageNum=1">上一页</a>
			</c:when>
			<c:otherwise>
				<a href="list.do?pageNum=${page.pageNum-1 }">上一页</a>
			</c:otherwise>
		</c:choose>
		<c:forEach var="i" begin="1" end="${page.pageTotal }" step="1">
			<a href="list.do?pageNum=${i }">${i }</a>
		</c:forEach>
		<c:choose>
			<c:when test="${page.pageNum>=page.pageTotal }">
				<a href="list.do?pageNum=${page.pageTotal} ">下一页</a>
			</c:when>
			<c:otherwise>
				<a href="list.do?pageNum=${page.pageNum+1 }">下一页</a>
			</c:otherwise>
		</c:choose>
		<span style="margin-left: 10px">一共${page.pageTotal }页&nbsp;${page.recordsNum }条信息</span>
	</div>
</body>
</html>

listStudent.jsp

<!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>源代码学生信息管理</title>
</head>
<style>
.row1 {
	background-color: #E0FFFF
}

.row2 {
	background-color: #7FFFD4
}
</style>
<body>
	<table border="1" cellpadding="0" align="center" cellspacing="0"
		width="600px" height="250px" style="margin: 0 auto">
		<caption>
			<font size="10px" color="#00BFFF">学生信息管理</font>(<a
				href="addStudent.jsp">添加学生</a>)
		</caption>
		<tr>
			<th>编号</th>
			<th>姓名</th>
			<th>年龄</th>
			<th>性别</th>
			<th>专业</th>
			<th>成绩</th>
			<th>操作</th>
		</tr>
		<c:forEach items="${listStudent }" var="student" varStatus="i">
			<tr class="row${i.index%2+1 }" align="center">
				<td>${student.id }</td>
				<td>${student.name }</td>
				<td>${student.age }</td>
				<td>${student.sex }</td>
				<td>${student.major }</td>
				<td>${student.grade }</td>
				<td><a href="delete.do?id=${student.id }"
					onclick="return confirm('是否确认删除${student.name }的信息?')"><font
						size="4px" color="#FF0000">删除</font></a>&nbsp;&nbsp;&nbsp;<a
					href="load.do?id=${student.id }"><font size="4px"
						color="#1E90FF">修改</font></a></td>
			</tr>
		</c:forEach>
	</table>
	<div style="width: 300px; margin: 0 auto">
		<c:choose>
			<c:when test="${page.pageNum<=1 }">
				<a href="list.do?pageNum=1">上一页</a>
			</c:when>
			<c:otherwise>
				<a href="list.do?pageNum=${page.pageNum-1 }">上一页</a>
			</c:otherwise>
		</c:choose>
		<c:forEach var="i" begin="1" end="${page.pageTotal }" step="1">
			<a href="list.do?pageNum=${i }">${i }</a>
		</c:forEach>
		<c:choose>
			<c:when test="${page.pageNum>=page.pageTotal }">
				<a href="list.do?pageNum=${page.pageTotal} ">下一页</a>
			</c:when>
			<c:otherwise>
				<a href="list.do?pageNum=${page.pageNum+1 }">下一页</a>
			</c:otherwise>
		</c:choose>
		<span style="margin-left: 10px">一共${page.pageTotal }页&nbsp;${page.recordsNum }条信息</span>
	</div>
</body>
</html>

addStudent.jsp

<!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>添加学生信息</title>
</head>
<body>
	<h3>添加学生信息</h3>
	<form method="post" action="add.do">
		姓名:<input type="text" name="name" /> 年龄:<input type="text" name="age" />
		性别:<input type="text" name="sex" /> 专业:<input type="text" name="major" />
		成绩:<input type="text" name="grade" /> <input type="submit" name="增加"
			value="添加学生" onclick="alert('添加成功!')" />
	</form>
</body>
</html>

后台代码:

Student.java

public class Student {
	@Id
	private Integer id;
	private String name;
	private Integer age;
	private String sex;
	private String major;
	private String grade;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getMajor() {
		return major;
	}
	public void setMajor(String major) {
		this.major = major;
	}
	public String getGrade() {
		return grade;
	}
	public void setGrade(String grade) {
		this.grade = grade;
	}
	
	public Student() {
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", major=" + major
				+ ", grade=" + grade + "]";
	}
	
}

IStudentDao.java

public interface IStudentDao extends IBaseDao<Student> {
	int getRecordsNum();
}

StudentDaoImpl.java

public class StudentDaoImpl extends BaseDaoImpl<Student> implements IStudentDao{

	public StudentDaoImpl() {
		super(Student.class);
	}

	@Override
	public int getRecordsNum() {
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		int recordsNum=0;
		try {
			con=JDBCUtil.intstance.getConn();
			String sql="select count(*) count from student";
			ps=con.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				recordsNum=rs.getInt("count");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				JDBCUtil.intstance.close(con, ps, null);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return recordsNum;
	}
	public List<Student> findByPageNum(Page page){
		int begin=(page.getPageNum()-1)*page.getPageSize();
		List<Student> sudentList=new ArrayList<Student>();
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			con=JDBCUtil.intstance.getConn();
			String sql="select * from student limit ?,?";
			ps=con.prepareStatement(sql);
			ps.setInt(1, begin);
			ps.setInt(2, page.getPageSize());
			rs=ps.executeQuery();
			while(rs.next()){
				Student student = new Student();
				student.setId(rs.getInt("id"));
				student.setName(rs.getString("name"));
				student.setAge(rs.getInt("age"));
				student.setSex(rs.getString("sex"));
				student.setMajor(rs.getString("major"));
				student.setGrade(rs.getString("grade"));
				sudentList.add(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				JDBCUtil.intstance.close(con, ps, rs);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return sudentList;
	}
}

ControllerServlet.java

@WebServlet("*.do")
public class ControllerServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;
	private StudentDaoImpl sd = new StudentDaoImpl();

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String url=request.getRequestURI();
		String action=request.getRequestURI().substring(url.lastIndexOf('/')+1, url.lastIndexOf('.'));
		if("list".equals(action)){
			Page page=new Page();
			String pageNum=request.getParameter("pageNum");
			if(pageNum==null){
				page.setPageNum(1);
			}else{
				page.setPageNum(Integer.parseInt(pageNum));
			}	
			List<Student> listStudent=sd.findByPageNum(page);
			request.setAttribute("listStudent", listStudent);
			request.setAttribute("page", page);
			request.getRequestDispatcher("listStudent.jsp").forward(request, response);
		}
		else if("add".equals(action)){
			Student student = new Student();
			student.setName(request.getParameter("name"));
			student.setAge(Integer.parseInt(request.getParameter("age")));
			student.setSex(request.getParameter("sex"));
			student.setMajor(request.getParameter("major"));
			student.setGrade(request.getParameter("grade"));
			sd.add(student);
			response.sendRedirect("list.do");
		}else if("delete".equals(action)){
			int id=Integer.parseInt(request.getParameter("id"));
			sd.delect(id);
			response.sendRedirect("list.do");
		}else if("load".equals(action)){
			Student student = sd.queryOne(Integer.parseInt(request.getParameter("id")));
			request.setAttribute("student",student);
			request.getRequestDispatcher("studentInfo.jsp").forward(request, response);
		}else if("update".equals(action)){
			Student student = new Student();
			student.setId(Integer.parseInt(request.getParameter("id")));
			student.setName(request.getParameter("name"));
			student.setAge(Integer.parseInt(request.getParameter("age")));
			student.setSex(request.getParameter("sex"));
			student.setMajor(request.getParameter("major"));
			student.setGrade(request.getParameter("grade"));
			sd.updata(student);
			response.sendRedirect("list.do");
		}
	}
}

以上就是关键代码,后面我会将这个demo放在码云上,以供大家参考。

转载于:https://my.oschina.net/u/3975109/blog/2253235

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值