目录
实现流程和思路
项目视频演示
设置数据库studenttest
①、创建user表
创建项目
jsp页面
登录界面login.jsp
主界面mainPage.jsp
添加学生信息AddUser.jsp
修改学生信息change.jsp
Servlet下
登录LoginServlet
查找SearchServlet
删除DeleteServlet
修改ChangeServlet
添加addServlet
Service下
FileServic接口下
FileServiceImpl实现类下
pojo下
User学生类
Dao下
FileDao接口下
FileDaoImpl实现类下
utils下
JDBCUtils
创建jsp页面,然后转发到servlet下,接受jsp页面传过来的值,拿着这些值去访问service服务层接口,服务层写个实类,这个实现类去服务层的接口,然后这个服务层的实现类再去访问dao层的接口,在持久层dao中在编写一个dao层接口的实现类去实现dao层的接口,最后在dao层的的实现类去访问数据库。
学生管理系统项目演示
其中包括设置删除的u_isdelete(0是存在,1是删除)
create table user(
u_name varchar(20),
u_id int primary key auto_increment,
u_pwd varchar(20) ,
u_phone varchar(20) not null,
u_role int not null,
u_isdelete int not null
)
charset=utf8;
项目界面总览
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
登录界面
<%-- css代码--%>
登录界面
用户:
${error}
密码:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%--JSTL语法,可以使用user.getName()方法--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
主界面
名字:${user.getU_name()}
编号:${user.getU_id()}
${tip}
学生id
学生名字
学生电话
学生成绩
学生品行
<%--jstl语法遍历,var是一个指指针--%>
${item.getU_id()}
${item.getU_name()}
${item.getU_phone()}
优秀
良好
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
添加信息界面
添加学生信息
学生id:
姓名:
密码:
电话号码:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basepath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
修改界面
修改学生信息
修改的学生id:
姓名:
密码:
电话号码:
package com.StudentTest.Servlet;
import com.StudentTest.pojo.User;
import com.StudentTest.service.FileService;
import com.StudentTest.service.FileServiceImpl;
import com.StudentTest.service.LoginService;
import com.StudentTest.service.LoginServiceImpl;
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.util.ArrayList;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String u_id=null,u_pwd=null;
User u = new User();
try{//非数字异常判断
u_id = req.getParameter("u_id");
u_pwd = req.getParameter("u_pwd");
u.setU_id(Integer.valueOf(u_id));
u.setU_pwd(u_pwd);
}catch (Exception e){
req.setAttribute("error", "你输入的不是数字");
//错误就回到主界面
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
//实现登录服务层的业务逻辑层,从服务层service到持久层dao
LoginService ls = new LoginServiceImpl();
//返回user这样可以看到登录的用户是谁
/**
* 如果user为空说明账号密码不一致,跳转到登录界面
* 不为空说明账号密码一致,跳转到主界面
*/
User user = null;
try {
user = ls.loginService(u);
} catch (Exception e) {
e.printStackTrace();
}
if (user != null) {
FileService fs=new FileServiceImpl();
//查看所有数据
ArrayList arrUser=fs.getAllStudent();
//将这些数据转发到前端
req.setAttribute("arr",arrUser);
req.setAttribute("user", user);
//跳转到主界面
req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
} else {
req.setAttribute("error", "密码不匹配");
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
}
}
package com.StudentTest.Servlet;
import com.StudentTest.pojo.User;
import com.StudentTest.service.FileService;
import com.StudentTest.service.FileServiceImpl;
import com.StudentTest.service.LoginService;
import com.StudentTest.service.LoginServiceImpl;
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.util.ArrayList;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String u_id=null,u_pwd=null;
User u = new User();
try{//非数字异常判断
u_id = req.getParameter("u_id");
u_pwd = req.getParameter("u_pwd");
u.setU_id(Integer.valueOf(u_id));
u.setU_pwd(u_pwd);
}catch (Exception e){
req.setAttribute("error", "你输入的不是数字");
//错误就回到主界面
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
//实现登录服务层的业务逻辑层,从服务层service到持久层dao
LoginService ls = new LoginServiceImpl();
//返回user这样可以看到登录的用户是谁
/**
* 如果user为空说明账号密码不一致,跳转到登录界面
* 不为空说明账号密码一致,跳转到主界面
*/
User user = null;
try {
user = ls.loginService(u);
} catch (Exception e) {
e.printStackTrace();
}
if (user != null) {
FileService fs=new FileServiceImpl();
//查看所有数据
ArrayList arrUser=fs.getAllStudent();
//将这些数据转发到前端
req.setAttribute("arr",arrUser);
req.setAttribute("user", user);
//跳转到主界面
req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
} else {
req.setAttribute("error", "密码不匹配");
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
}
}
package com.StudentTest.Servlet;
import com.StudentTest.pojo.User;
import com.StudentTest.service.FileService;
import com.StudentTest.service.FileServiceImpl;
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.util.ArrayList;
@WebServlet("/deleteServlet")
public class DeleteServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String del=req.getParameter("v");
//获取登录人信息
String adm=req.getParameter("id");
//访问service层,在service层创建对应的方法
FileService fs=new FileServiceImpl();
ArrayList list=fs.getAllStudent();
//获取登录人员信息
User u = fs.getAdmint(adm);
//service层调用方法,转到dao层执行sql语句
boolean user=fs.delUser(del);
String tip="";
if(user){
tip="删除成功";
}else {
tip="删除失败";
}
//发送到前端
req.setAttribute("user", u);
req.setAttribute("arr", list);
req.setAttribute("tip", tip);
req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
}
}
package com.StudentTest.Servlet;
import com.StudentTest.pojo.User;
import com.StudentTest.service.FileService;
import com.StudentTest.service.FileServiceImpl;
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.util.ArrayList;
@WebServlet("/changeUser")
public class ChangeServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String id=req.getParameter("u_id");
String pwd=req.getParameter("u_pwd");
String phone=req.getParameter("u_phone");
String name=req.getParameter("u_name");
FileService fs=new FileServiceImpl();
User user=new User(name,Integer.valueOf(id),pwd,phone);
String adm=req.getParameter("id");
//获取登录人员信息
User u = fs.getAdmint(adm);
System.out.println(u);
boolean flag=fs.changeUser( user);
ArrayList list=fs.getAllStudent();
String tip="";
if (flag){
tip="修改成功";
}else{
tip="修改失败";
}
//发送到前端
req.setAttribute("tip",tip);
req.setAttribute("user", u);
req.setAttribute("arr", list);
req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
}
}
package com.StudentTest.Servlet;
import com.StudentTest.pojo.User;
import com.StudentTest.service.FileService;
import com.StudentTest.service.FileServiceImpl;
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.util.ArrayList;
@WebServlet("/addUser")
public class AddServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String id=req.getParameter("u_id");
String pwd=req.getParameter("u_pwd");
String phone=req.getParameter("u_phone");
String name=req.getParameter("u_name");
FileService fs=new FileServiceImpl();
User user=new User(name,Integer.valueOf(id),pwd,phone);
String adm=req.getParameter("id");
//获取登录人员信息
User u = fs.getAdmint(adm);
System.out.println(u);
boolean flag=fs.addUser( user);
ArrayList list=fs.getAllStudent();
String tip="";
if (flag){
tip="添加成功";
}else{
tip="添加失败";
}
//发送到前端
req.setAttribute("tip",tip);
req.setAttribute("user", u);
req.setAttribute("arr", list);
req.getRequestDispatcher("mainPage.jsp").forward(req, resp);
}
}
package com.StudentTest.service;
import com.StudentTest.pojo.User;
import java.util.ArrayList;
public interface FileService {
ArrayList getAllStudent();
User searchIdName(String v);
User getAdmint(String id);
boolean delUser(String del);
boolean addUser(User user);
boolean changeUser(User user);
User loginService(User u) throws Exception;
}
package com.StudentTest.service;
import com.StudentTest.Dao.FileDao;
import com.StudentTest.Dao.FileDaoImpl;
import com.StudentTest.pojo.User;
import java.util.ArrayList;
public class FileServiceImpl implements FileService {
FileDao fd= new FileDaoImpl();
@Override
public ArrayList getAllStudent() {
/**
* 访问持久层
*/
return fd.getAllStudent();
}
@Override
public User searchIdName(String v) {
return fd.searchIdName( v);
}
@Override
public User getAdmint(String id) {
return fd.getAdmint(id);
}
@Override
public boolean delUser(String del) {
return fd.delUser(del );
}
@Override
public boolean addUser(User user) {
return fd.addUser(user);
}
@Override
public boolean changeUser(User user) {
return fd.changeUser(user);
}
@Override
public User loginService(User u) throws Exception {
return fd.loginDao( u);
}
}
package com.StudentTest.pojo;
public class User {
private String u_name;
private int u_id;
private String u_pwd;
private String u_phone;
private int u_role;
private int u_isdelete;
public User() {
}
public User(String u_name, int u_id, String u_pwd, String u_phone) {
this.u_name = u_name;
this.u_id = u_id;
this.u_pwd = u_pwd;
this.u_phone = u_phone;
}
public User(String u_name, int u_id, String u_pwd, String u_phone, int u_role, int u_isdelete) {
this.u_name = u_name;
this.u_id = u_id;
this.u_pwd = u_pwd;
this.u_phone = u_phone;
this.u_role = u_role;
this.u_isdelete = u_isdelete;
}
public String getU_name() {
return u_name;
}
public void setU_name(String u_name) {
this.u_name = u_name;
}
public int getU_id() {
return u_id;
}
public void setU_id(int u_id) {
this.u_id = u_id;
}
public String getU_pwd() {
return u_pwd;
}
public void setU_pwd(String u_pwd) {
this.u_pwd = u_pwd;
}
public String getU_phone() {
return u_phone;
}
public void setU_phone(String u_phone) {
this.u_phone = u_phone;
}
public int getU_role() {
return u_role;
}
public void setU_role(int u_role) {
this.u_role = u_role;
}
public int getU_isdelete() {
return u_isdelete;
}
public void setU_isdelete(int u_isdelete) {
this.u_isdelete = u_isdelete;
}
@Override
public String toString() {
return "User{" +
"u_name='" + u_name + '\'' +
", u_id=" + u_id +
", u_pwd='" + u_pwd + '\'' +
", u_phone='" + u_phone + '\'' +
", u_role=" + u_role +
", u_isdelete=" + u_isdelete +
'}';
}
}
package com.StudentTest.Dao;
import com.StudentTest.pojo.User;
import java.util.ArrayList;
public interface FileDao {
ArrayList getAllStudent();
User searchIdName(String v);
User getAdmint(String id);
boolean delUser(String del);
boolean addUser(User user);
boolean changeUser(User user);
User loginDao(User u) throws Exception;
}
package com.StudentTest.Dao;
import com.StudentTest.pojo.User;
import com.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class FileDaoImpl implements FileDao {
@Override
public User loginDao(User u) throws SQLException, ClassNotFoundException {
/**在这写数据库的内容
* 获取链接对象,获取sql语句
*/
int id=u.getU_id();
String pwd=u.getU_pwd();
// //访问数据库
// User user=new User("慧宝",1001,"520","15216123240",1,0);
Connection connection= JDBCUtils.getConnection();
PreparedStatement pre =null;
ResultSet res =null;
String sql="select *from user where u_id="+id+" and u_pwd='"+pwd+"'";
try{
pre= connection.prepareStatement(sql);
res=pre.executeQuery();
User user=new User();
while(res.next()){
user.setU_id(res.getInt("u_id"));
user.setU_name(res.getString("u_name"));
user.setU_pwd(res.getString("u_pwd"));
user.setU_phone(res.getString("u_phone"));
user.setU_role(res.getInt("u_role"));
user.setU_isdelete(res.getInt("u_isdelete"));
return user;
}
} catch (SQLException s){
s.printStackTrace();
}
return null;
}
@Override
public ArrayList getAllStudent() {
ArrayList arr=new ArrayList<>();
Connection connection= null;
try {
connection = JDBCUtils.getConnection();
} catch (Exception e) {
}
PreparedStatement pre =null;
ResultSet res =null;
//u_role为1表示普通用户,为0为管理员,u_isdelete为0表示还在,为1表示已删除
String sql="select *from user where u_role=1 and u_isdelete=0 ";
try{
pre= connection.prepareStatement(sql);
res=pre.executeQuery();
while(res.next()){
User user=new User();
user.setU_id(res.getInt("u_id"));
user.setU_name(res.getString("u_name"));
user.setU_pwd(res.getString("u_pwd"));
user.setU_phone(res.getString("u_phone"));
user.setU_role(res.getInt("u_role"));
user.setU_isdelete(res.getInt("u_isdelete"));
arr.add(user);
}
return arr;
} catch (SQLException s){
s.printStackTrace();
}
return null;
}
@Override
public User searchIdName(String v) {
/**
* sql
*/
Connection connection= null;
try {
connection = JDBCUtils.getConnection();
} catch (Exception e) {
}
PreparedStatement pre =null;
ResultSet res =null;
//u_role为1表示普通用户,为0为管理员,u_isdelete为0表示还在,为1表示已删除
String sql="select *from user where u_id ='"+v+"'or+ u_name ='"+v+"' ";
try{
pre= connection.prepareStatement(sql);
res=pre.executeQuery();
while(res.next()){
User user=new User();
user.setU_id(res.getInt("u_id"));
user.setU_name(res.getString("u_name"));
user.setU_pwd(res.getString("u_pwd"));
user.setU_phone(res.getString("u_phone"));
user.setU_role(res.getInt("u_role"));
user.setU_isdelete(res.getInt("u_isdelete"));
return user;
}
} catch (SQLException s){
s.printStackTrace();
}
return null;
}
@Override
public User getAdmint(String id) {
Connection connection= null;
try {
connection = JDBCUtils.getConnection();
} catch (Exception e) {
}
PreparedStatement pre =null;
ResultSet res =null;
String sql="select *from user where u_id ='"+id+"'or+ u_name ='"+id+"' ";
try{
pre= connection.prepareStatement(sql);
res=pre.executeQuery();
while(res.next()){
User user=new User();
user.setU_id(res.getInt("u_id"));
user.setU_name(res.getString("u_name"));
return user;
}
} catch (SQLException s){
s.printStackTrace();
}
return null;
}
@Override
public boolean delUser(String del) {
Connection connection= null;
try {
connection = JDBCUtils.getConnection();
} catch (Exception e) {
}
PreparedStatement pre =null;
ResultSet ress=null;
int res=0;
String sql="delete from user where u_id="+del;
try{
pre= connection.prepareStatement(sql);
res=pre.executeUpdate();
if(res>=0)return true;
} catch (SQLException s){
s.printStackTrace();
}
return false;
}
@Override
public boolean addUser(User user) {
Connection connection= null;
try {
connection = JDBCUtils.getConnection();
} catch (Exception e) {
}
PreparedStatement pre =null;
ResultSet ress=null;
String name=user.getU_name();
int id=user.getU_id();
String pwd=user.getU_pwd();
String phone=user.getU_phone();
int res=0;
//"insert into user values('"+user.getU_name()+"','"+user.getU_id()+"','"+user.getU_pwd()+"','"+user.getU_phone()+"')"
String sql="insert into user(u_name,u_id,u_pwd,u_phone)\n" +
"values('"+name+"','"+id+"','"+pwd+"','"+phone+"')";
try{
pre= connection.prepareStatement(sql);
res=pre.executeUpdate();
System.out.println(res);
if(res>=0)return true;
} catch (SQLException s){
s.printStackTrace();
}
return false;
}
@Override
public boolean changeUser(User user) {
Connection connection= null;
try {
connection = JDBCUtils.getConnection();
} catch (Exception e) {
}
PreparedStatement pre =null;
ResultSet ress=null;
String name=user.getU_name();
int id=user.getU_id();
String pwd=user.getU_pwd();
String phone=user.getU_phone();
int res=0,res1=0;
String sql1="delete from user where u_id="+id;
try{
pre= connection.prepareStatement(sql1);
res=pre.executeUpdate();
if (res>0){
String sql="insert into user(u_name,u_id,u_pwd,u_phone)\n" +
"values('"+name+"','"+id+"','"+pwd+"','"+phone+"')";
pre= connection.prepareStatement(sql);
res1=pre.executeUpdate();
}
System.out.println(res);
if(res1>0)return true;
} catch (SQLException s){
s.printStackTrace();
}
return false;
}
}
package com.utils;
import java.sql.*;
public class JDBCUtils {
public static Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/studenttest?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT";
String username="root";
String password="123456";
Connection conn= DriverManager.getConnection(url,username,password);
return conn;
}
public static void release(Statement stmt,Connection conn){
if(stmt!=null){
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
stmt=null;
}
if(conn!=null){
try{
conn.close();
}catch (SQLException e){
e.printStackTrace();
}
conn=null;
}
}
public static void release(ResultSet rs,Statement stmt,Connection conn){
if(rs!=null){
try{
rs.close();
}catch (SQLException e){
e.printStackTrace();
}
rs=null;
}
release(stmt,conn);
}
}
还有写web-inf下lib下的jar包