基于java+mysql+JDBC+tomcat+Servlet+JSP+js的学生管理系统
Posted 执久呀
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于java+mysql+JDBC+tomcat+Servlet+JSP+js的学生管理系统相关的知识,希望对你有一定的参考价值。
目录
实现流程和思路
创建jsp页面,然后转发到servlet下,接受jsp页面传过来的值,拿着这些值去访问service服务层接口,服务层写个实类,这个实现类去服务层的接口,然后这个服务层的实现类再去访问dao层的接口,在持久层dao中在编写一个dao层接口的实现类去实现dao层的接口,最后在dao层的的实现类去访问数据库。
图示:
项目视频演示
学生管理系统项目演示
设置数据库studenttest
①、创建user表
其中包括设置删除的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;
创建项目
项目界面总览
jsp页面
登录界面login.jsp
<%@ 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 + "/";
%>
<html>
<head>
<base href="<%=basepath %>"/>
<meta charset="utf-8"/>
<title>登录界面</title>
<%-- css代码--%>
<style>
#fame_
width: 500px;
height: 400px;
margin-left: 600px;
margin-top: 200px;
background-color: aqua;
/*边框黑色 实线 2像素*/
border: black solid 2px;
#inner_
margin-left: 140px;
margin-top: 160px;
</style>
</head>
<body>
<h1 align="center" style="color: red">登录界面</h1>
<div id="fame_" >
<div id="inner_">
用户:<input type="text" id="u_id">
<b> <span id="span1" style="color: crimson">$error</span></b><br><br>
密码:<input type="password" id="u_pwd">
<b> <span id="span2" style="color: crimson"></span></b><br><br>
<button id="btn01">重置</button>
<button id="btn02" style="margin-left: 20px">登录</button><br>
<script>
<%-- 这里写js代码验证账号密码值是否为空--%>
window.onload=function()
document.getElementById("btn02").onclick = function ()
let id_value = document.getElementById("u_id").value
if (id_value == null || id_value == "")
document.getElementById("span1").innerHTML = "用户名不能为空"
return;
//判断密码是否为空
let pwd = document.getElementById("u_pwd").value
if (pwd == null || pwd == "")
document.getElementById("span2").innerHTML = "密码不能为空"
return;
//跳转到servlet
window.location.href = "login?u_id=" + id_value + "&u_pwd=" + pwd;
//在次点击清空错误信息
document.getElementById("u_id").onfocus = function ()
document.getElementById("span1").innerText = ""
document.getElementById("u_pwd").onfocus = function ()
document.getElementById("span2").innerText = ""
//清空内容
document.getElementById("btn01").onclick=function ()
document.getElementById("u_id").value="";
document.getElementById("u_pwd").value="";
</script>
</div>
</div>
</body>
</html>
主界面mainPage.jsp
<%@ 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" %>
<html>
<head>
<title>主界面</title>
<style>
#head_
height: 100px;
width: 100%;
background-color: darkcyan;
#personFile
width: 120px;
height: 50px;
padding-top: 25px;
margin-left: 90%;
#search
height: 70px;
width: 100%;
background-color: aqua;
#inner_s
/*width: 200px;
height: 40px;
padding-right: 70%;
padding-top: 50%;*/
width: 30%;
padding-top:25px;
padding-left: 45%;
#table
margin-left: 30%;
margin-top: 30px;
td
text-align: center;
height: 20px;
width: 150px;
border: darkcyan 2px solid;
padding:6px;
</style>
</head>
<body>
<div id="head_">
<h2>
<div id="personFile" >
名字:<span style="color: red">$user.getU_name()</span><br>
编号:<span style="color: red">$user.getU_id()</span>
</div>
</h2>
</div>
<div id="search">
<div id="inner_s">
<input type="text" style="font-size: 20px; height: 26px;width: 190px " id="fileId">
<button style="font-size: 18px; height: 28px;" id="cx"margin-left: 30px>查询</button>
<button style="font-size: 18px; height: 28px;" id="addUser" margin-left: 100px>添加</button>
<button style="font-size: 18px; height: 28px;" id="deleteUser">删除</button>
<button style="font-size: 18px; height: 28px;" id="changeUser">修改</button>
<b> <span style="color: crimson">$tip</span></b>
</div>
</div>
<script>
//提交
document.getElementById("cx").onclick=function()
let v = document.getElementById('fileId').value;
//访问服务器searchServlet
window.location.href = "searchServlet?v=" + v+"&id=$user.getU_name()";
//删除
document.getElementById("deleteUser").onclick=function ()
let v =document.getElementById('fileId').value;
window.location.href = "deleteServlet?v="+v+"&id=$user.getU_name()";
// 添加跳转到AddUser.jsp界面
document.getElementById("addUser").onclick=function ()
window.location = "AddUser.jsp";
//修改,跳转到change.jsp
document.getElementById("changeUser").onclick=function ()
let v =document.getElementById('fileId').value;
window.location.href = "change.jsp";
</script>
<div>
<table id="table" style="height: 30px;width: 700px;border: black 1px solid;border-collapse:collapse;">
<tr style="font-weight: bold" >
<td>学生id</td>
<td>学生名字</td>
<td>学生电话</td>
<td>学生成绩</td>
<td>学生品行</td>
</tr>
<%--jstl语法遍历,var是一个指指针--%>
<c:forEach items="$arr" var="item">
<tr>
<td>$item.getU_id()</td>
<td>$item.getU_name()</td>
<td>$item.getU_phone()</td>
<td style="color: deeppink"> 优秀</td>
<td style="color: red">良好</td>
</td>
</tr>
</c:forEach>
</table>
</div>
<div>
</div>
</body>
</html>
添加学生信息AddUser.jsp
<%@ 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" %>
<html>
<head>
<base href="<%=basepath %>"/>
<meta charset="utf-8"/>
<title>添加信息界面</title>
</head>
<style>
#div1
width: 400px;
height: 300px;
margin-left: 600px;
margin-top: 200px;
background-color:deepskyblue;
/*边框黑色 实线 2像素*/
border: black solid 2px;
</style>
<body>
<h2 align="center" >添加学生信息</h2><br><br>
<div id="div1" >
<tr> 学生id:<input type="text" id="u_id" >
<span id="span01" style="color: red"></span> <br><br></tr>
<tr> 姓名:<input type="text" id="u_name" ><br><br></tr>
<tr> 密码:<input type="password" id="u_pwd"><br><br></tr>
<tr> 电话号码:<input type="text" id="u_phone"><br><br></tr>
<button id="btn3">提交</button>
<script>
document.getElementById("btn3").onclick=function()
//获取填写的数据
let u_id=document.getElementById("u_id").value;
let u_name=document.getElementById("u_name").value;
let u_pwd=document.getElementById("u_pwd").value;
let u_phone=document.getElementById("u_phone").value;
if(u_id==null||u_id==""||u_name==null||u_name=="")
document.getElementById("span01").innerText="id或名字为为空"
else
//把数据发送到后端,发送到servlet的addUser路径下
window.location.href="addUser?u_id="+u_id+"&u_pwd="+u_pwd+"&u_name="+u_name+"&u_phone="+u_phone+"&id=$user.getU_name()";
document.getElementById("u_id").onfocus=function ()
document.getElementById("span01").innerText=""
</script>
</div>
</body>
</html>
修改学生信息change.jsp
<%@ 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 + "/";
%>
<html>
<head>
<base href="<%=basepath %>"/>
<meta charset="utf-8"/>
<title>修改界面</title>
</head>
<style>
#div1
width: 400px;
height: 300px;
margin-left: 600px;
margin-top: 200px;
background-color:deepskyblue;
/*边框黑色 实线 2像素*/
border: black solid 2px;
</style>
<body>
<h2 align="center" >修改学生信息</h2><br><br>
<div id="div1" >
修改的学生id:<input type="text" id="u_id" >
<span id="span01" style="color: red"></span> <br><br>
姓名:<input type="text" id="u_name" ><br><br>
密码:<input type="password" id="u_pwd"><br><br>
电话号码:<input type="text" id="u_phone"><br><br>
<button id="btn3">提交</button>
<script>
//获取填写数据
document.getElementById("btn3").onclick=function()
let u_id=document.getElementById("u_id").value;
let u_name=document.getElementById("u_name").value;
let u_pwd=document.getElementById("u_pwd").value;
let u_phone=document.getElementById("u_phone").value;
//判断非空
if(u_id==null||u_id==""||u_name==null||u_name=="")
document.getElementById("span01").innerText="id或名字为为空"
else
//不为空就转发到后端
window.location.href="changeUser?u_id="+u_id+"&u_pwd="+u_pwd+"&u_name="+u_name+"&u_phone="+u_phone+"&id=$user.getU_name()";
//再次点击清除错误信息提示
document.getElementById("u_id").onfocus=function ()
document.getElementById("span01").innerText=""
</script>
</div>
</body>
</html>
Servlet下
登录LoginServlet
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<User> 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);
查找SearchServlet
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<User> 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);
删除DeleteServlet
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<User> 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);
修改ChangeServlet
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<User> 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);
添加addServlet
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<User> 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);
Service下
FileServic接口下
package com.StudentTest.service;
import com.StudentTest.pojo.User;
import java.util.ArrayList;
public interface FileService
ArrayList<User> 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;
FileServiceImpl实现类下
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<User> 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);
pojo下
User学生类
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 +
'';
Dao下
FileDao接口下
package com.StudentTest.Dao;
import com.StudentTest.pojo.User;
import java.util.ArrayList;
public interface FileDao
ArrayList<User> 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;
FileDaoImpl实现类下
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<User> getAllStudent()
ArrayList<User> 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_name ='"+del+"'or + 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;
utils下
JDBCUtils
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包
以上是关于基于java+mysql+JDBC+tomcat+Servlet+JSP+js的学生管理系统的主要内容,如果未能解决你的问题,请参考以下文章
Tomcat 8 - java.sql.SQLException:无法为连接 URL 'jdbc:mysql://xxx/myApp' 创建类 '' 的 JDBC 驱动程序
java.lang.ClassNotFoundException:com.mysql.jdbc.Driver [Eclipse,Tomcat] [重复]
java.lang.ClassNotFoundException:com.mysql.jdbc.Driver 在运行时(eclipse / maven /tomcat)[重复]
java web 工程找不到tomcat类 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException MySQL + Apache Tomcat 7