1、web用户管理系统源代码/连接数据库代码package com.tools;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;public class DBUtil public static Connection getConnection() throws ClassNotFoundException, SQLException / 1. 导入Jar包 / 2. 加载驱动 Class.forName(oracle.jdbc.driver.OracleDriver); /
2、 3. 获得数据库连接 Connection conn = DriverManager.getConnection(jdbc:oracle:thin:localhost:1521:orcl, scott, tiger); return conn; /EMP表的增删改查功能实现package com.dao;import java.sql.Connection;import java.sql.Date;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import ja
3、va.sql.Statement;import java.text.SimpleDateFormat;import java.util.ArrayList;import com.bean.Emp;import com.tools.DBUtil;public class EmpDAO public static void main(String args) throws ClassNotFoundException, SQLException EmpDAO dao = new EmpDAO(); dao.queryAll(); public ArrayList queryAll() throws
4、 ClassNotFoundException, SQLException Connection conn = DBUtil.getConnection(); / 4. 创建Statement Statement s = conn.createStatement(); / 5. 执行SQL语句 String sql = select * from emp; / 6. 处理结果集 ResultSet rs = s.executeQuery(sql); ArrayList list = new ArrayList(); while (rs.next() Emp e = new Emp(); e.s
5、etEmpno(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); Date date = rs.getDate(hiredate); e.setHiredate(date.getYear() + 1900 + 年 + (date.getMonth() + 1) + 月 + date.getDate() + 日); e.se
6、tDeptno(rs.getInt(deptno); list.add(e); / 7. 关闭 rs.close(); s.close(); conn.close(); return list; public Emp findById(int id) throws ClassNotFoundException, SQLException Connection conn = DBUtil.getConnection(); / SQL语句生成 String sql = select * from emp where empno=?; PreparedStatement ps = conn.prep
7、areStatement(sql); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); Emp e = new Emp(); 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); Date d = rs.getDate(hi
8、redate); java.util.Date dd = new java.util.Date(d.getTime(); String str = dd.getYear() + 1900 + - + (dd.getMonth() + 1) + - + dd.getDate(); e.setHiredate(str); e.setDeptno(rs.getInt(deptno); / 7. 关闭 rs.close(); ps.close(); conn.close(); return e; public void deleteById(int id) throws ClassNotFoundEx
9、ception, SQLException Connection conn = DBUtil.getConnection(); String sql = delete from emp where empno=?; PreparedStatement ps = conn.prepareStatement(sql); ps.setInt(1, id); ps.executeUpdate(); / 7. 关闭 ps.close(); conn.close(); public void add(Emp e) throws ClassNotFoundException, SQLException Co
10、nnection conn = DBUtil.getConnection(); String sql = insert into emp ( + empno, + / 员工编号 ename, + job, + mgr, + sal, + comm, + hiredate, + deptno) + values (SEQ_EMP_EMPNO.nextval, ?, ?, ?, ?, ?, to_date(?, YYYY-MM-DD), ?); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, e.getEname
11、(); 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(); public void update(Emp e) throws ClassNotFoundException, SQLException
12、 Connection conn = DBUtil.getConnection(); String sql = update emp set ename=?, job=?, mgr=?, sal=?, comm=?, hiredate=to_date(?, YYYY-MM-DD), deptno=? where empno=?; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, e.getEname(); ps.setString(2, e.getJob(); ps.setInt(3, e.getManager
13、(); 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表的各种数据成员package com.bean;public class Emp private int empno; private String ename; private Stri
14、ng job; private int manager; private float sal; private float comm; private int deptno; private String hiredate; public int getEmpno() return empno; public void setEmpno(int empno) this.empno = empno; public String getEname() return ename; public void setEname(String ename) this.ename = ename; publi
15、c String getJob() return job; public void setJob(String job) this.job = job; public int getManager() return manager; public void setManager(int manager) this.manager = manager; public float getSal() return sal; public void setSal(float sal) this.sal = sal; public float getComm() return comm; public
16、void setComm(float comm) m = comm; public int getDeptno() return deptno; public void setDeptno(int deptno) this.deptno = deptno; public String getHiredate() return hiredate; public void setHiredate(String hiredate) this.hiredate = hiredate; /增加用户信息package com;import java.io.IOException;import java.s
17、ql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.bean.Emp;import com.dao.EmpDAO;public class AddEmp extends HttpServlet /* * Constructor of the object. */
18、public AddEmp() super(); /* * Destruction of the servlet. */ public void destroy() super.destroy(); / Just puts destroy string in log / Put your code here /* * The doGet method of the servlet. * * This method is called when a form has its tag value method equals to get. * * param request the request
19、 send by the client to the server * param response the response send by the server to the client * throws ServletException if an error occurred * throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOExceptio
20、n / 获得参数 String ename = request.getParameter(ename); String job = request.getParameter(job); String mgr = request.getParameter(mgr); String sal = request.getParameter(sal); String comm = request.getParameter(comm); String hiredate = request.getParameter(hiredate); String deptno = request.getParamete
21、r(deptno); Emp emp = new Emp(); 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); / 调用查询 EmpDAO dao = new EmpDAO(); try dao.add(emp); cat
22、ch (NumberFormatException e) / TODO Auto-generated catch block e.printStackTrace(); catch (ClassNotFoundException e) / TODO Auto-generated catch block e.printStackTrace(); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); / 设置参数 / 重定向 response.sendRedirect(ListEmp); /* *
23、The doPost method of the servlet. * * This method is called when a form has its tag value method equals to post. * * param request the request send by the client to the server * param response the response send by the server to the client * throws ServletException if an error occurred * throws IOExc
24、eption if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException doGet(request, response); /* * Initialization of the servlet. * * throws ServletException if an error occurs */ public void init() throws ServletException
25、/ Put your code here /删除信息package com;import java.io.IOException;import java.io.PrintWriter;import java.sql.SQLException;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servle
26、t.http.HttpServletResponse;import com.bean.Emp;import com.dao.EmpDAO;public class DeleteEmp extends HttpServlet /* * Constructor of the object. */ public DeleteEmp() super(); /* * Destruction of the servlet. */ public void destroy() super.destroy(); / Just puts destroy string in log / Put your code
27、here /* * The doGet method of the servlet. * * This method is called when a form has its tag value method equals to get. * * param request the request send by the client to the server * param response the response send by the server to the client * throws ServletException if an error occurred * thro
28、ws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException / 获得参数 String empno = request.getParameter(empno); / 调用查询 EmpDAO dao = new EmpDAO(); try dao.deleteById(Integer.parseInt(empno); catch (NumberFormatException e) / TODO Auto-generated catch block e.printStackTrace(); catch (ClassNotFoundException e) / TODO Auto-generated catch block e.printStackTrace(); catch (SQLException e) / TODO Auto-generated catch block
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1