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“);// 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 java.sql.Statement;import java.text.SimpleDateat;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 ClassNotFoundException,SQLException {Connection conn = DBUtil.getConnection();// 4. 创建 StatementStatement s = conn.createStatement();// 5. 执行 SQL 语句String sql = “select * from emp“;// 6. 处理结果集ResultSet rs = s.cuteQuery(sql);ArrayList list = new ArrayList();while (rs.next()) {Emp e = new Emp();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 date = 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();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.prepareStatement(sql);ps.setInt(1, id);ResultSet rs = ps.cuteQuery();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(“hiredate“);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 ClassNotFoundException, SQLException {Connection conn = DBUtil.getConnection();String sql = “delete from emp where empno=?“;PreparedStatement ps = conn.prepareStatement(sql);ps.setInt(1, id);ps.cuteUpdate();// 7. 关闭ps.close();conn.close();}public void add(Emp e) throws ClassNotFoundException, SQLException {Connection 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