1.登陆页面
login.jsp
<%--
Created by IntelliJ IDEA.
User: tao
Date: 2024/8/5
Time: 20:17
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
<style type="text/css">
*{margin:0;padding:0;}
body{margin:0;padding:0;text-align:center;}
#tab{width:60%;height:120px;border-collapse: collapse;margin:0 auto;}
#tab td{border:1px solid blue;font-size:12px;}
.tdcls{text-align:right;width:40%;}
.inputw{width:180px;}
</style>
</head>
<body>
<form action="loginServlet" method="post">
<table id="tab">
<tr>
<td colspan="2" align="center">
<strong>登录操作</strong>
</td>
</tr>
<tr>
<td class="tdcls">登录名称:</td>
<td>
<input type="text" name="loginame" class="inputw" >
</td>
</tr>
<tr>
<td class="tdcls">登录密码:</td>
<td>
<input type="password" name="loginpass" class="inputw">
<% if(application.getAttribute("INFO")!=null){
%>
<%=application.getAttribute("INFO")%>
<% }
%>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="登 录">
<input type="reset" value="重 置">
</td>
</tr>
</table>
</form>
</body>
</html>
LoginServlet
@WebServlet(name = "LoginServlet", value = "/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext application=this.getServletContext();
String loginame = request.getParameter("loginame");
String loginpass = request.getParameter("loginpass");
boolean b = new WatchEmpServiceImpl().loginEmp(loginame, loginpass);
if(b){
List<UnionEmp> emps = new WatchEmpServiceImpl().watchEmp2();
request.setAttribute("List_UnionEmp",emps);
request.getRequestDispatcher("WatchAll.jsp").forward(request,response);
}else{
application.setAttribute("INFO","登陆失败,用户名或密码错误");
response.sendRedirect("login.jsp");
}
}
}
2.登录之后进入WatchAll.jsp"页面
该页面可进行查询,修改,新增,分页等功能
WatchAll.jsp页面
<form action="seachServlet" method="get">
<input type="text" name="searchName" placeholder="请输入您要查询的名字">
<input type="text" name="searchJob" placeholder="请输入您要查询的工作">
<input type="text" name="searchDeptNo" placeholder="请输入您要查询的部门编号">
<input type="submit" value="搜索">
</form>
<table border="1">
<tr>
<td>编号</td>
<td>姓名</td>
<td>密码</td>
<td>职位</td>
<td>薪水</td>
<td>部门编号</td>
<td>部门名称</td>
<td>入职日期</td>
<td>操作</td>
</tr>
<%-- <c:forEach ></c:forEach>--%>
<c:forEach var="unionnObj" varStatus="i" items="${requestScope.List_UnionEmp}">
<tr>
<td> ${unionnObj.empno}</td>
<td> ${unionnObj.ename}</td>
<td> ${unionnObj.pass}</td>
<td> ${unionnObj.job}</td>
<td> ${unionnObj.sal}</td>
<td> ${unionnObj.DEPTNO}</td>
<td> ${unionnObj.dname}</td>
<td> ${unionnObj.hireDate}</td>
<td>
<a href="updateEmpServlet?empno=${unionnObj.empno}">修改</a>
<a href="deleteEmpServlet?empno=${unionnObj.empno}">删除</a>
</td>
</tr>
</c:forEach>
<tr>
<td>
<a href="pageServlet?currPage=1">首页</a>
<c:if test="${CURRENT_PAGE>1}">
<a href="pageServlet?current_page=${CURRENT_PAGE-1}">上一页</a>
</c:if>
<c:if test="${CURRENT_PAGE<MAX_PAGE}">
<a href="pageServlet?current_page=${CURRENT_PAGE+1}">下一页</a>
</c:if>
<a href="pageServlet?currPage=${requestScope.MAX_PAGE}">尾页</a>
</td>
</tr>
</table>
<a href="add.jsp">新增员工</a>
<% if(application.getAttribute("info")!=null){
%>
<%=application.getAttribute("info")%>
<% }
%>
watchAllServlet
@WebServlet(name = "WatchAllServlet", value = "/watchAllServlet")
public class WatchAllServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<UnionEmp> emps = new WatchEmpServiceImpl().watchEmp2();
request.setAttribute("List_UnionEmp",emps);
request.getRequestDispatcher("WatchAll.jsp").forward(request,response);
}
3.删除功能:点击删除按钮显示删除成功
deleteEmpServlet
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext aplication=this.getServletContext();
String empno = request.getParameter("empno");
boolean b = new WatchEmpServiceImpl().deleteEmpOne(Double.parseDouble(empno));
if(b){
List<Emp> emps = new WatchEmpServiceImpl().watchEmp();
request.setAttribute("List_Emp",emps);
aplication.setAttribute("info","删除成功");
request.getRequestDispatcher("WatchAll.jsp").forward(request,response);
}else{
aplication.setAttribute("info","删除错误");
response.sendRedirect("/WatchAll.jsp");
}
}
}
4.新增功能
点击首页新增员工
add.jsp
<form action="addEmpServlet" method="post">
<table>
<%-- (input type="hidden"),用于存储原始的empNo值。--%>
<tr>
<td>编号:</td>
<td><input type="text" name="empNo" /></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="empName" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="pass"/></td>
</tr>
<tr>
<td>工作:</td>
<td><input type="text" name="job" /></td>
</tr>
<tr>
<td>:薪水</td>
<td><input type="text" name="sal" /></td>
</tr>
<tr>
<td>:部门编号</td>
<td><input type="text" name="deptNo" /></td>
</tr>
<tr>
<td>:入职日期</td>
<td><input type="date" name="hiredate" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="新增"/></td>
</tr>
</table>
</form>
addEmpServlet
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
ServletContext application=this.getServletContext();
// empNo empName pass job sal hiredate
String empNo = request.getParameter("empNo");
String empName = request.getParameter("empName");
String pass = request.getParameter("pass");
String job = request.getParameter("job");
String deptNo = request.getParameter("deptNo");
String sal = request.getParameter("sal");
String hiredateStr = request.getParameter("hiredate");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); // 假设日期格式为ISO标准日期格式
Date hiredate = null;
// 使用 SimpleDateFormat 解析日期字符串
try {
hiredate = sdf.parse(hiredateStr);
} catch (ParseException e) {
throw new RuntimeException(e);
}
boolean b = new WatchEmpDaoImpl().addEmpOne(Double.parseDouble(empNo), empName, job, pass, Double.parseDouble(sal), hiredate,deptNo);
if(b){
application.setAttribute("info","新增成功");
request.getRequestDispatcher("/watchAllServlet").forward(request,response);
// response.sendRedirect("/watchAllServlet");
}else{
application.setAttribute("info","新增失败");
// request.getRequestDispatcher("/watchAllServlet").forward(request,response);
response.sendRedirect("/watchAllServlet");
}
}
5.修改员工数据
updateEmpServlet
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String empno = request.getParameter("empno");
List<Emp> emps = new WatchEmpServiceImpl().selectEmpOne(Double.parseDouble(empno));
List<Dept> depts = new WatchEmpServiceImpl().watchAllDept();
if(!emps.isEmpty()){
Emp emp = emps.get(0);
request.setAttribute("Emp",emp);
request.setAttribute("Dept",depts);
request.getRequestDispatcher("update.jsp").forward(request,response);
}else {
request.getRequestDispatcher("/watchAllServlet").forward(request,response);
// response.sendRedirect("watchAllServlet");
}
update.jsp
<table border="1">
<tr>
<td>编号</td>
<td>姓名</td>
<td>密码</td>
<td>职位</td>
<td>薪水</td>
<td>部门编号</td>
<td>部门名称</td>
<td>入职日期</td>
<td>操作</td>
</tr>
<%
List<UnionEmp> unionEmpList = (List<UnionEmp>) request.getAttribute("UnionEmp_List");
for (UnionEmp emp : unionEmpList) {
%>
<tr>
<td><%=emp.getEmpno()%></td>
<td><%=emp.getEname()%></td>
<td><%=emp.getPass()%></td>
<td><%=emp.getJob()%></td>
<td><%=emp.getSal()%></td>
<td><%=emp.getDEPTNO()%></td>
<td><%=emp.getDname()%></td>
<td><%=emp.getHireDate()%></td>
<td>
<a href="updateEmpServlet?empno=<%=emp.getEmpno()%>">修改</a>
<a href="deleteEmpServlet?empno=<%=emp.getEmpno()%>">删除</a>
<a href="watchAllServlet">返回</a>
</td>
</tr>
<%
}
%>
</table>
5..动态查询数据
三个条件同时查询
根据查询条件新建一个类EmpMation
public class EmpMation {
private String ename;
private String job;
private String DEPTNO;
数据库多条件查询:
@Override
public List<UnionEmp> SearchEmpMation(EmpMation empMation) {
String sql = "select emp.*, dept.dname from emp JOIN dept on emp.DEPTNO = dept.DEPTNO where 1=1";
List<Object> contain = new ArrayList<>();
sql += (empMation.getEname() != null) ? " and emp.ENAME like ?" : "";
if (empMation.getEname() != null) {
contain.add("%" + empMation.getEname() + "%");
}
sql += (empMation.getJob() != null) ? " and emp.job LIKE ?" : "";
if (empMation.getJob() != null) {
contain.add("%" + empMation.getJob() + "%");
}
sql += (empMation.getDEPTNO() != null) ? " and emp.deptno= ? " : "";
if (empMation.getDEPTNO() != null) {
contain.add(empMation.getDEPTNO());
}
System.out.println(sql);
System.out.println(":"+empMation.getJob());
// return JdbcUtil.executeQuery(sql, UnionEmp.class, "SALESMAN");
return JdbcUtil.executeQuery(sql, UnionEmp.class, contain.toArray());
}
seachServlet
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("searchName");
String job = request.getParameter("searchJob");
// String job="SALESMAN ";
String deptNo = request.getParameter("searchDeptNo");
if (name == "") {
name = null;
}
if (deptNo == "") {
deptNo = null;
}
if (job == "") {
job = null;
}
EmpMation empMation = new EmpMation(name, job, deptNo);
System.out.println(empMation);
System.out.println(name + " " + job + " " + deptNo);
List<UnionEmp> emps = new WatchEmpServiceImpl().SearchEmpMation(empMation);
System.out.println(emps);
request.setAttribute("UnionEmp_List", emps);
request.getRequestDispatcher("SeachEmp.jsp").forward(request, response);
}
SearchEmp.jsp
<table border="1">
<tr>
<td>编号</td>
<td>姓名</td>
<td>密码</td>
<td>职位</td>
<td>薪水</td>
<td>部门编号</td>
<td>部门名称</td>
<td>入职日期</td>
<td>操作</td>
</tr>
<%
List<UnionEmp> unionEmpList = (List<UnionEmp>) request.getAttribute("UnionEmp_List");
for (UnionEmp emp : unionEmpList) {
%>
<tr>
<td><%=emp.getEmpno()%></td>
<td><%=emp.getEname()%></td>
<td><%=emp.getPass()%></td>
<td><%=emp.getJob()%></td>
<td><%=emp.getSal()%></td>
<td><%=emp.getDEPTNO()%></td>
<td><%=emp.getDname()%></td>
<td><%=emp.getHireDate()%></td>
<td>
<a href="updateEmpServlet?empno=<%=emp.getEmpno()%>">修改</a>
<a href="deleteEmpServlet?empno=<%=emp.getEmpno()%>">删除</a>
<a href="watchAllServlet">返回</a>
</td>
</tr>
<%
}
%>
</table>
6.分页功能
WatchALL.jsp
<tr>
<td>
<a href="pageServlet?currPage=1">首页</a>
<c:if test="${CURRENT_PAGE>1}">
<a href="pageServlet?current_page=${CURRENT_PAGE-1}">上一页</a>
</c:if>
<c:if test="${CURRENT_PAGE<MAX_PAGE}">
<a href="pageServlet?current_page=${CURRENT_PAGE+1}">下一页</a>
</c:if>
<a href="pageServlet?currPage=${requestScope.MAX_PAGE}">尾页</a>
</td>
</tr>
pageServlet
private void queryAllPage() throws ServletException, IOException {
String currnPage=request.getParameter("current_page");
Integer pageIndex=0;//当前页
int pageCount=2;
if(currnPage==null){
pageIndex=1;
}else {
pageIndex=Integer.parseInt(currnPage);
}
int maxPage=0;
int total= new WatchEmpServiceImpl().watchEmp2().size();
if(total%pageCount==0){
maxPage=total/pageCount;
}else {
maxPage=(total/pageCount)+1;
}
request.setAttribute("MAX_PAGE",maxPage);
request.setAttribute("CURRENT_PAGE",pageIndex);
List<UnionEmp> emps = new WatchEmpServiceImpl().unionWatchEmp(pageIndex, pageCount);
// List<UnionEmp> emps = new WatchEmpServiceImpl().watchEmp2();
request.setAttribute("List_UnionEmp",emps);
request.getRequestDispatcher("WatchAll.jsp").forward(request,response);
数据库根据页数查询
@Override
public List<UnionEmp> unionWatchEmp(int pageNow, int pageCount) {
String sql = "SELECT emp.*,DNAME from emp JOIN dept on emp.DEPTNO=dept.DEPTNO limit ?,?";
return JdbcUtil.executeQuery(sql, UnionEmp.class, (pageNow - 1) * pageCount, pageCount);
}
7.数据库操作
// 查询所有
List<Emp> watchEmp();
List<UnionEmp> watchEmp2();
// 分页查询
List<UnionEmp> unionWatchEmp(int pageNow,int pageCount);
// 查询单个
// List<Emp> selectEmpOne(double empNo);
List<Emp> selectEmpOne(double empNo);
//模糊查询
List<UnionEmp> SearchEmp(String name);
// 模糊查询多个
List<UnionEmp> SearchEmpMation(EmpMation empMation);
// 修改单个
//删除单个
boolean deleteEmpOne(double empNo);
// 根据用户名和密码登录
boolean loginEmp(String name,String pass);
// 修改数据
boolean updateEmpOne(double empNo,double empNo2,String empName ,String pass ,String job,String deptNo);
// empNo empName pass job sal hiredate
boolean addEmpOne(double empNo, String empName, String job,String pass, double sal, Date hiredate,String deptNo);
//查看所有部门编号和部门名字
List<Dept> watchAllDept();
八、总结
感觉学java学的好累,程序代码老出错,还老犯一些低级错误,都没什么时间出去玩,我真的感觉这钱是该程序员赚哇。