JSP三层架构开发学生管理系统源码-dao、entity、service、servlet

表示层:MVC中的视图层和控制器层,前端通常为html css js jsp文件,后端例如Servlet文件,调用业务逻辑层业务逻辑层(Service):接受表示层的请求,先进行逻辑处理,后调用数据逻辑层,(增:查+增)

数据访问层(Dao):直接访问数据库的操作

JSP三层架构开发学生管理系统源码-dao、entity、service、servlet

通过三层架构,将表示层请求传递到数据访问层,数据访问层将结果返回至表示层

具体流程

JSP三层架构开发学生管理系统源码-dao、entity、service、servlet

开发环境:IDEA Mysql  8.0.19   mysql driver:8.0.15  servlet:3.0以上版本
代码结构(正规来说的话service 和 dao 需要接口,为了代码看着方便省略了)

JSP三层架构开发学生管理系统源码-dao、entity、service、servlet

add.jsp

JSP三层架构开发学生管理系统源码-dao、entity、service、servlet

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>add</title>
</head>
<body>
<form action="addStudentServlet" >
学号:<input type="number" name="sno"><br/>
name: <input type="text" name="sname"/><br/>
age: <input type="text" name="sage"/><br/>
address: <input type="text" name="saddress"/><br/>
<input type="submit" value="add"/><br/>
</form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>add</title> </head> <body> <form action="addStudentServlet" > 学号:<input type="number" name="sno"><br/> name: <input type="text" name="sname"/><br/> age: <input type="text" name="sage"/><br/> address: <input type="text" name="saddress"/><br/> <input type="submit" value="add"/><br/> </form> </body> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>add</title>
</head>
<body>
<form action="addStudentServlet" >
学号:<input type="number" name="sno"><br/>
name: <input type="text" name="sname"/><br/>
age: <input type="text" name="sage"/><br/>
address: <input type="text" name="saddress"/><br/>
<input type="submit" value="add"/><br/>
</form>
</body>
</html>

