尚硅谷之JDBC

1.6 测试类

package com.atguigu.dao.test;

 

import java.util.List;

import java.util.Scanner;

 

import org.junit.Test;

 

import com.atguigu.bean.Department;

import com.atguigu.dao.DepartmentDAO;

import com.atguigu.dao.impl.basic.DepartmentDAOImplBasic;

import com.atguigu.dao.impl.original.DepartmentDAOImpl;

import com.atguigu.utils.CMUtility;

 

public class TestDepartmentDAO {

//  DepartmentDAO dao = new DepartmentDAOImpl();

    DepartmentDAO dao = new DepartmentDAOImplBasic();

 

    @Test

    public void addDepartment() {

        Scanner input = new Scanner(System.in);

        System.out.println("请输入部门名称:");

        String name = input.nextLine();

 

        System.out.println("请输入部门简介:");

        String description = input.nextLine();

 

        Department department = new Department(name, description);

 

        try {

            dao.addDepartment(department);

            System.out.println("添加成功");

        } catch (Exception e) {

            e.printStackTrace();

            System.out.println("添加失败");

        }

    }

 

    @Test

    public void getAllDepartment() throws Exception {

        List<Department> all = dao.getAll();

        for (Department department : all) {

            System.out.println(department);

        }

    }

 

    @Test

    public void updateDepartment() {

 

        try {

            getAllDepartment();

 

            Scanner input = new Scanner(System.in);

            System.out.println("请选择要修改的部门编号:");

            String did = input.nextLine();

 

            Department dept = dao.getById(did);

 

            System.out.println("请输入部门名称("+dept.getName()+"):");

            String name = CMUtility.readString(dept.getName());

 

            System.out.println("请输入部门简介("+dept.getDescription()+"):");

            String description = CMUtility.readString(dept.getDescription());

 

            Department department = new Department(dept.getId(),name, description);

 

            dao.updateDepartment(department);

            System.out.println("修改成功");

        } catch (Exception e) {

            e.printStackTrace();

            System.out.println("修改失败");

        }

    }

 

    @Test

    public void deleteDepartment() {

 

        try {

            getAllDepartment();

 

            Scanner input = new Scanner(System.in);

            System.out.println("请选择要删除的部门编号:");

            String did = input.nextLine();

 

            dao.deleteById(did);

            System.out.println("删除成功");

        } catch (Exception e) {

            e.printStackTrace();

            System.out.println("删除失败");

        }

    }

 

}

 

package com.atguigu.dao.test;

 

import java.util.Date;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Set;

 

import org.junit.Test;

 

import com.atguigu.bean.Employee;

import com.atguigu.dao.EmployeeDAO;

import com.atguigu.dao.impl.basic.EmployeeDAOImpl;

 

public class TestEmployeeDAO {

    EmployeeDAO ed = new EmployeeDAOImpl();

 

    @Test

    public void addEmployee()throws Exception{

        //省略键盘输入

        String ename = "张三";

        String tel = "10080";

        String gender = "男";

        double salary = 10000;

        double commissionPct = 0.3;

        Date birthday = new Date();

        Date hiredate = new Date();;

        int jobId = 2;

        String email="zhangsan@lina.com";

        int mid = 1;

        String address = "xx";

        String nativePlace = "xxx";

        int did = 2;

 

        Employee emp = new Employee(ename, tel, gender, salary, commissionPct, birthday, hiredate, jobId, email, mid, address, nativePlace, did);

        try {

            ed.addEmployee(emp);

            System.out.println("添加成功");

        } catch (Exception e) {

            e.printStackTrace();

            System.out.println("添加失败");

        }

    }

 

    @Test

    public void updateEmployee()throws Exception{

        //省略键盘输入

        String eid = "1";

        Employee emp = ed.getById(eid);

 

        //这里只演示修改一下,可以修改除了eid以外的所有项目

        emp.setSalary(emp.getSalary() + 1000);

 

        try {

            ed.updateEmployee(emp);

            System.out.println("修改成功");

        } catch (Exception e) {

            e.printStackTrace();

            System.out.println("修改失败");

        }

    }

 

    @Test

    public void deleteById()throws Exception{

        //省略键盘输入

        String eid = "26";

 

        try {

            ed.deleteById(eid);

            System.out.println("删除成功");

        } catch (Exception e) {

            e.printStackTrace();

            System.out.println("删除失败");

        }

 

    }

 

    @Test

    public void getAll()throws Exception{

        List<Employee> all = ed.getAll();

        for (Employee employee : all) {

            System.out.println(employee);

        }

    }

 

    @Test

    public void getAllPage()throws Exception{

        Long count = ed.getCount();

        System.out.println("总记录数:" + count);

        int pageSize = 5;

        System.out.println("每页显示5条");

        int page = 2;

        System.out.println("用户选择第" + page + "页");

 

        List<Employee> all = ed.getAll(page, pageSize);

        for (Employee employee : all) {

            System.out.println(employee);

        }

    }

 

    @Test

    public void getMaxSalary()throws Exception{

        Double maxSalary = ed.getMaxSalary();

        System.out.println("公司最高工资是: " + maxSalary);

    }

 

    @Test

    public void getAvgSalaryByDid()throws Exception{

        Map<Integer, Double> map = ed.getAvgSalaryByDid();

        Set<Entry<Integer, Double>> entrySet = map.entrySet();

        for (Entry<Integer, Double> entry : entrySet) {

            System.out.println(entry.getKey()+ ":" + entry.getValue());

        }

    }

}