三层组成
A.表示层(也称视图层)
前台对应于MVC中的View;后台对应于MVC中的Controller,用于控制跳转、调用业务逻辑层Servlet;
B.业务逻辑层(Service层)
接受表示层的请求;组装数据访问层,进行的操作是逻辑性的
C.数据访问层(Dao层)
直接访问数据库的操作,原子性的操作(增删改查);
三层之间的关系
上层将请求传递给下层,下层处理后,返回给上层
插入学生信息的实例
首先,在这个实例中,学生信息包括学号、姓名、年龄、地址这四个方面用户在前台输入这些信息点击提交后,向业务逻辑层发出请求,业务逻辑层通过调用数据访问层的方法,先调用查询操作,判断此学生是否在数据库已经存在;若不存在则接着调用插入操作,并把操作结果返回值表示层。
运行结果
先看一下数据库中已存在的学生信息
添加一个已经存在的学生
添加一个不存在的学生
添加成功后的数据库表信息
代码实现
项目结构
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加学生</title>
</head>
<body>
<form action="AddStudentServlet" method="post">
学号:<input type="number" name="stuNo"><br>
姓名:<input type="text" name="stuName"><br>
年龄:<input type="number" name="stuAge"><br>
地址:<input type="text" name="stuAddress"><br>
<input type="submit" value="提交"><br>
</form>
</body>
</html>
AddStudentServlet.java
package com.ajy.servlet;
import com.ajy.entity.Student;
import com.ajy.service.AddStudentService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "AddStudentServlet",value = "/AddStudentServlet")
public class AddStudentServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
int stuNo = Integer.parseInt(request.getParameter("stuNo"));
String stuName = request.getParameter("stuName");
int stuAge = Integer.parseInt(request.getParameter("stuAge"));
String stuAddress = request.getParameter("stuAddress");
Student stu = new Student(stuNo,stuName,stuAge,stuAddress);
AddStudentService addStudentService = new AddStudentService();
boolean res = addStudentService.addStudent(stu);
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
if (res){
out.print("添加成功");
}else{
out.print("添加失败");
}
}
}
AddStudentService.java
package com.ajy.service;
import com.ajy.dao.StudentDao;
import com.ajy.entity.Student;
public class AddStudentService {
StudentDao studentDao = new StudentDao();
public boolean addStudent(Student stu){
if (!studentDao.isExits(stu)){
return studentDao.addStudent(stu);
}else{
return false;
}
}
}
StudentDao.java
package com.ajy.dao;
import com.ajy.entity.Student;
import java.sql.*;
public class StudentDao {
private final String URL="jdbc:mysql://localhost:3306/anjiyubase?&serverTimezone=UTC&useSSL=false";
private final String DRIVER="com.mysql.cj.jdbc.Driver";
private final String NAME="root";
private final String PWD="121181";
public boolean isExits(Student stu){
return queryStudent(stu.getStuNo())==null?false:true;
}
public boolean addStudent(Student stu){
Connection con = null;
PreparedStatement pstmt = null;
int count = 0;
try {
Class.forName(DRIVER);
con = DriverManager.getConnection(URL,NAME,PWD);
String sql = "insert into student values(?,?,?,?)";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1,stu.getStuNo());
pstmt.setString(2,stu.getStuName());
pstmt.setInt(3,stu.getStuAge());
pstmt.setString(4,stu.getStuAddress());
count = pstmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
return false;
} catch (SQLException e) {
e.printStackTrace();
return false;
}finally {
try {
if (pstmt!=null)pstmt.close();
if (con!=null)con.close();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
if(count==0){
return false;
}else{
return true;
}
}
public Student queryStudent(int stuNo){
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Student stu = null;
int count = 0;
try {
Class.forName(DRIVER);
con = DriverManager.getConnection(URL,NAME,PWD);
String sql = "select * from student where sno=?";
pstmt = con.prepareStatement(sql);
pstmt.setInt(1,stuNo);
rs = pstmt.executeQuery();
if (rs.next()){
stu = new Student(rs.getInt("sno"),
rs.getString("sname"),
rs.getInt("sage"),
rs.getString("saddress"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
} catch (SQLException e) {
e.printStackTrace();
return null;
}finally {
try {
if (rs!=null)rs.close();
if (pstmt!=null)pstmt.close();
if (con!=null)con.close();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
return stu;
}
}
Student.java
package com.ajy.entity;
public class Student {
private int stuNo;
private String stuName;
private int stuAge;
private String stuAddress;
public Student(int stuNo,String stuName,int stuAge,String stuAddress){
this.stuNo = stuNo;
this.stuName = stuName;
this.stuAge = stuAge;
this.stuAddress = stuAddress;
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getStuAge() {
return stuAge;
}
public void setStuAge(int stuAge) {
this.stuAge = stuAge;
}
public String getStuAddress() {
return stuAddress;
}
public void setStuAddress(String stuAddress) {
this.stuAddress = stuAddress;
}
}