写后端代码前,先将数据封装Java bean(entity包中)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.Student.entity;
public class Student {
private int sno;
private String sname;
private int sage;
private String 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(String sname, int sage, String saddress) {
this.sname = sname;
this.sage = sage;
this.saddress = saddress;
}
@Override
public String toString() {
return "Student{" +
"sno=" + sno +
", sname='" + sname + '\'' +
", sage=" + sage +
", 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;
}
}
package com.Student.entity; public class Student { private int sno; private String sname; private int sage; private String 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(String sname, int sage, String saddress) { this.sname = sname; this.sage = sage; this.saddress = saddress; } @Override public String toString() { return "Student{" + "sno=" + sno + ", sname='" + sname + '\'' + ", sage=" + sage + ", 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; } }
package com.Student.entity;

public class Student {
private int sno;
private String sname;
private int sage;
private String 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(String sname, int sage, String saddress) {
this.sname = sname;
this.sage = sage;
this.saddress = saddress;
}

@Override
public String toString() {
return "Student{" +
"sno=" + sno +
", sname='" + sname + '\'' +
", sage=" + sage +
", 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(com.student.servlet包中)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.Student.Servlet;
import com.Student.entity.Student;
import com.Student.service.StudentService;
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("/addStudentServlet")
public class addStudentServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// response.setCharacterEncoding("utf-8"); // 指定输出到客户端的是编码格式
response.setContentType("text/html;charset=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);
PrintWriter out = response.getWriter();
if(result){
out.println("add successfully");
}else{
out.println("fail to add");
}
/*if (!result) { //如果增加失败,给request放入一条数据error
request.setAttribute("error", "error");
}
request.getRequestDispatcher("queryAllStudentServlet").forward(request, response);*/
}
}
package com.Student.Servlet; import com.Student.entity.Student; import com.Student.service.StudentService; 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("/addStudentServlet") public class addStudentServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // response.setCharacterEncoding("utf-8"); // 指定输出到客户端的是编码格式 response.setContentType("text/html;charset=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); PrintWriter out = response.getWriter(); if(result){ out.println("add successfully"); }else{ out.println("fail to add"); } /*if (!result) { //如果增加失败,给request放入一条数据error request.setAttribute("error", "error"); } request.getRequestDispatcher("queryAllStudentServlet").forward(request, response);*/ } }
package com.Student.Servlet;

import com.Student.entity.Student;
import com.Student.service.StudentService;

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("/addStudentServlet")
public class addStudentServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// response.setCharacterEncoding("utf-8"); // 指定输出到客户端的是编码格式
response.setContentType("text/html;charset=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);
PrintWriter out = response.getWriter();
if(result){
out.println("add successfully");
}else{
out.println("fail to add");
}
/*if (!result) { //如果增加失败,给request放入一条数据error
request.setAttribute("error", "error");
}
request.getRequestDispatcher("queryAllStudentServlet").forward(request, response);*/
}
}

addStudentService.java(自行匹配放包中)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.Student.service;
import com.Student.Dao.StudentDao;
import com.Student.entity.Student;
import java.util.List;
//业务逻辑层:逻辑性的增删改查(增:查+增), 对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;
}
}
public boolean deleteStudent(int sno){
if(studentDao.isExist(sno)){
return studentDao.deleteStudentBySno(sno);
}else{
return false;
}
}
public boolean updateStudentBySno(int sno,Student student){
if(studentDao.isExist(sno)){
return studentDao.updateStudentBySno(sno, student);
}else{
return false;
}
}
//根据学号查询一个学生
public Student queryStudentBySno(int sno){
return studentDao.queryStudentBySno(sno);
}
public List<Student> queryAllStudent(){
return studentDao.queryAllStudent();
}
}
package com.Student.service; import com.Student.Dao.StudentDao; import com.Student.entity.Student; import java.util.List; //业务逻辑层:逻辑性的增删改查(增:查+增), 对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; } } public boolean deleteStudent(int sno){ if(studentDao.isExist(sno)){ return studentDao.deleteStudentBySno(sno); }else{ return false; } } public boolean updateStudentBySno(int sno,Student student){ if(studentDao.isExist(sno)){ return studentDao.updateStudentBySno(sno, student); }else{ return false; } } //根据学号查询一个学生 public Student queryStudentBySno(int sno){ return studentDao.queryStudentBySno(sno); } public List<Student> queryAllStudent(){ return studentDao.queryAllStudent(); } }
package com.Student.service;

import com.Student.Dao.StudentDao;
import com.Student.entity.Student;

import java.util.List;

//业务逻辑层:逻辑性的增删改查(增:查+增), 对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;
}
}

public boolean deleteStudent(int sno){
if(studentDao.isExist(sno)){
return studentDao.deleteStudentBySno(sno);
}else{
return false;
}
}

public boolean updateStudentBySno(int sno,Student student){
if(studentDao.isExist(sno)){
return studentDao.updateStudentBySno(sno, student);
}else{
return false;
}
}

//根据学号查询一个学生
public Student queryStudentBySno(int sno){
return studentDao.queryStudentBySno(sno);
}

public List<Student> queryAllStudent(){
return studentDao.queryAllStudent();
}
}

StudentDao.java

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.Student.Dao;
import com.Student.entity.Student;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
//数据访问层,原子性的增删改查,不可拆,
public class StudentDao {
private final String url = "jdbc:mysql://localhost/Student";
private final String User = "root";
private final String pwd = "123456";
public boolean isExist(int sno) { //true :此人存在 false : 此人不存在
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, User, pwd);
System.out.println("The database was connected successfully");
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();
System.out.println("count = "+count);
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();
}
}
}
}
package com.Student.Dao; import com.Student.entity.Student; import java.sql.*; import java.util.ArrayList; import java.util.List; //数据访问层,原子性的增删改查,不可拆, public class StudentDao { private final String url = "jdbc:mysql://localhost/Student"; private final String User = "root"; private final String pwd = "123456"; public boolean isExist(int sno) { //true :此人存在 false : 此人不存在 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, User, pwd); System.out.println("The database was connected successfully"); 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(); System.out.println("count = "+count); 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(); } } } }
package com.Student.Dao;

