跟着b站上一个up的视频开始做项目,因为这学期期末实验课要求的一个大作业是开发一个基于B/S的信息管理系统,相当于是补交吧。
一、登陆界面——login.jsp 简单的输入空验证
<%--
Created by IntelliJ IDEA.
User: 康
Date: 2022/1/14
Time: 22:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆界面</title>
<style>
#frame_{
width: 400px;
height: 300px;
margin-left: 550px;
margin-top: 50px;
background-color: azure;
border: black solid 2px;
}
#inner{
margin-left: 100px;
margin-top: 70px;
}
</style>
<script>
</script>
</head>
<body>
<div id="frame_">
<div id="inner">
用户名: <input type="text" id="user-id"><br><br>
密码:     <input type="password" id="user-pwd"><br><br>
<button id="btn01">登录</button> <button id="btn02" style="margin-left: 30px">重置</button><br>
<span id="error" style="color: red">${error}</span>
<script>
let btn001=document.getElementById("btn01");
btn001.onclick=function (){
//查询id是否为空
let id_str=document.getElementById("user-id").value;
if(id_str==null||id_str==""){
document.getElementById("error").innerHTML="账号不能为空!";
return;
}
//查询密码是否为空
let pwd_str=document.getElementById("user-pwd").value;
if(pwd_str==null||pwd_str==""){
document.getElementById("error").innerHTML="密码不能为空!";
return;
}
//开始访问服务器
window.location.href="login?u_id="+id_str+"&u_pwd="+pwd_str;
}
</script>
</div>
</div>
</body>
</html>
二、LoginServlet实现登录页的密码验证并跳转到主页面
package com.studentTest.servlet;
import com.studentTest.bean.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.sql.SQLException;
import java.util.ArrayList;
//注解为起的名字
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
static User flag;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String u_id=req.getParameter("u_id");
String u_pwd=req.getParameter("u_pwd");
User u=new User();
u.setU_id(u_id);
u.setU_pwd(u_pwd);
loginService login=new loginServiceImpl();//多态思想,实现类实现接口
try {
flag=login.loginservice(u);//flag承接返回结果,如果为空,则账户密码与数据库不一致,否则验证合格,跳转
} catch (SQLException e) {
e.printStackTrace();
}
if(flag!=null){
FileService fs=new FileServiceImpl();
ArrayList<User> arrUser= null;
try {
arrUser = fs.getAllStudent();
} catch (SQLException e) {
e.printStackTrace();
}
req.setAttribute("user",flag);
req.setAttribute("arrUser",arrUser);
req.getRequestDispatcher("mainPage.jsp").forward(req,resp);
}else {
req.setAttribute("error","账户和密码不一致");
req.getRequestDispatcher("login.jsp").forward(req,resp);
}
}
}
三、mainPage.jsp 主页显示
<%--
Created by IntelliJ IDEA.
@author: 康
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>主界面</title>
<style>
#head_{
height: 100px;
width: 100%;
background-color: slategray;
}
#personFile{
width: 120px;
height: 50px;
padding-top: 25px;
margin-left: 90%;
}
#search{
height: 70px;
width: 100%;
background-color: cornflowerblue;
}
#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: black 1px solid;
padding:1px;
}
</style>
<script>
function Update(v) {
//修改
if(${user.getU_role()==1}){
window.location.href="updateServlet?v="+v+"&id=${user.getU_id()}";
}else{
alert("权限不够,操作失败!");
}
}
function insert() {
//增加
if(${user.getU_role()==1}){
window.location.href="add.jsp?admin=${user.getU_id()}";
}else{
alert("权限不够,操作失败!");
}
}
function search(str){
//查询
window.location.href="mainServlet?str="+str+"&id=${user.getU_id()}";
}
function goShow(v){
window.location.href="showServlet?v="+v+"&id=${user.getU_id()}";
}
function del(v) {
//删除
if(${user.getU_role()==1}){
let isdel=confirm("是否删除?");
if(isdel){
window.location.href="delServlet?v="+v+"&id=${user.getU_id()}";
alert("操作成功,删除完成!");
}
}else{
alert("权限不够,删除失败!请联系管理员或老师");
}
}
function x(){
var data=sessionStorage.getItem("admin_id");
return data;
}
</script>
</head>
<body>
<div id="head_">
<div id="personFile">
<h4 style="color: black;font-size: large;text-align: center">学生信息管理系统</h4>
名字:<span style="color: red">${user.getU_name()}</span><br>
编号:<span style="color: red">${user.getU_id()}</span><br>
身份:<span style="color: red" id="身份">
<script>
if(${user.getU_role()==1}&&${user.getU_isdelete()==0})
{document.getElementById("身份").innerHTML="管理员";}
if(${user.getU_role()==0}&&${user.getU_isdelete()==0}){
document.getElementById("身份").innerHTML="学生";
}
if(${user.getU_role()==1}&&${user.getU_isdelete()==1})
{document.getElementById("身份").innerHTML="老师";}
</script>
</span>
</div>
</div>
<div id="search">
<div id="inner_s">
<input type="text" style="font-size: 20px; height: 26px;width: 190px" id="file_id">
<button style="font-size: 18px; height: 28px;" onclick="search(document.getElementById('file_id').value)">查询</button>
<button style="font-size: 18px; height: 28px;" onclick="insert()">录入</button>
</div>
</div>
<div>
<table id="table" style="height: 30px;width: 700px;border: black 1px solid;border-collapse:collapse;">
<tr >
<td>学生id</td>
<td>学生名字</td>
<td>学生电话</td>
<td>查看成绩</td>
<td>操作</td>
</tr>
<c:forEach items="${arrUser}" var="item">
<tr>
<td>${item.getU_id()}</td>
<td>${item.getU_name()}</td>
<td>${item.getU_phone()}</td>
<td><button style="color: chocolate" onclick="goShow(${item.getU_id()})">查看成绩</button></td>
<td><button style="color: chocolate" onclick="Update(${item.getU_id()})">修改</button>
<button style="color: chocolate" onclick="del(${item.getU_id()})">删除</button>
</td>
</tr>
</c:forEach>
</table>
</div>
<div>
</div>
</body>
</html>
对id的识别通过数据库的两个属性is_role和is_delete两个属性判断身份和是否被删除,
isrole ,isdelete 0 0 学生
isrole ,isdelete 0 1 删除的学生
isrole ,isdelete 1 0 管理员(有权限)
isrole ,isdelete 1 1 老师(有权限)
四、scorePage.jsp 查看学生成绩页面
<%--
Created by IntelliJ IDEA.
User: 康
Date: 2022/1/16
Time: 23:34
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>学生成绩</title>
<style>
#table{
margin-left: 30%;
margin-top: 30px;
}
td{
text-align: center;
height: 20px;
width: 150px;
border: black 1px solid;
padding:1px;
}
</style>
<script>
function BackMain(){
window.location.href="backMainServlet?admID=${admin}";
}
</script>
</head>
<body>
<table id="table" style="height: 30px;width: 700px;border: black 1px solid;border-collapse:collapse;">
<tr>
<td>课程号</td>
<td>课程名</td>
<td>课程老师</td>
<td>分数</td>
</tr>
<c:forEach items="${arr}" var="item">
<tr>
<td>${item.getCou_id()}</td>
<td>${item.getCourse_name()}</td>
<td>${item.getCourse_teacher()}</td>
<td>${item.getScore()}</td>
</tr>
</c:forEach>
</table>
<button onclick="BackMain()" value="back" style="margin-left: 50%;background-color: aquamarine;font-size: medium">返回</button>
</body>
</html>
五、updatePage.jsp 修改学生信息页面
<%--
Created by IntelliJ IDEA.
User: 康
Date: 2022/1/17
Time: 23:04
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>修改学生信息</title>
<script>
function Toback() {
window.location.href="backMainServlet?admID=${admin}";
}
function ToUp() {
let pwd=document.getElementById("u_pwd").value;
let phone=document.getElementById("u_phone").value;
let sc_1=document.getElementById("sc_1").value;
let sc_2=document.getElementById("sc_2").value;
let sc_3=document.getElementById("sc_3").value;
let sc_4=document.getElementById("sc_4").value;
window.location.href="dealUpdateServlet?admin=${admin}+&id=${stu.getU_id()}&u_pwd="+pwd+"&u_phone="+phone+"&sc_1="+sc_1+"&sc_2="+sc_2+"&sc_3="+sc_3+"&sc_4="+sc_4;
}
</script>
</head>
<body>
<div style="width: 400px">
<h3 style="margin-left: 50%">修改</h3>
<form method="post" >
<!--隐藏域 提交id-->
<div>
<label for="u_id">id:</label>
<input type="text" name="id" id="u_id" readonly="readonly" placeholder="${stu.getU_id()}">
</div>
<div>
<label for="u_name">姓名:</label>
<input type="text" name="u_name" id="u_name" readonly="readonly" placeholder="${stu.getU_name()}">
</div>
<div>
<label for="u_pwd">密码:</label>
<input type="text" name="u_pwd" id="u_pwd" placeholder="请输入密码">
</div>
<div>
<label for="u_phone">电话:</label>
<input type="text" name="u_phone" id="u_phone" placeholder="请输入电话">
</div>
<div>
<label for="sc_1">计算机网络:</label>
<input type="text" name="course1" id="sc_1" placeholder="请输入成绩">
</div>
<div>
<label for="sc_2">嵌入式开发:</label>
<input type="text" name="course2" id="sc_2" placeholder="请输入成绩">
</div>
<div>
<label for="sc_3">数据挖掘: </label>
<input type="text" name="course3" id="sc_3" placeholder="请输入成绩">
</div>
<div>
<label for="sc_4">Web设计:</label>
<input type="text" name="course4" id="sc_4" placeholder="请输入成绩">
</div>
</form>
<div>
<button onclick="ToUp()" value="sure" style="margin-left: 50%" type="submit">确认修改</button>
<button onclick="Toback()" value="back"style="margin-left: 50%">返回主页</button>
</div>
</div>
</body>
</html>
id和姓名采用
readonly="readonly"设置为只读,方便查看
六、 DelServlet.java 简单的删除功能
package com.studentTest.servlet;
import com.studentTest.bean.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.sql.SQLException;
import java.util.ArrayList;
@WebServlet("/delServlet")
public class DelServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id=req.getParameter("v");
String admin_id=req.getParameter("id");
FileService fs=new FileServiceImpl();
try {
fs.del(id);
} catch (SQLException e) {
e.printStackTrace();
}
/*
/获取两个对象
管理员,所有对象
*/
User admin=null;
try {
admin =fs.getFileById(admin_id);
} catch (SQLException e) {
e.printStackTrace();
}
ArrayList<User> arr = null;
try {
arr=fs.getAllStudent();
} catch (SQLException e) {
e.printStackTrace();
}
req.setAttribute("user",admin);
req.setAttribute("arrUser",arr);
req.getRequestDispatcher("mainPage.jsp").forward(req,resp);
}
}
另外,添加对id识别权限的分配,只有老师和管理员才能删除和修改,学生只能查看成绩。
原理很简单,在按钮添加函数检测当前用户的isrole和isdelete属性值
七、BackMainServlet.java 从其他页面返回主页面
package com.studentTest.servlet;
import com.studentTest.bean.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.sql.SQLException;
import java.util.ArrayList;
@WebServlet("/backMainServlet")
public class BackMainServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String admin_id=req.getParameter("admID");
User admin=null;
ArrayList<User> arr=null;
FileService fs=new FileServiceImpl();
try {
admin=fs.getFileById(admin_id);
arr=fs.getAllStudent();
} catch (SQLException e) {
e.printStackTrace();
}
req.setAttribute("user",admin);
req.setAttribute("arrUser",arr);
req.getRequestDispatcher("mainPage.jsp").forward(req,resp);
}
}
其实方法都大同小异,写多了就明了了。咱也是跟着视频走第一次写,改进了一些
八、DealUpdateServlet.java 接受jsp收到的数据交给service
package com.studentTest.servlet;
import com.studentTest.bean.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.sql.SQLException;
import java.util.ArrayList;
@WebServlet("/dealUpdateServlet")
public class DealUpdateServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String admin_id=req.getParameter("admin");
String stu_id=req.getParameter("id");
String stu_pwd=req.getParameter("u_pwd");
String stu_phone=req.getParameter("u_phone");
String sc_1=req.getParameter("sc_1");
String sc_2=req.getParameter("sc_2");
String sc_3=req.getParameter("sc_3");
String sc_4=req.getParameter("sc_4");
System.out.println(admin_id+" " +stu_id+" "+stu_pwd+" " +stu_phone+" " +sc_1+" "+sc_2+" "+sc_3+" "+sc_4);
FileService fs=new FileServiceImpl();
ArrayList<User> arr=null;
User admin=null;
try {
fs.updateStu(stu_id,stu_pwd,stu_phone,sc_1,sc_2,sc_3,sc_4);
arr=fs.getAllStudent();
admin=fs.getFileById(admin_id);
} catch (SQLException e) {
e.printStackTrace();
}
req.setAttribute("user",admin);
req.setAttribute("arrUser",arr);
req.getRequestDispatcher("mainPage.jsp").forward(req,resp);
}
}
九、DealAddServlet
package com.studentTest.servlet;
import com.studentTest.bean.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.sql.SQLException;
import java.util.ArrayList;
@WebServlet("/dealAddServlet")
public class DealAddServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String admin_id=req.getParameter("admin");
String stu_id=req.getParameter("u_id");
String stu_name=req.getParameter("u_name");
String stu_pwd=req.getParameter("u_pwd");
String stu_phone=req.getParameter("u_phone");
String sc_1=req.getParameter("sc_1");
String sc_2=req.getParameter("sc_2");
String sc_3=req.getParameter("sc_3");
String sc_4=req.getParameter("sc_4");
System.out.println(admin_id+" " +stu_id+" "+stu_name+" "+stu_pwd+" " +stu_phone+" " +sc_1+" "+sc_2+" "+sc_3+" "+sc_4);
FileService fs=new FileServiceImpl();
ArrayList<User> arr=null;
User admin=null;
try {
fs.add(stu_id,stu_name,stu_pwd,stu_phone,sc_1,sc_2,sc_3,sc_4);
arr=fs.getAllStudent();
admin=fs.getFileById(admin_id);
} catch (SQLException e) {
e.printStackTrace();
}
req.setAttribute("user",admin);
req.setAttribute("arrUser",arr);
req.getRequestDispatcher("mainPage.jsp").forward(req,resp);
}
}
这里比较笨,一个一个接受参数,看到其他项目用的Map接受有点方便,下次优化学学。
十、主界面的servlet忘贴了 MainServlet.java
package com.studentTest.servlet;
import com.studentTest.bean.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.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.regex.Pattern;
@WebServlet("/mainServlet")
public class MainServlet extends HttpServlet {
public static boolean isNumberic(String str){
for (int i=0; i<str.length() ; i++) {
if(!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String str =req.getParameter("str");
FileService fs=new FileServiceImpl();
User u=new User();
try {
if(isNumberic(str)){
u=fs.getFileById(str);
}else{
u=fs.getFileByName(str);
}
} catch (Exception e) {
e.printStackTrace();
}
ArrayList<User> arr =new ArrayList<>();
arr.add(u);
String s= (String) req.getSession().getAttribute("admin_id");
System.out.println(s);
String id=req.getParameter("id");
FileService fs1=new FileServiceImpl();
User admin= null;
try {
admin = fs1.getFileById(id);
} catch (SQLException e) {
e.printStackTrace();
}
req.setAttribute("user",admin);
if(str.trim().equals("")){
FileService f=new FileServiceImpl();
ArrayList<User> Arr = new ArrayList<>();
try {
Arr = f.getAllStudent();
} catch (SQLException e) {
e.printStackTrace();
}
req.setAttribute("arrUser",Arr);
req.getRequestDispatcher("mainPage.jsp").forward(req,resp);
}else{
req.setAttribute("arrUser",arr);
req.getRequestDispatcher("mainPage.jsp").forward(req,resp);
}
}
}
十一、ShowServlet.java 展示学生成绩的servlet
其实跟前面的servlet都很相似,换皮
package com.studentTest.servlet;
import com.studentTest.bean.Major;
import com.studentTest.bean.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.sql.SQLException;
import java.util.ArrayList;
@WebServlet("/showServlet")
public class ShowServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//查看该id的成绩
String StudentID=req.getParameter("v");
String admin_id=req.getParameter("id");
FileService fs=new FileServiceImpl();
ArrayList arr=new ArrayList();
try {
arr=fs.getPersonFile(StudentID);
} catch (SQLException e) {
e.printStackTrace();
}
req.setAttribute("arr",arr);
req.setAttribute("admin",admin_id);
req.getRequestDispatcher("scorePage.jsp").forward(req,resp);
}
}
十二、UpdateServlet.java
package com.studentTest.servlet;
import com.studentTest.bean.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.sql.SQLException;
import java.util.Map;
@WebServlet("/updateServlet")
public class UpdateServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String id=req.getParameter("v");
String admin_id=req.getParameter("id");
FileService fs=new FileServiceImpl();
User u=null;
try {
u=fs.getFileById(id);
} catch (SQLException e) {
e.printStackTrace();
}
req.setAttribute("stu",u);
req.setAttribute("admin",admin_id);
req.getRequestDispatcher("updatePage.jsp").forward(req,resp);
}
}
然后服务层(Service)
十三、Service层
1.loginService 接口类
package com.studentTest.service;
import com.studentTest.bean.User;
import java.sql.SQLException;
import java.util.ArrayList;
public interface loginService {
User loginservice(User user) throws SQLException;
}
2.loginServiceImpl 接口实现类
package com.studentTest.service;
import com.studentTest.bean.User;
import com.studentTest.dao.LoginDao;
import com.studentTest.dao.LoginDaoImpl;
import java.sql.SQLException;
import java.util.ArrayList;
public class loginServiceImpl implements loginService{
@Override
public User loginservice(User user) throws SQLException {
LoginDao logindao=new LoginDaoImpl();
return logindao.Logindao(user);
}
}
3.FileService 接口类
package com.studentTest.service;
import com.studentTest.bean.Major;
import com.studentTest.bean.User;
import java.sql.SQLException;
import java.util.ArrayList;
public interface FileService {
ArrayList<User> getAllStudent() throws SQLException;
User getFileById(String id) throws SQLException;
User getFileByName(String name) throws SQLException;
ArrayList<Major> getPersonFile(String studentID) throws SQLException;
void del(String id) throws SQLException;
void updateStu(String stu_id, String stu_pwd, String stu_phone, String sc_1, String sc_2, String sc_3, String sc_4) throws SQLException;
void add(String stu_id, String stu_name, String stu_pwd, String stu_phone, String sc_1, String sc_2, String sc_3, String sc_4) throws SQLException
}
4.FileServiceImpl 接口实现类
package com.studentTest.service;
import com.studentTest.bean.Major;
import com.studentTest.bean.User;
import com.studentTest.dao.FileDao;
import com.studentTest.dao.FileDaoImpl;
import java.sql.SQLException;
import java.util.ArrayList;
public class FileServiceImpl implements FileService{
FileDao fileDao=new FileDaoImpl();
@Override
public ArrayList<User> getAllStudent() throws SQLException {
//访问持久层
return fileDao.getAllStudent();
}
@Override
public User getFileById(String id) throws SQLException {
return fileDao.getFileById(id);
}
@Override
public User getFileByName(String name) throws SQLException {
return fileDao.getFileByName(name);
}
@Override
public ArrayList<Major> getPersonFile(String studentID) throws SQLException {
return fileDao.getPersonFile(studentID);
}
@Override
public void del(String id) throws SQLException {
fileDao.del(id);
}
@Override
public void updateStu(String stu_id, String stu_pwd, String stu_phone, String sc_1, String sc_2, String sc_3, String sc_4) throws SQLException {
fileDao.updateStu(stu_id,stu_pwd,stu_phone,sc_1,sc_2,sc_3,sc_4);
}
@Override
public void add(String stu_id, String stu_name, String stu_pwd, String stu_phone, String sc_1, String sc_2, String sc_3, String sc_4) throws SQLException {
fileDao.add(stu_id,stu_name,stu_pwd,stu_phone,sc_1,sc_2,sc_3,sc_4);
}
}
服务层通过调用持久层(Dao层)实现对数据库的连接、操作
十四、持久层(Dao层)
1.LoginDao
package com.studentTest.dao;
import com.studentTest.bean.User;
import com.studentTest.util.JDBCUtils;
import java.sql.Connection;
import java.sql.SQLException;
public interface LoginDao {
public static Connection connection = null;
static Connection getConnection() throws SQLException {
return JDBCUtils.getConnection();
}
User Logindao(User user) throws SQLException;
}
2.LoginDaoImpl
package com.studentTest.dao;
import com.studentTest.bean.User;
import com.studentTest.util.JDBCUtils;
import javax.naming.ldap.ControlFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class LoginDaoImpl implements LoginDao {
static Connection conn;
static {
try {
conn = FileDao.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public User Logindao(User user) throws SQLException {
//数据库查询验证登陆
String id=user.getU_id();
String pwd=user.getU_pwd();
//获取连接对象
//Connection connection= JDBCUtils.getConnection();
//准备sql语句
String sql="select * from user where u_id="+id+" AND u_pwd="+pwd;
PreparedStatement preparedStatement=conn.prepareStatement(sql);
ResultSet res=preparedStatement.executeQuery();
while(res.next()){
User u=new User();
u.setU_id(res.getString("u_id"));
u.setU_pwd(res.getString("u_pwd"));
u.setU_name(res.getString("u_name"));
u.setU_phone(res.getString("u_phone"));
u.setU_role(res.getInt("u_role"));
u.setU_isdelete(res.getInt("u_isdelete"));
return u;
}
return null;
}
}
数据库连接,验证jsp传来的id和pwd是否在数据库存在,如果有,返回一个User实体,否则返回null
3.FileDao
package com.studentTest.dao;
import com.studentTest.bean.Major;
import com.studentTest.bean.User;
import com.studentTest.util.JDBCUtils;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
public interface FileDao {
static Connection getConnection() throws SQLException {
return JDBCUtils.getConnection();
}
ArrayList<User> getAllStudent() throws SQLException;
User getFileById(String id) throws SQLException;
User getFileByName(String name) throws SQLException;
ArrayList<Major> getPersonFile(String studentID) throws SQLException;
void del(String id) throws SQLException;
void updateStu(String stu_id, String stu_pwd, String stu_phone, String sc_1, String sc_2, String sc_3, String sc_4) throws SQLException;
void add(String stu_id, String stu_name, String stu_pwd, String stu_phone, String sc_1, String sc_2, String sc_3, String sc_4) throws SQLException;
}
4.FileDaoImpl
package com.studentTest.dao;
import com.studentTest.bean.Major;
import com.studentTest.bean.User;
import com.studentTest.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class FileDaoImpl implements FileDao{
static Connection conn;
static {
try {
conn = FileDao.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public ArrayList<User> getAllStudent() throws SQLException {
ArrayList<User> list=new ArrayList<>();
//获取连接对象
//Connection connection= JDBCUtils.getConnection();
//准备sql语句
String sql="select * from user where u_role=0 AND u_isdelete=0";
PreparedStatement preparedStatement=conn.prepareStatement(sql);
ResultSet res=preparedStatement.executeQuery();
while(res.next()){
User u=new User();
u.setU_id(res.getString("u_id"));
u.setU_pwd(res.getString("u_pwd"));
u.setU_name(res.getString("u_name"));
u.setU_phone(res.getString("u_phone"));
u.setU_role(res.getInt("u_role"));
u.setU_isdelete(res.getInt("u_isdelete"));
list.add(u);
}
return list;
}
@Override
public User getFileById(String id) throws SQLException {
//获取连接对象
//Connection connection = JDBCUtils.getConnection();
//准备sql语句
String sql = "select * from user where u_id="+"'"+id+"'";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
ResultSet res = preparedStatement.executeQuery();
while (res.next()) {
User u = new User();
u.setU_id(res.getString("u_id"));
u.setU_pwd(res.getString("u_pwd"));
u.setU_name(res.getString("u_name"));
u.setU_phone(res.getString("u_phone"));
u.setU_role(res.getInt("u_role"));
u.setU_isdelete(res.getInt("u_isdelete"));
return u;
}
JDBCUtils.close();
return null;
}
@Override
public User getFileByName(String name) throws SQLException {
//获取连接对象
//Connection connection= JDBCUtils.getConnection();
//准备sql语句
String sql="select * from user where u_name='"+name+"'";
PreparedStatement preparedStatement=conn.prepareStatement(sql);
ResultSet res=preparedStatement.executeQuery();
while (res.next()) {
User u = new User();
u.setU_id(res.getString("u_id"));
u.setU_pwd(res.getString("u_pwd"));
u.setU_name(res.getString("u_name"));
u.setU_phone(res.getString("u_phone"));
u.setU_role(res.getInt("u_role"));
u.setU_isdelete(res.getInt("u_isdelete"));
return u;
}
return null;
}
@Override
public ArrayList<Major> getPersonFile(String studentID) throws SQLException {
//获取连接对象
//Connection connection= JDBCUtils.getConnection();
//准备sql语句
String sql="SELECT cou_id,course_name,course_teacher,score FROM course,userchoose where course.course_id=userchoose.cou_id AND userchoose.stu_id="+"'"+studentID+"'";
PreparedStatement preparedStatement=conn.prepareStatement(sql);
ResultSet res=preparedStatement.executeQuery();
ArrayList<Major> arr=new ArrayList<>();
while (res.next()) {
Major m=new Major();
m.setCou_id(res.getString("cou_id"));
m.setCourse_name(res.getString("course_name"));
m.setCourse_teacher(res.getString("course_teacher"));
m.setScore(res.getString("score"));
arr.add(m);
}
return arr;
}
@Override
public void del(String id) throws SQLException {
//Connection connection= JDBCUtils.getConnection();
//准备sql语句
String sql="UPDATE user SET u_isdelete=1 WHERE u_id="+id;
PreparedStatement preparedStatement=conn.prepareStatement(sql);
preparedStatement.executeUpdate();
}
@Override
public void updateStu(String stu_id, String stu_pwd, String stu_phone, String sc_1, String sc_2, String sc_3, String sc_4) throws SQLException {
int sc1=Integer.parseInt(sc_1);int sc2=Integer.parseInt(sc_2);
int sc3=Integer.parseInt(sc_3);int sc4=Integer.parseInt(sc_4);
String sql1="UPDATE `user` SET u_pwd =?,u_phone =? WHERE u_id=?";
String sql2="UPDATE userchoose SET score=? WHERE stu_id=? and cou_id=?";
PreparedStatement preparedStatement=conn.prepareStatement(sql1);
preparedStatement.setString(1,stu_pwd);
preparedStatement.setString(2,stu_phone);
preparedStatement.setString(3,stu_id);
preparedStatement.executeUpdate();
PreparedStatement pstm=conn.prepareStatement(sql2);
pstm.setInt(1,sc1);
pstm.setString(2,stu_id);
pstm.setString(3,"A1001");
pstm.executeUpdate();
PreparedStatement pstm2=conn.prepareStatement(sql2);
pstm2.setInt(1,sc2);
pstm2.setString(2,stu_id);
pstm2.setString(3,"A1002");
pstm2.executeUpdate();
PreparedStatement pstm3=conn.prepareStatement(sql2);
pstm3.setInt(1,sc3);
pstm3.setString(2,stu_id);
pstm3.setString(3,"A1003");
pstm3.executeUpdate();
PreparedStatement pstm4=conn.prepareStatement(sql2);
pstm4.setInt(1,sc4);
pstm4.setString(2,stu_id);
pstm4.setString(3,"A1004");
pstm4.executeUpdate();
}
@Override
public void add(String stu_id, String stu_name, String stu_pwd, String stu_phone, String sc_1, String sc_2, String sc_3, String sc_4) throws SQLException {
int sc1=Integer.parseInt(sc_1);int sc2=Integer.parseInt(sc_2);
int sc3=Integer.parseInt(sc_3);int sc4=Integer.parseInt(sc_4);
String sql1="INSERT into `user` VALUES (?,?,?,?,'0','0')";
String sql2="INSERT into userchoose VALUES (?,?,?)";
PreparedStatement preparedStatement=conn.prepareStatement(sql1);
preparedStatement.setString(1,stu_id);
preparedStatement.setString(2,stu_pwd);
preparedStatement.setString(3,stu_name);
preparedStatement.setString(4,stu_phone);
preparedStatement.executeUpdate();
PreparedStatement pstm=conn.prepareStatement(sql2);
pstm.setInt(3,sc1);
pstm.setString(1,stu_id);
pstm.setString(2,"A1001");
pstm.executeUpdate();
PreparedStatement pstm2=conn.prepareStatement(sql2);
pstm2.setInt(3,sc2);
pstm2.setString(1,stu_id);
pstm2.setString(2,"A1002");
pstm2.executeUpdate();
PreparedStatement pstm3=conn.prepareStatement(sql2);
pstm3.setInt(3,sc3);
pstm3.setString(1,stu_id);
pstm3.setString(2,"A1003");
pstm3.executeUpdate();
PreparedStatement pstm4=conn.prepareStatement(sql2);
pstm4.setInt(3,sc4);
pstm4.setString(1,stu_id);
pstm4.setString(2,"A1004");
pstm4.executeUpdate();
}
}
最底层的数据库操作,之前前段页面多次点击访问数据库就会卡死,控制台报错GetConnectionTimeoutException,应该是每次对service对象实例化就会执行
Connection connection= JDBCUtils.getConnection()一次,超过数据库连接池的最大连接数,然后报错,最后在dao的实现接口类中定义一个静态的connection,保证每次调用它只connect一次,然后连接池自动管理,就不会超过连接数了吧,这里我不是很清楚连接池(Druid)的连接原理,之后是没报错了however。
十五、 基本对象的创建
1.User
package com.studentTest.bean;
public class User {
private String u_id;
private String u_pwd;
private String u_name;
private String u_phone;
private int u_role;
private int u_isdelete;
public User() {}
public User(String u_id, String u_pwd, String u_name, String u_phone, int u_role, int u_isdelete) {
this.u_id = u_id;
this.u_pwd = u_pwd;
this.u_name = u_name;
this.u_phone = u_phone;
this.u_role = u_role;
this.u_isdelete = u_isdelete;
}
public String getU_id() {
return u_id;
}
public void setU_id(String 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_name() {
return u_name;
}
public void setU_name(String u_name) {
this.u_name = u_name;
}
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_id=" + u_id +
", u_pwd='" + u_pwd + '\'' +
", u_name='" + u_name + '\'' +
", u_phone='" + u_phone + '\'' +
", u_role=" + u_role +
", u_isdelete=" + u_isdelete +
'}';
}
}
2.Major
package com.studentTest.bean;
public class Major {
private String cou_id;
private String course_name;
private String course_teacher;
private String score;
public Major() {
}
public Major(String cou_id, String course_name, String course_teacher, String score) {
this.cou_id = cou_id;
this.course_name = course_name;
this.course_teacher = course_teacher;
this.score = score;
}
public String getCou_id() {
return cou_id;
}
public void setCou_id(String cou_id) {
this.cou_id = cou_id;
}
public String getCourse_name() {
return course_name;
}
public void setCourse_name(String course_name) {
this.course_name = course_name;
}
public String getCourse_teacher() {
return course_teacher;
}
public void setCourse_teacher(String course_teacher) {
this.course_teacher = course_teacher;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
@Override
public String toString() {
return "Major{" +
"cou_id='" + cou_id + '\'' +
", course_name='" + course_name + '\'' +
", course_teacher='" + course_teacher + '\'' +
", score='" + score + '\'' +
'}';
}
}
十六、数据库 表设计
用户表 user
课程表 course
选课表 userchoose
十七、
大致功能实现了,还有个添加没有弄,后面有时间再补
对前端设计完全没有思路啊喂,这学期Web网页设计白上了(哭),后面继续学其他的吧,从网上打听到servlet现在好像有点过时了,都在搞ssm了,得赶紧看了,小垃圾,加油,冲!
2022.1.19
1.23 想起来项目还没有完善,今天把它弄完了,录入功能。