MVC与三层架构的联系及三层架构实现学生注册功能

MVC与三层架构的联系及三层架构实现学生注册功能


三层架构的逻辑关系:

在这里插入图片描述


MVC和三层架构的联系:

在这里插入图片描述


三层架构实现学生注册功能的逻辑:

在这里插入图片描述

学生注册页面:

在这里插入图片描述

输入学生信息:
在这里插入图片描述

完成学生注册:

在这里插入图片描述

代码实现:
在这里插入图片描述

add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>
<body background="photo/海浪.jpg"> 
	
	<form action="AddStudentServlet" method="post" >
		学号:<input type="text" name="sno" /><br/>
		姓名:<input type="text" name="sname" /><br/>
		年龄:<input type="text" name="sage" /><br/>
		地址:<input type="text" name="saddress" /><br/>
		<input type="submit" value="注册" />
	</form>
</body>
</html>

Student.java

package org.student.entity;

/**
 * @author 11441
 *student实体类
 */
public class Student {
	private int sno;
	private String sname;
	private int sage;
	private String saddress;
	
	
	public Student(String sname, int sage, String saddress) {
		this.sname = sname;
		this.sage = sage;
		this.saddress = saddress;
	}
	
	public Student(int sno, String sname, int sage, String saddress) {
		this.sno = sno;
		this.sname = sname;
		this.sage = sage;
		this.saddress = saddress;
	}
	
	public Student() {
		
	}
	
	public Student(int sno, String sname, int sage) {
		this.sno = sno;
		this.sname = sname;
		this.sage = sage;
	}
	
	public Student(int sage, String saddress) {
		this.sage = sage;
		this.saddress = saddress;
	}
	public int getSno() {
		return sno;
	}
	public void setSno(int sno) {
		this.sno = sno;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public int getSage() {
		return sage;
	}
	public void setSage(int sage) {
		this.sage = sage;
	}
	public String getSaddress() {
		return saddress;
	}
	public void setSaddress(String saddress) {
		this.saddress = saddress;
	}
	
}

AddStudentServlet.java

package org.student.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.student.entity.Student;
import org.student.service.StudentService;


public class AddStudentServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		
		int no = Integer.parseInt(request.getParameter("sno"));
		String name = request.getParameter("sname");
		int age = Integer.parseInt(request.getParameter("sage")) ;
		String address = request.getParameter("saddress");
		Student student = new Student(no,name,age,address);
		

		StudentService studentService = new StudentService();
		boolean result = studentService.addStudent(student);
		//jsp内置对象:out request response session application..
		//out :PrintWriter out = response.getWriter();
		//session : request.getSession();
		//application : request.getServletContext();
		
		//设置编码
		//必须在out之前设置编码
		response.setContentType("text/html; charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		if(result) {
			out.println("增加成功!!");
		}else {
			out.println("增加失败!!");
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

StudentService.java

package org.student.service;

import org.student.dao.StudentDao;
import org.student.entity.Student;

/**
 * @author 11441
 *业务逻辑层,逻辑性的增删改查,(增:查+增)到Dao层进行的组装
 */
public class StudentService {
	StudentDao studentDao = new StudentDao();
	public boolean addStudent(Student student) {
		if(!studentDao.isExist(student.getSno())) {
			//学生不存在,增加该学生
			studentDao.addStudent(student);
			return true;
		}else {
			System.out.println("该学生已存在");
			return false;
		}
	}
}

StudentDao.java

package org.student.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.student.entity.Student;

/**
 * @author 11441
 *数据访问层:原子性,具体的 增删改查
 */
public class StudentDao {
	
	private final String URL = "jdbc:mysql://localhost:3306/test1";
	private final String USERNAME = "root";
	private final String PASSWOED = "root";
	
	//判断学生存在
	public boolean isExist(int sno) {
		return queryStudentBySno(sno)==null ? false : true;
	}
	
	//增加
	public boolean addStudent(Student student) {
		//封装到数据类
		Connection connection = null;
		PreparedStatement pstmt = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection(URL, USERNAME, PASSWOED);
			String sql = "insert into student values (?,?,?,?)";
			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, student.getSno());
			pstmt.setString(2, student.getSname());
			pstmt.setInt(3, student.getSage());
			pstmt.setString(4, student.getSaddress());
			int count = pstmt.executeUpdate();
			if(count > 0) {
				return true;
			}else {
				return false;
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if(pstmt != null) pstmt.close();
				if(connection != null) connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	//根据学号查询学生信息
	public Student queryStudentBySno(int sno) {
		Student student = null;
		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection(URL, USERNAME, PASSWOED);
			String sql = "select * from student where sno = ?";
			pstmt = connection.prepareStatement(sql);
			pstmt.setInt(1, sno);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				int no = rs.getInt("sno");
				String name = rs.getString("sname");
				int age = rs.getInt("sage");
				String address = rs.getString("saddress");
				student = new Student(sno,name,age,address);
			}
			return student;
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				if(rs != null) rs.close();
				if(pstmt != null) pstmt.close();
				if(connection != null) connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值