import com.Student.entity.Student;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
//数据访问层,原子性的增删改查,不可拆,
public class StudentDao {
private final String url = "jdbc:mysql://localhost/Student";
private final String User = "root";
private final String pwd = "123456";

public boolean isExist(int sno) { //true :此人存在 false : 此人不存在
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, User, pwd);
System.out.println("The database was connected successfully");
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();
System.out.println("count = "+count);
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();
}
}

}
}

JSP三层架构开发学生管理系统源码-dao、entity、service、servlet JSP三层架构开发学生管理系统源码-dao、entity、service、servlet

125jz网原创文章。发布者:江山如画,转载请注明出处:http://www.125jz.com/9551.html

(0)
江山如画的头像江山如画管理团队
上一篇 2021年5月1日 下午5:12
下一篇 2021年5月1日 下午5:39

99%的人还看了以下文章

  • Java常见面试题200+,学习、面试必备

    本套Java面试题,选取了企业面试最常问到的问题,可以做为Java工程师的面试宝典,也可以做为想要不断完善和扩充自己 java 技术的学习者。 主要包含: Java 基础、容器、多线程、反射、对象拷贝、Java Web 模块、异常、网络、设计模式、Spring/Spring MVC、Spring Boot/Spring Cloud、Hibernate、Myb…

    2019年8月29日
    4.5K0
  • python 期末复习-综合练习题

    十八、分别使用for循环和while循环求1+2+3+4+……+856的和 要求: 1、新建一个“sum.py”文件。 2、编写程序。 3、调试程序。 4、排除错误。 十九、输入一个数,判断奇数偶数 要求: 1、新建一个“number.py”文件 2、编写程序。 3、调试程序。 4、排除错误。 二十、分别使用for循环和while循环输出1到100之间的偶数…

    2023年6月15日
    2.0K0
  • 跟永哥学MVC:jsp+javabean+servlet实现求圆的面积

    上一节课我们通过一个案例三种实现,教你理解Jsp、javabean、Servlet(精),今天我们仍然通过同一个案例求圆的面积,使用MVC:jsp+javabean+servlet来实现,深入理解MVC及Jsp、javabean、Servlet的分工和使用。 r5.jsp <form action=”servletCircle” Method=”pos…

    2018年2月22日
    4.6K0
  • 输入python显示不是内部命令的原因及解决方法

    问题原因:没有将python的安装路径添加到环境变量中。 解决方法: 首先在桌面上右键点击“此电脑”,选择“属性”,弹出系统界面选择“高级系统设置”,进入系统属性界面后在“高级”选项中选中“环境变量”。 然后在“系统变量”中找到变量Path,双击Path变量进入编辑界面。 接着在编辑环境变量对话框中点击“新建”,添加Python的安装路径,之后一直点确定即可…

    2023年5月18日 编程开发
    1.7K1
  • 单元测试步骤、单元测试策略,单元测试快速入门教程三

    工作性质不同决定了工作侧重点也不同,因此程序开发人员在单元测试过程中关注更多的是程序代码本身和已经实现的功能。因此,站在他们的角度看,单元测试的过程就是在编写测试方法之前: 首先考虑如何对方法进行测试; 然后编写测试代码; 下一步就是运行某个测试,或者同时运行该单元的所有测试,确保所有测试都通过。 下图从宏观的角度概括了单元测试的工作过程图。 1.单元测试进…

    2018年4月18日
    4.5K0
  • java WEB编程技术上机练习一:

    jsp运行环境的搭建(jdk+TOMCAT)、编写JSP页面,配置虚拟目录并访问、熟悉MYECLIPSE的使用,包括项目的部署及运行。

    2018年8月29日
    5.6K1

发表回复

登录后才能评论