2019-04-01 配置db.properties、配置数据库地址、方法接口、继承并重写所有方法、SignInfo类、newSignInfo类

配置db.properties

#右键,属性-->UTF-8
#数据库配置文件name=value的形式配置
url=jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8
user=root
password=root

配置数据库地址

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
 * 1.节省资源 -- 单例
 * 2.开闭原则 -- 配置文件方式获取数据库信息
 * 3.获取链接 -- getConnection
 * 
 * 常见错误:
 *  ①配置文件必须在src下(classpath)
 *  ②配置信息中不要有多余的符号,例如""
 * @author Administrator
 */
public class DBUtils {
    private static DBUtils db;
    private String url_;
    private String user_;
    private String password_;
    private DBUtils() {
        try {
            // 加载配置文件信息
            Properties p = new Properties();
            p.load(this.getClass().getClassLoader().getResourceAsStream("db.properties"));
//          getProperty通过key值获取配置文件中的value值(返回值)
            url_ = p.getProperty("url");
            user_ = p.getProperty("user");
            password_ = p.getProperty("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static DBUtils getInstance() {
        if(db == null) {
            db = new DBUtils();
        }
        return db;
    }
    /**
     * 获取数据库连接的方法
     * @return 连接对象
     * @throws SQLException
     */
    public Connection getConnection() throws SQLException {
        Connection conn = DriverManager.getConnection(url_, user_, password_);
        return conn;
    }
}

方法接口

public interface EmpDao {
    // 增加一个员工
    void addEmp(String name); 
    // 根据empno删除员工
    int delEmp(int empno);
    // 根据empno修改员工姓名
    int updateEmp(Emp e);
    // 员工打卡
    int signIn(int empno);
    // 查询所有打卡信息
    List getSignInfo();
    // 查询所有打卡信息,包含姓名,工号,签到时间,签退时间
    // ① 再建一个实体类 ,包含姓名,工号,签到时间,签退时间
    List getNewSignInfo();
    // ② List中存Map
    List> getNewSignInfoTwo();
}

继承并重写所有方法

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.neuedu.jdbc3.DBUtils;
import com.neuedu.jdbc3.Emp;
import com.neuedu.jdbc3.NewSignInfo;
import com.neuedu.jdbc3.SignInfo;

public class EmpDaoImpl implements EmpDao{
    @Override
    public void addEmp(String name) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DBUtils.getInstance().getConnection();
            conn.setAutoCommit(false);
            ps = conn.prepareStatement("insert into emp(ename) values (?)");
            ps.setString(1, name);
            ps.executeUpdate();
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            if(conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public int delEmp(int empno) {
        Connection conn = null;
        PreparedStatement ps = null;
        int count = 0;
        try {
            conn = DBUtils.getInstance().getConnection();
            conn.setAutoCommit(false);
            ps = conn.prepareStatement("delete from emp where empno = ?");
            ps.setInt(1, empno);
            count = ps.executeUpdate();
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            if(conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return count;
    }

    @Override
    public int updateEmp(Emp e) {
        Connection conn = null;
        PreparedStatement ps = null;
        int count = 0;
        try {
            conn = DBUtils.getInstance().getConnection();
            conn.setAutoCommit(false);
            ps = conn.prepareStatement("update emp set ename = ? where empno = ?");
            // 从传入的对象当中获取编号和姓名
            ps.setString(1, e.getEname());
            ps.setInt(2, e.getEmpno());
            count = ps.executeUpdate();
            conn.commit();
        } catch (SQLException e2) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e2.printStackTrace();
        } finally {
            if(conn != null) {
                try {
                    conn.close();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                }
            }
            if(ps != null) {
                try {
                    ps.close();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                }
            }
        }
        return count;
    }

    @Override
    public int signIn(int empno) {
        /*
         * 1.查询empno工号员工是否存在(根据人数是0或1)
         * 2.1      不为0-- 打卡insert/签退update
         * 查询empno和当天打卡记录是否存在select count(id) idcounts from signinfo where empno = ? and date_format(signin,'%Y-%m-%d') = ?
         *  idcounts == 0 不存在 打卡insert
         *  idcounts != 0 存在打卡记录update
         * 2.2      为0-- 提示不能打卡
         * 
         * 常见错误:
         *  ①函数或表达式的列必须起别名
         *  ②yyyy-MM-dd 对应   %Y-%m-%d  
         *  ③date_format(signin,'%Y-%m-%d') = ?  错写成  date_format(signin,'%Y-%m-%d' = ?) 
         */
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        try {
            conn = DBUtils.getInstance().getConnection();
            // 1.查询工号为empno的人数,根据人数是0或1就能判断该员工是否可以打卡
            ps = conn.prepareStatement("select count(empno) counts from emp where empno = ?");
            ps.setInt(1, empno);
            rs = ps.executeQuery();
            if(rs.next()) {
                count = rs.getInt("counts");
                // 2.count不为0,代表有该工号员工,可以打卡/签退
                if(count != 0) {
                    // 判断是打卡【insert】还是签退【update】
                    // 先查询signinfo表中是否有当天打卡记录
                    Date curDate = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String strDate = sdf.format(curDate);
                    // 3.查询该工号员工,当天是否有打卡记录
                    ps = conn.prepareStatement("select count(id) idcounts from signinfo where empno = ? and date_format(signin,'%Y-%m-%d') = ?");
                    ps.setInt(1, empno);
                    ps.setString(2, strDate);
                    rs = ps.executeQuery();
                    if(rs.next()) {
                        int idcounts = rs.getInt("idcounts");
                        conn.setAutoCommit(false);
                        if(idcounts == 0) {
                            // idcounts是0,表示今天没打过卡,本次操作就是正常打卡【insert】
                            ps = conn.prepareStatement("insert into signinfo(empno,signin) values (?,?)");
                            ps.setInt(1, empno);
                            Timestamp ts = new Timestamp(curDate.getTime());
                            ps.setTimestamp(2, ts);
                            System.out.println(empno+" 号员工签到成功");
                        }else {
                            // 非0,表示今天打过卡,本次操作就是签退【update】
                            ps = conn.prepareStatement("update signinfo set signout = ? where empno = ? and date_format(signin,'%Y-%m-%d') = ?");
                            Timestamp ts = new Timestamp(curDate.getTime());
                            ps.setTimestamp(1, ts);
                            ps.setInt(2, empno);
                            ps.setString(3, strDate);
                            System.out.println(empno+" 号员工签退成功");
                        }
                        ps.executeUpdate();
                        conn.commit();
                    }
                }else {
                    System.out.println("抱歉,您没有录入信息,不能打卡");
                }
            }
        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            if(conn != null) {
                try {
                    conn.close();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                }
            }
            if(ps != null) {
                try {
                    ps.close();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                }
            }
            if(rs != null) {
                try {
                    rs.close();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                }
            }
        }
        return count;
    }
    @Override
    public List getSignInfo() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        // 打卡名单
        List list = new ArrayList();
        try {
            conn = DBUtils.getInstance().getConnection();
            ps = conn.prepareStatement("select * from signinfo");
            rs = ps.executeQuery();
            while(rs.next()) {
                int id = rs.getInt("id");
                int empno = rs.getInt("empno");
                Date signin = rs.getTimestamp("signin");
                Date signout = rs.getTimestamp("signout");
                // 封装信息
                SignInfo info = new SignInfo();
                info.setId(id);
                info.setEmpno(empno);
                info.setSignin(signin);
                info.setSignout(signout);
                // 扔进list
                list.add(info);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(conn != null) {
                try {
                    conn.close();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                }
            }
            if(ps != null) {
                try {
                    ps.close();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                }
            }
            if(rs != null) {
                try {
                    rs.close();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                }
            }
        }
        return list;
    }
    
    @Override
    public List getNewSignInfo() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        // 打卡名单
        List list = new ArrayList();
        try {
            conn = DBUtils.getInstance().getConnection();
            ps = conn.prepareStatement("select e.empno,ename,signin,signout from signinfo s join emp e on e.empno = s.empno");
            rs = ps.executeQuery();
            while(rs.next()) {
                int empno = rs.getInt("empno");
                String ename = rs.getString("ename");
                Date signin = rs.getTimestamp("signin");
                Date signout = rs.getTimestamp("signout");
                // 封装信息
                NewSignInfo info = new NewSignInfo();
                info.setEname(ename);
                info.setEmpno(empno);
                info.setSignIn(signin);
                info.setSignOut(signout);
                // 扔进list
                list.add(info);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(conn != null) {
                try {
                    conn.close();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                }
            }
            if(ps != null) {
                try {
                    ps.close();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                }
            }
            if(rs != null) {
                try {
                    rs.close();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                }
            }
        }
        return list;
    }
    @Override
    public List> getNewSignInfoTwo() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        // 打卡名单
        List> list = new ArrayList<>();
        try {
            conn = DBUtils.getInstance().getConnection();
            ps = conn.prepareStatement("select e.empno,ename,signin,signout from signinfo s join emp e on e.empno = s.empno");
            rs = ps.executeQuery();
            while(rs.next()) {
                int empno = rs.getInt("empno");
                String ename = rs.getString("ename");
                Date signin = rs.getTimestamp("signin");
                Date signout = rs.getTimestamp("signout");
                // 封装信息
                Map map = new HashMap();
                map.put("empno", empno);
                map.put("ename", ename);
                map.put("signin", signin);
                map.put("signout", signout);
                // 扔进list
                list.add(map);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(conn != null) {
                try {
                    conn.close();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                }
            }
            if(ps != null) {
                try {
                    ps.close();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                }
            }
            if(rs != null) {
                try {
                    rs.close();
                } catch (SQLException e3) {
                    e3.printStackTrace();
                }
            }
        }
        return list;
    }
}

SignInfo类

import java.util.Date;

public class SignInfo {
    private int id;
    private int empno;
    private Date signin;
    private Date signout;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getEmpno() {
        return empno;
    }
    public void setEmpno(int empno) {
        this.empno = empno;
    }
    public Date getSignin() {
        return signin;
    }
    public void setSignin(Date signin) {
        this.signin = signin;
    }
    public Date getSignout() {
        return signout;
    }
    public void setSignout(Date signout) {
        this.signout = signout;
    }
    
}

newSignInfo类

import java.util.Date;

public class newSignInfo {
    private int empno;
    private String ename;
    private Date signIn;
    private Date signOut;
    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;
    }
    public Date getSignIn() {
        return signIn;
    }
    public void setSignIn(Date signIn) {
        this.signIn = signIn;
    }
    public Date getSignOut() {
        return signOut;
    }
    public void setSignOut(Date signOut) {
        this.signOut = signOut;
    }
    
    
}

你可能感兴趣的:(2019-04-01 配置db.properties、配置数据库地址、方法接口、继承并重写所有方法、SignInfo类、newSignInfo类)