Javaweb——一個簡單的學生管理系統(crud)

先來看效果:html

1.展現和添加頁面

2.登陸

        

        這是我學習javaEE時用原生技術寫的一個簡單的增、刪、改、查的demo,若有錯誤請你們指正!前端

須要用到的jar包:java

    

這裏紅框框住的是本身作的原生的BaseDao的jar包,下一篇文章會詳細說明。sql

CRUD:數據庫

就是對數據的一系類操做:前端頁面獲取數據,後臺處理數據,到數據庫查詢,返回數據給前端頁面。jsp

下面上代碼:ide

前端代碼:post

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.jspui

<!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放在碼雲上,以供你們參考。

相關文章
相關標籤/搜索