2021 11 10 添加代码
1.先写实体类
两个
打包。Java。POJO
公共课部门{
私有int deptNo
私有字符串deptName
公共部门(int deptNo,String deptName){ 0
this.deptNo=deptNo
this.deptName=deptName
}
公共部门()
}
public int GetDeptno(){ 0
返回deptNo
}
public void setDeptNo(int deptNo){ 0
this.deptNo=deptNo
}
公共字符串getDeptName(){ 0
返回deptName
}
公共void setDeptName(字符串deptName){ 0
this.deptName=deptName
}
@覆盖
公共字符串ToString(){ 0
返回"部门{"
deptNo=' deptNo
,deptName='' deptName '\ ' '
'}';
}
}
打包。Java。POJO
导入Java。乌提尔。日期;
公共类Emp {
private int empNo
私有字符串empName
私有字符串密码;
私人约会生日;
私有int deptNo
私人部门;
public int GetDeptno(){ 0
返回deptNo
}
公共Emp(整数empNo,字符串empName,字符串密码,日期生日,部门间,部门间){ 0
this.empNo=empNo
this.empName=empName
this.password=password
这个。生日=生日;
this.deptNo=deptNo
本部=部门
}
公共Emp(字符串empName,字符串密码,日期生日,int deptNo){ 0
this.empName=empName
this.password=password
这个。生日=生日;
this.deptNo=deptNo
}
public void setDeptNo(int deptNo){ 0
this.deptNo=deptNo
}
公共Emp(){ 0
}
公共部门GetDept(){ 0
退货部门;
}
公共作废设置部门(部门){部门
本部=部门
}
public int getEmpNo(){ return empNo;}
public void setEmpNo(int empNo){ this。empNo=empNo}
公共字符串getEmpName(){ 0
返回empName
}
r>
public void setEmpName(String empName) {
this.empName = empName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
2.写工具类
package com.java.util;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
public class DBUtil {
public static DataSource getDS(){
ComboPooledDataSource ds=new ComboPooledDataSource();
return ds;
}
}
3.写Dao类
package com.java.dao;
import com.java.pojo.Dept;
import java.util.List;
public interface DeptDao {
ListDept getDeptById(int deptNo);
ListDept getAllDept();
}
package com.java.dao;
import com.java.pojo.Emp;
import java.util.List;
public interface EmpDao {
ListEmp getAllEmps();
int addEmp(Emp emp);
}
package com.java.dao.impl;
import com.java.dao.DeptDao;
import com.java.pojo.Dept;
import com.java.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class DeptDaoImpl implements DeptDao {
private QueryRunner qr=new QueryRunner(DBUtil.getDS());
@Override
public ListDept getDeptById(int deptNo) {
String sql="select*from dept where deptNo=";
ListDept dept = null;
try {
dept = qr.query(sql, new BeanListHandler(Dept.class),deptNo);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return dept;
}
@Override
public ListDept getAllDept() {
String sql="select *from dept";
ListDept deptList = null;
try {
deptList = qr.query(sql, new BeanListHandler(Dept.class));
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return deptList;
}
}
package com.java.dao.impl;
import com.java.dao.EmpDao;
import com.java.pojo.Emp;
import com.java.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class EmpDaoImpl implements EmpDao {
private QueryRunner qr=new QueryRunner(DBUtil.getDS());
@Override
public ListEmp getAllEmps() {
String sql="select * from emp";
ListEmp empList = null;
try {
empList = qr.query(sql, new BeanListHandler(Emp.class));
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return empList;
}
@Override
public int addEmp(Emp emp) {
String sql="insert into emp(empName,password,birthday,deptNo) values(,,,)";
int i = 0;
try {
i = qr.update(sql, emp.getEmpName(), emp.getPassword(), emp.getBirthday(), emp.getDeptNo());
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return i;
}
}
4.写服务类
package com.java.service;
import com.java.pojo.Emp;
import java.util.List;
public interface EmpService {
ListEmp getAllEmps();
int addEmp(Emp emp);
}
package com.java.service;
import com.java.pojo.Dept;
import java.util.List;
public interface DeptService {
Dept getDeptById(int deptNO);
ListDept getAllDept();
}
package com.java.service.impl;
import com.java.dao.DeptDao;
import com.java.dao.impl.DeptDaoImpl;
import com.java.pojo.Dept;
import com.java.service.DeptService;
import java.util.List;
public class DeptServiceImpl implements DeptService {
private DeptDao deptDao=new DeptDaoImpl();
@Override
public Dept getDeptById(int deptNO) {
Dept dept = (Dept) deptDao.getDeptById(deptNO);
return dept;
}
@Override
public ListDept getAllDept() {
ListDept allDept=deptDao.getAllDept();
return allDept;
}
}
package com.java.service.impl;
import com.java.dao.EmpDao;
import com.java.dao.impl.EmpDaoImpl;
import com.java.pojo.Emp;
import com.java.service.EmpService;
import java.util.List;
public class EmpServiceImpl implements EmpService {
private EmpDao empDao=new EmpDaoImpl();
@Override
public ListEmp getAllEmps() {
ListEmp allEmps = empDao.getAllEmps();
return allEmps;
}
@Override
public int addEmp(Emp emp) {
int i=empDao.addEmp(emp);
return i;
}
}
5.写serlet
package com.java.service.impl;
import com.java.dao.EmpDao;
import com.java.dao.impl.EmpDaoImpl;
import com.java.pojo.Emp;
import com.java.service.EmpService;
import java.util.List;
public class EmpServiceImpl implements EmpService {
private EmpDao empDao=new EmpDaoImpl();
@Override
public ListEmp getAllEmps() {
ListEmp allEmps = empDao.getAllEmps();
return allEmps;
}
@Override
public int addEmp(Emp emp) {
int i=empDao.addEmp(emp);
return i;
}
}
package com.java.servlet;
import com.java.dao.DeptDao;
import com.java.pojo.Dept;
import com.java.pojo.Emp;
import com.java.service.DeptService;
import com.java.service.EmpService;
import com.java.service.impl.DeptServiceImpl;
import com.java.service.impl.EmpServiceImpl;
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.List;
@WebServlet("/empList")
public class EmpListServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
EmpService es=new EmpServiceImpl();
DeptService ds=new DeptServiceImpl();
ListEmp allEmps=es.getAllEmps();
for (Emp emp:allEmps) {
Dept dept=ds.getDeptById(emp.getDeptNo());
emp.setDept(dept);
}
request.getSession().setAttribute("allEmp",allEmps);
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
package com.java.servlet;
import com.java.pojo.Emp;
import com.java.service.EmpService;
import com.java.service.impl.EmpServiceImpl;
import com.java.util.DateUtil;
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;
@WebServlet(name = "addEmpServlet")
public class AddEmpServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String empName=request.getParameter("empName");
String password=request.getParameter("password");
String birthday=request.getParameter("birthday");
int deptNo=Integer.parseInt(request.getParameter("deptNo"));
EmpService empService= new EmpServiceImpl();
int i=empService.addEmp(new Emp(empName,password, DateUtil.stringToDate(birthday),deptNo));
if (i0){
response.sendRedirect(request.getContextPath()+"/empList");
}else {
response.getWriter().println("添加员工失败");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
6.写主页 和添加页
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/79561.html