web用户管理系统源代码.docx
《web用户管理系统源代码.docx》由会员分享,可在线阅读,更多相关《web用户管理系统源代码.docx(22页珍藏版)》请在冰豆网上搜索。
![web用户管理系统源代码.docx](https://file1.bdocx.com/fileroot1/2023-2/3/229012c5-b6f2-4c9c-95eb-fe3352f053c2/229012c5-b6f2-4c9c-95eb-fe3352f053c21.gif)
web用户管理系统源代码
//连接数据库代码
packagecom.tools;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;
publicclassDBUtil{
publicstaticConnectiongetConnection()throwsClassNotFoundException,SQLException{
//1.导入Jar包
//2.加载驱动
Class.forName("oracle.jdbc.driver.OracleDriver");
//3.获得数据库连接
Connectionconn=DriverManager.getConnection("jdbc:
oracle:
thin:
@localhost:
1521:
orcl","scott","tiger");
returnconn;
}
}
//EMP表的增删改查功能实现
packagecom.dao;
importjava.sql.Connection;
importjava.sql.Date;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.text.SimpleDateFormat;
importjava.util.ArrayList;
importcom.bean.Emp;
importcom.tools.DBUtil;
publicclassEmpDAO{
publicstaticvoidmain(String[]args)throwsClassNotFoundException,
SQLException{
EmpDAOdao=newEmpDAO();
dao.queryAll();
}
publicArrayListqueryAll()throwsClassNotFoundException,
SQLException{
Connectionconn=DBUtil.getConnection();
//4.创建Statement
Statements=conn.createStatement();
//5.执行SQL语句
Stringsql="select*fromemp";
//6.处理结果集
ResultSetrs=s.executeQuery(sql);
ArrayListlist=newArrayList();
while(rs.next()){
Empe=newEmp();
e.setEmpno(rs.getInt("empno"));
e.setEname(rs.getString("ename"));
e.setJob(rs.getString("job"));
e.setManager(rs.getInt("mgr"));
e.setSal(rs.getFloat("sal"));
e.setComm(rs.getFloat("comm"));
Datedate=rs.getDate("hiredate");
e.setHiredate(date.getYear()+1900+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日");
e.setDeptno(rs.getInt("deptno"));
list.add(e);
}
//7.关闭
rs.close();
s.close();
conn.close();
returnlist;
}
publicEmpfindById(intid)throwsClassNotFoundException,SQLException{
Connectionconn=DBUtil.getConnection();
//SQL语句生成
Stringsql="select*fromempwhereempno=?
";
PreparedStatementps=conn.prepareStatement(sql);
ps.setInt(1,id);
ResultSetrs=ps.executeQuery();
Empe=newEmp();
if(rs.next()){
e.setEmpno(rs.getInt("empno"));
e.setEname(rs.getString("ename"));
e.setJob(rs.getString("job"));
e.setManager(rs.getInt("mgr"));
e.setSal(rs.getFloat("sal"));
e.setComm(rs.getFloat("comm"));
Dated=rs.getDate("hiredate");
java.util.Datedd=newjava.util.Date(d.getTime());
Stringstr=dd.getYear()+1900+"-"+(dd.getMonth()+1)+"-"+dd.getDate();
e.setHiredate(str);
e.setDeptno(rs.getInt("deptno"));
}
//7.关闭
rs.close();
ps.close();
conn.close();
returne;
}
publicvoiddeleteById(intid)throwsClassNotFoundException,SQLException{
Connectionconn=DBUtil.getConnection();
Stringsql="deletefromempwhereempno=?
";
PreparedStatementps=conn.prepareStatement(sql);
ps.setInt(1,id);
ps.executeUpdate();
//7.关闭
ps.close();
conn.close();
}
publicvoidadd(Empe)throwsClassNotFoundException,SQLException{
Connectionconn=DBUtil.getConnection();
Stringsql="insertintoemp("
+"empno,"
+//员工编号
"ename,"
+"job,"
+"mgr,"
+"sal,"
+"comm,"
+"hiredate,"
+"deptno)"
+"values(SEQ_EMP_EMPNO.nextval,?
?
?
?
?
to_date(?
'YYYY-MM-DD'),?
)";
PreparedStatementps=conn.prepareStatement(sql);
ps.setString(1,e.getEname());
ps.setString(2,e.getJob());
ps.setInt(3,e.getManager());
ps.setFloat(4,e.getSal());
ps.setFloat(5,e.getComm());
ps.setString(6,e.getHiredate());
ps.setInt(7,e.getDeptno());
ps.executeUpdate();
//7.关闭
ps.close();
conn.close();
}
publicvoidupdate(Empe)throwsClassNotFoundException,SQLException{
Connectionconn=DBUtil.getConnection();
Stringsql="updateempsetename=?
job=?
mgr=?
sal=?
comm=?
hiredate=to_date(?
'YYYY-MM-DD'),deptno=?
whereempno=?
";
PreparedStatementps=conn.prepareStatement(sql);
ps.setString(1,e.getEname());
ps.setString(2,e.getJob());
ps.setInt(3,e.getManager());
ps.setFloat(4,e.getSal());
ps.setFloat(5,e.getComm());
ps.setString(6,e.getHiredate());
ps.setInt(7,e.getDeptno());
ps.setInt(8,e.getEmpno());
ps.executeUpdate();
//7.关闭
ps.close();
conn.close();
}
}
//EMP表的各种数据成员
packagecom.bean;
publicclassEmp{
privateintempno;
privateStringename;
privateStringjob;
privateintmanager;
privatefloatsal;
privatefloatcomm;
privateintdeptno;
privateStringhiredate;
publicintgetEmpno(){
returnempno;
}
publicvoidsetEmpno(intempno){
this.empno=empno;
}
publicStringgetEname(){
returnename;
}
publicvoidsetEname(Stringename){
this.ename=ename;
}
publicStringgetJob(){
returnjob;
}
publicvoidsetJob(Stringjob){
this.job=job;
}
publicintgetManager(){
returnmanager;
}
publicvoidsetManager(intmanager){
this.manager=manager;
}
publicfloatgetSal(){
returnsal;
}
publicvoidsetSal(floatsal){
this.sal=sal;
}
publicfloatgetComm(){
returncomm;
}
publicvoidsetComm(floatcomm){
m=comm;
}
publicintgetDeptno(){
returndeptno;
}
publicvoidsetDeptno(intdeptno){
this.deptno=deptno;
}
publicStringgetHiredate(){
returnhiredate;
}
publicvoidsetHiredate(Stringhiredate){
this.hiredate=hiredate;
}
}
//增加用户信息
packagecom;
importjava.io.IOException;
importjava.sql.SQLException;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcom.bean.Emp;
importcom.dao.EmpDAO;
publicclassAddEmpextendsHttpServlet{
/**
*Constructoroftheobject.
*/
publicAddEmp(){
super();
}
/**
*Destructionoftheservlet.
*/
publicvoiddestroy(){
super.destroy();//Justputs"destroy"stringinlog
//Putyourcodehere
}
/**
*ThedoGetmethodoftheservlet.
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
*
*@paramrequesttherequestsendbytheclienttotheserver
*@paramresponsetheresponsesendbytheservertotheclient
*@throwsServletExceptionifanerroroccurred
*@throwsIOExceptionifanerroroccurred
*/
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
//获得参数
Stringename=request.getParameter("ename");
Stringjob=request.getParameter("job");
Stringmgr=request.getParameter("mgr");
Stringsal=request.getParameter("sal");
Stringcomm=request.getParameter("comm");
Stringhiredate=request.getParameter("hiredate");
Stringdeptno=request.getParameter("deptno");
Empemp=newEmp();
emp.setEname(ename);
emp.setJob(job);
emp.setManager(Integer.parseInt(mgr));
emp.setSal(Float.parseFloat(sal));
emp.setComm(Float.parseFloat(comm));
emp.setHiredate(hiredate);
emp.setDeptno(Integer.parseInt(deptno));
//调用查询
EmpDAOdao=newEmpDAO();
try{
dao.add(emp);
}catch(NumberFormatExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
//设置参数
//重定向
response.sendRedirect("ListEmp");
}
/**
*ThedoPostmethodoftheservlet.
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.
*
*@paramrequesttherequestsendbytheclienttotheserver
*@paramresponsetheresponsesendbytheservertotheclient
*@throwsServletExceptionifanerroroccurred
*@throwsIOExceptionifanerroroccurred
*/
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doGet(request,response);
}
/**
*Initializationoftheservlet.
*
*@throwsServletExceptionifanerroroccurs
*/
publicvoidinit()throwsServletException{
//Putyourcodehere
}
}
//删除信息
packagecom;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.sql.SQLException;
importjavax.servlet.RequestDispatcher;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcom.bean.Emp;
importcom.dao.EmpDAO;
publicclassDeleteEmpextendsHttpServlet{
/**
*Constructoroftheobject.
*/
publicDeleteEmp(){
super();
}
/**
*Destructionoftheservlet.
*/
publicvoiddestroy(){
super.destroy();//Justputs"destroy"stringinlog
//Putyourcodehere
}
/**
*ThedoGetmethodoftheservlet.
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
*
*@paramrequesttherequestsendbytheclienttotheserver
*@paramresponsetheresponsesendbytheservertotheclient
*@throwsServletExceptionifanerroroccurred
*@throwsIOExceptionifanerroroccurred
*/
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
//获得参数
Stringempno=request.getParameter("empno");
//调用查询
EmpDAOdao=newEmpDAO();
try{
dao.deleteById(Integer.parseInt(empno));
}catch(NumberFormatExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(ClassNotFoundExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock