JSP+Servlet+JavaBean+DAO技术开发设计学生体质信息管理系统

Student.java  学生类JavaBean

package bean;

public class Student {
	private int id;
	private String name;
	private String sex;
	private int age;
	private float weight;
	private float hight;
	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 getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public float getWeight() {
		return weight;
	}
	public void setWeight(float weight) {
		this.weight = weight;
	}
	public float getHight() {
		return hight;
	}
	public void setHight(float hight) {
		this.hight = hight;
	}
	
}

DbConnect.java   数据库连接与释放资源工具

package bean;
import java.sql.*;
public class DbConnect {
	private static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private static String userName="sa";
	private static String userPwd="zqb19971023";
	private static String url="jdbc:sqlserver://localhost:1433;DatabaseName=666";
	public static Connection getDBconnect() {
		try {
			Class.forName(driverName);
			Connection con=DriverManager.getConnection(url, userName, userPwd);
			return con;
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void closeDB(Connection con,PreparedStatement pstm,ResultSet rs) {
		try {
			if(rs!=null) rs.close();
			if(pstm!=null) pstm.close();
			if(con!=null) con.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}

IStudentDAO.java   实现数据库处理的接口

package com.dao;
import java.util.List;
import bean.Student;
public interface IStudentDAO {
	public abstract Student create (Student stu) throws Exception;
	public abstract void remove(Student stu) throws Exception; 
	public abstract Student find(Student stu) throws Exception; 
	public abstract List<Student> findAll() throws Exception; 
	public abstract void update(Student stu) throws Exception; 
}

StudentDAO.java    对接口IStudentDAO的实现及其访问逻辑处理类

package com.dao;
import java.sql.*;
import java.util.List;
import java.util.ArrayList;
import bean.DbConnect;
import bean.Student;
public class StudentDAO implements IStudentDAO{
	protected static final String FIELDS_INSERT="id,name,sex,age,weight,hight";
	protected static String INSERT_SQL="insert into stu_info("+FIELDS_INSERT+")"+" values(?,?,?,?,?,?)";
	protected static String SELECT_SQL="select "+FIELDS_INSERT+" from stu_info where id=?";
	protected static String UPDATE_SQL="update stu_info set "+"id=?,name=?,sex=?,age=?,weight=?,hight=? where id=?";
	protected static String DELETE_SQL="delete from stu_info where id=?";
	public Student create (Student stu) throws Exception{
		Connection con=null;
		PreparedStatement prepStmt=null;
		ResultSet rs=null;
		try {
			con=DbConnect.getDBconnect();
			prepStmt=con.prepareStatement(INSERT_SQL);
			prepStmt.setInt(1, stu.getId());
			prepStmt.setString(2, stu.getName());
			prepStmt.setString(3, stu.getSex());
			prepStmt.setInt(4, stu.getAge());
			prepStmt.setFloat(5, stu.getWeight());
			prepStmt.setFloat(6, stu.getHight());
			prepStmt.executeUpdate();
		}catch(Exception e) {
		}finally {
			DbConnect.closeDB(con,prepStmt,rs);
		}
		return stu;
	}
	public void remove(Student stu) throws Exception{
		Connection con=null;
		PreparedStatement prepStmt=null;
		ResultSet rs=null;
		try {
			con=DbConnect.getDBconnect();
			prepStmt=con.prepareStatement(DELETE_SQL);
			prepStmt.setInt(1, stu.getId());
			prepStmt.executeUpdate();
		}catch(Exception e) {
		}finally {
			DbConnect.closeDB(con,prepStmt,rs);
		}
	}
	public Student find(Student stu) throws Exception{
		Connection con=null;
		PreparedStatement prepStmt=null;
		ResultSet rs=null;
		Student stu2=null;
		try {
			con=DbConnect.getDBconnect();
			prepStmt=con.prepareStatement(SELECT_SQL);
			prepStmt.setInt(1, stu.getId());
			rs=prepStmt.executeQuery();
			if(rs.next()) {
				stu2=new Student();
				stu2.setId(rs.getInt(1));
				stu2.setName(rs.getString(2));
				stu2.setSex(rs.getString(3));
				stu2.setAge(rs.getInt(4));
				stu2.setWeight(rs.getFloat(5));
				stu2.setHight(rs.getFloat(6));
			}
		}catch(Exception e) {
		}finally {
			DbConnect.closeDB(con,prepStmt,rs);
		}
		return stu2;
	}
	public List<Student> findAll() throws Exception{
		Connection con=null;
		PreparedStatement prepStmt=null;
		ResultSet rs=null;
		List<Student> student=new ArrayList<Student>();
		con=DbConnect.getDBconnect();
		prepStmt=con.prepareStatement("select * from stu_info");
		rs=prepStmt.executeQuery();
		while(rs.next()) {
			Student stu2=new Student();
			stu2.setId(rs.getInt(1));
			stu2.setName(rs.getString(2));
			stu2.setSex(rs.getString(3));
			stu2.setAge(rs.getInt(4));
			stu2.setWeight(rs.getFloat(5));
			stu2.setHight(rs.getFloat(6));
			student.add(stu2);
		}
		DbConnect.closeDB(con, prepStmt, rs);
		return student;
	}
	public void update(Student stu) throws Exception {
		Connection con=null;
		PreparedStatement prepStmt=null;
		ResultSet rs=null;
		try {
			con=DbConnect.getDBconnect();
			prepStmt=con.prepareStatement(UPDATE_SQL);
			prepStmt.setInt(1, stu.getId());
			prepStmt.setString(2, stu.getName());
			prepStmt.setString(3, stu.getSex());
			prepStmt.setInt(4, stu.getAge());
			prepStmt.setFloat(5, stu.getWeight());
			prepStmt.setFloat(6, stu.getHight());
			prepStmt.setFloat(7, stu.getId());
			int rowCount=prepStmt.executeUpdate();
			if(rowCount==0) {
				throw new Exception("Update Error:Student Id:"+stu.getId());
			}
		}catch(Exception e) {
		}finally {
			DbConnect.closeDB(con, prepStmt, rs);
		}
	}
}

servlet

find.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("GB2312");
		Student stu=new Student();
		stu.setId(Integer.parseInt(request.getParameter("id")));
		StudentDAO stu2=new StudentDAO();
		try {
			stu=stu2.find(stu);
			request.setAttribute("stu", stu);
			request.getRequestDispatcher("find_show.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
	}

findAll.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("GB2312");
		StudentDAO stu2=new StudentDAO();
		List<Student> list = new ArrayList<Student>();
		try {
			list=stu2.findAll();
			request.setAttribute("list", list);
			request.getRequestDispatcher("find_showall.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
	}

insert.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("GB2312");
		Student stu=new Student();
		stu.setId(Integer.parseInt(request.getParameter("id")));
		stu.setName(request.getParameter("name"));
		stu.setSex(request.getParameter("sex"));
		stu.setAge(Integer.parseInt(request.getParameter("age")));
		stu.setWeight(Float.parseFloat(request.getParameter("weight")));
		stu.setHight(Float.parseFloat(request.getParameter("hight")));
		StudentDAO stu2=new StudentDAO();
		try {
			stu2.create(stu);
			request.getRequestDispatcher("succeed.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
	}

update.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("GB2312");
		Student stu=new Student();
		stu.setId(Integer.parseInt(request.getParameter("id")));
		stu.setName(request.getParameter("name"));
		stu.setSex(request.getParameter("sex"));
		stu.setAge(Integer.parseInt(request.getParameter("age")));
		stu.setWeight(Float.parseFloat(request.getParameter("weight")));
		stu.setHight(Float.parseFloat(request.getParameter("hight")));
		StudentDAO stu2=new StudentDAO();
		try {
			stu2.update(stu);
			request.getRequestDispatcher("succeed.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
	}

delete.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("GB2312");
		Student stu=new Student();
		stu.setId(Integer.parseInt(request.getParameter("id")));
		StudentDAO stu2=new StudentDAO();
		try {
			stu2.remove(stu);
			request.getRequestDispatcher("succeed.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
	}

jsp页面

succeed.java

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>成功页面</title>
</head>
<body>
	数据库操作成功!<br>
</body>
</html>

error.java

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>出错页面</title>
</head>
<body>
	数据库操作失败!<br>
</body>
</html>

index_stu.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生身体素质信息管理系统</title>
</head>
	<frameset rows="80,*">
		<frame src="index_stu_title.jsp" srolling="no">
		<frameset cols="140,*">
			<frame src="index_stu_left.jsp" scrolling="no">
			<frame src="index_stu_right.jsp" name="right" scrolling="auto">
		</frameset>
	</frameset>
</html>

index_stu_title.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>页面标题</title>
</head>
<body>
	<center><h1>学生身体体质信息管理系统</h1></center>
</body>
</html>

index_stu_left.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>页面菜单</title>
</head>
<body>
	<br><br><br><br><br><br>
	<p><a href="insert_stu_tijiao.jsp" target="right">添加学生</a>
	<p><a href="delete_stu_tijiao.jsp" target="right">删除学生</a>
	<p><a href="find_stu_tijiao.jsp" target="right">查询学生</a>
	<p><a href="findAll" target="right">列出全部学生</a>
	<p><a href="update_stu_tijiao.jsp" target="right">修改学生</a>
</body>
</html>

index_stu_right.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>信息显示页面</title>
</head>
<body>

</body>
</html>

insert_stu_tijiao.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加信息提交页面</title>
</head>
<body>
	<form action="insert" method="post">
		<table border="0" width="238" height="252">
			<tr><td>学号</td><td><input type="text" name="id"></td></tr>
			<tr><td>姓名</td><td><input type="text" name="name"></td></tr>
			<tr><td>性别</td><td><input type="text" name="sex"></td></tr>
			<tr><td>年龄</td><td><input type="text" name="age"></td></tr>
			<tr><td>体重</td><td><input type="text" name="weight"></td></tr>
			<tr><td>身高</td><td><input type="text" name="hight"></td></tr>
			<tr align="center">
				<td colspan="2">
					<input type="submit" value="添  加"> &nbsp;&nbsp;&nbsp;
					<input type="reset" value="取  消">
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

delete_stu_tijiao.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>删除信息提页面</title>
</head>
<body>
	<form action="delete" method="post">
		<table border="0" width="238" height="252">
			<tr><td>学号</td><td><input type="text" name="id"></td></tr>
			<tr align="center">
				<td colspan="2">
					<input type="submit" value="删  除"> &nbsp;&nbsp;&nbsp;
					<input type="reset" value="取  消">
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

update_stu_tijiao.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>更新信息显示页面</title>
</head>
<body>
	<form action="update" method="post">
		<table border="0" width="238" height="252">
			<tr><td>学号</td><td><input type="text" name="id"></td></tr>
			<tr><td>姓名</td><td><input type="text" name="name"></td></tr>
			<tr><td>性别</td><td><input type="text" name="sex"></td></tr>
			<tr><td>年龄</td><td><input type="text" name="age"></td></tr>
			<tr><td>体重</td><td><input type="text" name="weight"></td></tr>
			<tr><td>身高</td><td><input type="text" name="hight"></td></tr>
			<tr align="center">
				<td colspan="2">
					<input type="submit" value="更  新"> &nbsp;&nbsp;&nbsp;
					<input type="reset" value="取  消">
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

find_stu_tijiao.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>条件查询信息页面</title>
</head>
<body>
	<form action="find" method="post">
		<table border="0" width="238" height="252">
			<tr><td>学号</td><td><input type="text" name="id"></td></tr>
			<tr align="center">
				<td colspan="2">
					<input type="submit" value="查  询"> &nbsp;&nbsp;&nbsp;
					<input type="reset" value="取  消">
				</td>
			</tr>
		</table>
	</form>
</body>
</html>

find_show.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="bean.Student" import="java.util.List" import="java.util.ArrayList"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>条件查询结果页面</title>
</head>
<body>
	<%
		Student stu=(Student)request.getAttribute("stu");
	%>
	你要查询的学生数据表共有
	<table border="2" bgcolor="ccceee" width="650">
		<tr bgcolor="CCCCCC" align="center">
			<td>学号</td><td>姓名</td><td>性别</td>
			<td>年龄</td><td>体重</td><td>身高</td>
		</tr>
		<tr align="center">
			<td><%=stu.getId() %></td>
			<td><%=stu.getName() %></td>
			<td><%=stu.getSex() %></td>
			<td><%=stu.getAge() %></td>
			<td><%=stu.getWeight() %></td>
			<td><%=stu.getHight() %></td>
		</tr> 
	</table>
</body>
</html>

find_showall.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="bean.Student" import="java.util.List" import="java.util.ArrayList"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>条件查询结果页面</title>
</head>
<body>
	<%
		List<Student> list = (List<Student>)request.getAttribute("list");
	%>
	<table border="2" bgcolor="ccceee" width="650">
		<tr bgcolor="CCCCCC" align="center">
			<td>学号</td><td>姓名</td><td>性别</td>
			<td>年龄</td><td>体重</td><td>身高</td>
		</tr>
	<%
		if(list == null || list.size() < 1){
				out.print("没有数据!");
		}else{
				// 遍历学生集合中的数据
			for(Student stu : list){
	%>
		<tr align="center">
			<td><%=stu.getId() %></td>
			<td><%=stu.getName() %></td>
			<td><%=stu.getSex() %></td>
			<td><%=stu.getAge() %></td>
			<td><%=stu.getWeight() %></td>
			<td><%=stu.getHight() %></td>
		</tr> 
	<%
			}
		}
	%>
	</table>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <servlet>
    <servlet-name>insert</servlet-name>
    <servlet-class>servlet.insert</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>insert</servlet-name>
    <url-pattern>/insert</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>delete</servlet-name>
    <servlet-class>servlet.delete</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>delete</servlet-name>
    <url-pattern>/delete</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>find</servlet-name>
    <servlet-class>servlet.find</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>find</servlet-name>
    <url-pattern>/find</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>update</servlet-name>
    <servlet-class>servlet.update</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>update</servlet-name>
    <url-pattern>/update</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>findAll</servlet-name>
    <servlet-class>servlet.findAll</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>findAll</servlet-name>
    <url-pattern>/findAll</url-pattern>
  </servlet-mapping>
</web-app>

 

刚开始的页面

  • 11
    点赞
  • 62
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值