实训日志——第二天

数据访问接口实现类CollegeDaoImpl


/**
 * 包名:net.hw.student.dao.impl
 * 类名:CollegeDaoImpl
 * 描述:学校数据访问接口实现类  
 * 作者:向铭涵
 * 日期:2019年6月19日  
 */
package net.xmh.student.bean.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import net.xmh.student.bean.College;
import net.xmh.student.bean.dao.impl.CollegeDao;
import net.xmh.student.bean.dbutil.ConnectionManager;

public class CollegeDaoImpl implements CollegeDao {

    @Override
    public College findById(int id) {
        // 声明学校对象
        College college = null;

        // 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "SELECT * FROM t_college WHERE id = ?";
        try {
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setInt(1, id);
            // 执行SQL查询,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 判断结果集是否有记录
            if (rs.next()) {
                // 实例化学校
                college = new College();
                // 利用当前记录字段值去设置学校对象的属性
                college.setId(rs.getInt("id"));
                college.setName(rs.getString("name"));
                college.setPresident(rs.getString("president"));
                college.setStartTime(rs.getDate("start_time"));
                college.setTelephone(rs.getString("telephone"));
                college.setEmail(rs.getString("email"));
                college.setAddress(rs.getString("address"));
                college.setProfile(rs.getString("profile"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }

        // 返回学校对象
        return college;
    }

    @Override
    public int update(College college) {
        // 定义更新记录数
        int count = 0;

        // 获得数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 定义SQL字符串
        String strSQL = "UPDATE t_college SET name = ?, president = ?, start_time = ?,"
                + " telephone = ?, email = ?, address = ?, profile = ? WHERE id = ?";
        try {
            // 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 设置占位符的值
            pstmt.setString(1, college.getName());
            pstmt.setString(2, college.getPresident());
            pstmt.setTimestamp(3, new Timestamp(college.getStartTime().getTime()));
            pstmt.setString(4, college.getTelephone());
            pstmt.setString(5, college.getEmail());
            pstmt.setString(6, college.getAddress());
            pstmt.setString(7, college.getProfile());
            pstmt.setInt(8, college.getId());
            // 执行更新操作,更新记录
            count = pstmt.executeUpdate();
            // 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }

        // 返回更新记录数
        return count;
    }
}

创建net.xmh.student.test包,在里面创建测试类TestCollegeDaoImpl

package net.xmh.student.bean.test;
import net.xmh.student.bean.College;
import net.xmh.student.bean.dao.impl.CollegeDao;
import net.xmh.student.bean.dao.CollegeDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * 功能:测试学校数据访问接口实现类
 * 作者:向铭涵
 * 日期:2019年6月17日
 */
public class testCollegeDaoImpl {
    @Before
    public void beforeTest( ){
        System.out.println("单元测试开始!");
    }
    @Test
    public void testFindByID() {
        CollegeDao dao = new CollegeDaoImpl();
        College college =dao.findById(1);
        System.out.println(college);
    }
    @Test
    public void testUpdate(){
        CollegeDao dao= new CollegeDaoImpl();
        College college = dao.findById(1);
        college.setPresident("王洪礼");
        dao.update(college);
        college = dao.findById(1);
        System.out.println(college);
    }
    @After
    public void afterTest(){
        System.out.println("单元测试结束!");
    }
}

运行结果为:
在这里插入图片描述
在这里插入图片描述
状态数据访问接口实现类StatusDaoImpl

package net.xmh.student.bean.dao;

import net.xmh.student.bean.Status;
import net.xmh.student.bean.dbutil.ConnectionManager;
import net.xmh.student.bean.dao.impl.StatusDao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 包名:met.xmh.student.dao.impl
 * 类名:StatusDaoImpl
 * 描述:状态数据访问接口实现类
 * 作者:向铭涵
 * 日期:2019年6月18日
 */
public class StatusDaoImpl implements StatusDao {
    @Override
    public  Status findById(int id) {
        //声明状态对象
        Status status = null;
        //获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        //定义SQL字符串
        String strSQL = "select * from t_status where id = ?";
        try {
            //创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            //设置占位符的值
            pstmt.setInt(1, id);
            //执行SQL查询,返回结果集
            ResultSet rs = pstmt.executeQuery();
            //判断结果集是否有记录
            if (rs.next()) {
                //实例化状态
                status = new Status();
                //利用当前记录字段值去设置状态对象的属性
                status.setId(rs.getInt("id"));
                status.setCollege(rs.getString("college"));
                status.setVersion(rs.getString("version"));
                status.setAuthor(rs.getString("author"));
                status.setTelephone(rs.getString("telephone"));
                status.setAddress(rs.getString("address"));
                status.setEmail(rs.getString("email"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectionManager.closeConnection(conn);
        }
        //返回状态对象
        return status;
    }
    @Override
    public int update(Status status){
        //定义更新记录数
        int count = 0;
        //获得数据库连接
        Connection conn = ConnectionManager.getConnection();
        //定义SQL字符串
        String strSQL = "update t_status set college = ?,version = ?,author = ?,"
                +"telephone = ?,address = ?,email =? where id +?";
        try{
            //创建预备语句对象
            PreparedStatement pstmt =conn.prepareStatement(strSQL);
            //设置占位符的值
            pstmt.setString(1,status.getCollege());
            pstmt.setString(2,status.getVersion());
            pstmt.setString(3,status.getAuthor());
            pstmt.setString(4,status.getTelephone());
            pstmt.setString(5,status.getAddress());
            pstmt.setString(6,status.getEmail());
            pstmt.setInt(7,status.getId());
            //执行更新操作,更新记录
            count = pstmt.executeUpdate();
            //关闭预备语句对象
            pstmt.close();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            ConnectionManager.closeConnection(conn);
        }
        //返回更新记录
        return count;
    }
}

在net.xmh.student.test包里创建测试类TestStatusDaoImpl

package net.xmh.student.bean.test;

import net.xmh.student.bean.Status;
import net.xmh.student.bean.dao.StatusDaoImpl;
import net.xmh.student.bean.dao.impl.StatusDao;
import org.junit.Test;

import javax.swing.plaf.nimbus.State;

/**
 * 功能:测试状态数据访问接口实现类
 * 作者:向铭涵
 * 日期:2019年6月18日
 */
public class testStatusDaoImpl {
    @Test
    public void testFindById(){
        StatusDao dao = new StatusDaoImpl();
        Status status = dao.findById(1);
        System.out.println(status);
    }
    @Test
    public void testUpdate(){
        StatusDao dao = new StatusDaoImpl();
        Status status = dao.findById(1);
        status.setAuthor("嘻嘻");
        int count = dao.update(status);
        if (count > 0){
            System.out.println("更新成功!");
        }else{
            System.out.println("更新失败!");}
        }
    }

运行结果为:
在这里插入图片描述
在这里插入图片描述
学生数据访问接口实现类StudentDaoImpl

package net.xmh.student.bean.dao;

import net.xmh.student.bean.Student;
import net.xmh.student.bean.dao.impl.StudentDao;
import net.xmh.student.bean.dbutil.ConnectionManager;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * 功能:学生数据访问接口实现类
 * 作者:华卫
 * 日期:2019年6月18日
 */
public class StudentDaoImpl implements StudentDao {
    /**
     * 插入学生记录
     *
     * @param student
     * @return 插入记录数
     */
    @Override
    public int insert(Student student) {
        // 定义插入记录数
        int count = 0;

        // 1. 获得数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "insert into t_student (id, name, sex, age, department, class, telephone)"
                + " values (?, ?, ?, ?, ?, ?, ?)";
        try {
            // 3. 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4. 设置占位符的值
            pstmt.setString(1, student.getId());
            pstmt.setString(2, student.getName());
            pstmt.setString(3, student.getSex());
            pstmt.setInt(4, student.getAge());
            pstmt.setString(5, student.getDepartment());
            pstmt.setString(6, student.getClazz());
            pstmt.setString(7, student.getTelephone());
            // 5. 执行SQL,返回插入记录数
            count = pstmt.executeUpdate();
            // 6. 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回插入记录数
        return count;
    }

    /**
     * 按学号删除学生记录
     *
     * @param id
     * @return 删除记录数
     */
    @Override
    public int deleteById(String id) {
        // 定义删除记录数
        int count = 0;

        // 1. 获取数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "delete from t_student where id = ?";
        try {
            // 3. 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4. 设置占位符的值
            pstmt.setString(1, id);
            // 5. 执行SQL,返回删除记录数
            count = pstmt.executeUpdate();
            // 6. 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回删除记录数
        return count;
    }

    /**
     * 按班级删除学生记录
     *
     * @param clazz
     * @return 删除记录数
     */
    @Override
    public int deleteByClass(String clazz) {
        // 定义删除记录数
        int count = 0;

        // 1. 获取数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "delete from t_student where class = ?";
        try {
            // 3. 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4. 设置占位符的值
            pstmt.setString(1, clazz);
            // 5. 执行SQL,返回删除记录数
            count = pstmt.executeUpdate();
            // 6. 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回删除记录数
        return count;
    }

    /**
     * 按系部删除学生记录
     *
     * @param department
     * @return 删除记录数
     */
    @Override
    public int deleteByDepartment(String department) {
        // 定义删除记录数
        int count = 0;

        // 1. 获得数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "delete from t_student where department = ?";
        try {
            // 3. 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4. 设置占位符的值
            pstmt.setString(1, department);
            // 5. 执行SQL,返回删除记录数
            count = pstmt.executeUpdate();
            // 6. 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回删除记录数
        return count;
    }

    /**
     * 更新学生记录
     *
     * @param student
     * @return 更新记录数
     */
    @Override
    public int update(Student student) {
        // 定义更新记录数
        int count = 0;

        // 1. 获得数据库连接
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "update t_student set name = ?, sex = ?, age = ?,"
                + " department = ?, class = ?, telephone = ? where id = ?";
        try {
            // 3. 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4. 设置占位符的值
            pstmt.setString(1, student.getName());
            pstmt.setString(2, student.getSex());
            pstmt.setInt(3, student.getAge());
            pstmt.setString(4, student.getDepartment());
            pstmt.setString(5, student.getClazz());
            pstmt.setString(6, student.getTelephone());
            pstmt.setString(7, student.getId());
            // 5. 执行SQL,返回更新记录数
            count = pstmt.executeUpdate();
            // 6. 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回更新记录数
        return count;
    }

    /**
     * 按学号查询学生记录
     *
     * @param id
     * @return 学生实体
     */
    @Override
    public Student findById(String id) {
        // 声明学生对象
        Student student = null;

        // 1. 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "select * from t_student where id = ?";
        try {
            // 3. 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4. 设置占位符的值
            pstmt.setString(1, id);
            // 5. 执行SQL,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 6. 判断结果集是否有记录
            if (rs.next()) {
                // 创建学生实体
                student = new Student();
                // 利用当前记录各字段值设置学生实体属性
                student.setId(rs.getString("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getInt("age"));
                student.setDepartment(rs.getString("department"));
                student.setClazz(rs.getString("class"));
                student.setTelephone(rs.getString("telephone"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回学生对象
        return student;
    }

    /**
     * 按姓名查询学生记录
     *
     * @param name
     * @return 学生列表
     */
    @Override
    public List<Student> findByName(String name) {
        // 声明学生列表
        List<Student> students = new ArrayList<Student>();

        // 1. 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "select * from t_student where name like ?";
        try {
            // 3. 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4. 设置占位符的值
            pstmt.setString(1, name + "%");
            // 5. 执行SQL,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 6. 遍历结果集
            while (rs.next()) {
                // 创建学生实体
                Student student = new Student();
                // 利用当前记录各字段值设置学生实体属性
                student.setId(rs.getString("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getInt("age"));
                student.setDepartment(rs.getString("department"));
                student.setClazz(rs.getString("class"));
                student.setTelephone(rs.getString("telephone"));
                // 将实体添加到学生列表
                students.add(student);
            }
            // 7. 关闭结果集
            rs.close();
            // 8. 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回学生列表
        return students;
    }

    /**
     * 按班级查询学生记录
     *
     * @param clazz
     * @return 学生列表
     */
    @Override
    public List<Student> findByClass(String clazz) {
        // 声明学生列表
        List<Student> students = new ArrayList<Student>();

        // 1. 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "select * from t_student where class like ?";
        try {
            // 3. 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4. 设置占位符的值
            pstmt.setString(1, clazz + "%");
            // 5. 执行SQL,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 6. 遍历结果集
            while (rs.next()) {
                // 创建学生实体
                Student student = new Student();
                // 利用当前记录各字段值设置学生实体属性
                student.setId(rs.getString("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getInt("age"));
                student.setDepartment(rs.getString("department"));
                student.setClazz(rs.getString("class"));
                student.setTelephone(rs.getString("telephone"));
                // 将实体添加到学生列表
                students.add(student);
            }
            // 7. 关闭结果集
            rs.close();
            // 8. 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回学生列表
        return students;
    }

    /**
     * 按系部查询学生记录
     *
     * @param department
     * @return 学生列表
     */
    @Override
    public List<Student> findByDepartment(String department) {
        // 声明学生列表
        List<Student> students = new ArrayList<Student>();

        // 1. 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "select * from t_student where department like ?";
        try {
            // 3. 创建预备语句对象
            PreparedStatement pstmt = conn.prepareStatement(strSQL);
            // 4. 设置占位符的值
            pstmt.setString(1, department + "%");
            // 5. 执行SQL,返回结果集
            ResultSet rs = pstmt.executeQuery();
            // 6. 遍历结果集
            while (rs.next()) {
                // 创建学生实体
                Student student = new Student();
                // 利用当前记录各字段值设置学生实体属性
                student.setId(rs.getString("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getInt("age"));
                student.setDepartment(rs.getString("department"));
                student.setClazz(rs.getString("class"));
                student.setTelephone(rs.getString("telephone"));
                // 将实体添加到学生列表
                students.add(student);
            }
            // 7. 关闭结果集
            rs.close();
            // 8. 关闭预备语句对象
            pstmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回学生列表
        return students;
    }

    /**
     * 查询全部学生记录
     *
     * @return 学生列表
     */
    @Override
    public List<Student> findAll() {
        // 声明学生列表
        List<Student> students = new ArrayList<Student>();

        // 1. 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "select * from t_student";
        try {
            // 3. 创建语句对象
            Statement stmt = conn.createStatement();
            // 4. 执行SQL,返回结果集
            ResultSet rs = stmt.executeQuery(strSQL);
            // 5. 遍历结果集
            while (rs.next()) {
                // 创建学生实体
                Student student = new Student();
                // 利用当前记录各字段值设置学生实体属性
                student.setId(rs.getString("id"));
                student.setName(rs.getString("name"));
                student.setSex(rs.getString("sex"));
                student.setAge(rs.getInt("age"));
                student.setDepartment(rs.getString("department"));
                student.setClazz(rs.getString("class"));
                student.setTelephone(rs.getString("telephone"));
                // 将实体添加到学生列表
                students.add(student);
            }
            // 6. 关闭结果集
            rs.close();
            // 7. 关闭语句对象
            stmt.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回学生列表
        return students;
    }

    /**
     * 按性别统计学生人数
     *
     * @return 统计结果向量
     */
    @Override
    public Vector findRowsBySex() {
        // 定义行集向量
        Vector rows = new Vector();

        // 1. 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "select sex as '性别', count(*) as '人数'"
                + " from t_student group by sex order by sex desc";
        try {
            // 3. 创建语句对象
            Statement stmt = conn.createStatement();
            // 4. 执行SQL,返回结果集
            ResultSet rs = stmt.executeQuery(strSQL);
            // 5. 遍历结果集
            while (rs.next()) {
                // 定义当前行向量
                Vector<String> currentRow = new Vector();
                // 利用当前记录字段值设置当前行向量的元素值
                currentRow.addElement(rs.getString("性别"));
                currentRow.addElement(rs.getInt("人数") + "");
                // 将当前行向量添加到行集向量
                rows.addElement(currentRow);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回行集向量
        return rows;
    }

    /**
     * 按班级统计学生人数
     *
     * @return 统计结果向量
     */
    @Override
    public Vector findRowsByClass() {
        // 定义行集向量
        Vector rows = new Vector();

        // 1. 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "select class as '班级', count(*) as '人数'"
                + " from t_student group by class order by class desc";
        try {
            // 3. 创建语句对象
            Statement stmt = conn.createStatement();
            // 4. 执行SQL,返回结果集
            ResultSet rs = stmt.executeQuery(strSQL);
            // 5. 遍历结果集
            while (rs.next()) {
                // 定义当前行向量
                Vector<String> currentRow = new Vector();
                // 利用当前记录字段值设置当前行向量的元素值
                currentRow.addElement(rs.getString("班级"));
                currentRow.addElement(rs.getInt("人数") + "");
                // 将当前行向量添加到行集向量
                rows.addElement(currentRow);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回行集向量
        return rows;
    }

    /**
     * 按系部统计学生人数
     *
     * @return 统计结果向量
     */
    @Override
    public Vector findRowsByDepartment() {
        // 定义行集向量
        Vector rows = new Vector();

        // 1. 获取数据库连接对象
        Connection conn = ConnectionManager.getConnection();
        // 2. 定义SQL字符串
        String strSQL = "select department as '系部', count(*) as '人数'"
                + " from t_student group by department order by department desc";
        try {
            // 3. 创建语句对象
            Statement stmt = conn.createStatement();
            // 4. 执行SQL,返回结果集
            ResultSet rs = stmt.executeQuery(strSQL);
            // 5. 遍历结果集
            while (rs.next()) {
                // 定义当前行向量
                Vector<String> currentRow = new Vector();
                // 利用当前记录字段值设置当前行向量的元素值
                currentRow.addElement(rs.getString("系部"));
                currentRow.addElement(rs.getInt("人数") + "");
                // 将当前行向量添加到行集向量
                rows.addElement(currentRow);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭数据库连接
            ConnectionManager.closeConnection(conn);
        }

        // 返回行集向量
        return rows;
    }
}

在net.xmh.student.test包里创建测试类TestStudentDaoImpl

package net.xmh.student.bean.test;

import net.xmh.student.bean.Student;
import net.xmh.student.bean.dao.StudentDaoImpl;
import net.xmh.student.bean.dao.impl.StudentDao;
import org.junit.Test;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

/**
 * 功能:测试学生数据访问接口实现类
 * 作者:向铭涵
 * 日期:2019年6月17日
 */
public class TestStudentDaoImpl {
    @Test
    public void testInsert(){
        Student student = new Student();
        student.setId("12345678");
        student.setName("琳琳");
        student.setSex("女");
        student.setAge(19);
        student.setDepartment("信息工程系");
        student.setClazz("18大数据1班");
        student.setTelephone("78927657265");

        StudentDao dao = new StudentDaoImpl();
        int count = dao.insert(student);
        if (count > 0){
            System.out.println("恭喜,学生记录插入成功!");
        }else {
            System.out.println("遗憾,学生记录插入失败!");
        }
    }
    @Test
    public void testDeleteById(){
        StudentDao dao = new StudentDaoImpl();
        String id = "12345678";
        int count = dao.deleteById(id);
        if (count > 0 ){
            System.out.println("恭喜,学生记录删除成功!");
        }else {
            System.out.println("遗憾,学生记录删除失败!");
        }
    }
    @Test
    public void testDeleteByClass(){
        StudentDao dao = new StudentDaoImpl();
        String clazz = "11英教1班";
        int count = dao.deleteByClass(clazz);
        if (count > 0){
            System.out.println("恭喜,[" +clazz +"]学生记录删除成功!");
        }else {
            System.out.println("遗憾,[" + clazz+"]学生记录删除失败!");
        }
    }
    @Test
    public void deleteByDepartment(){
        StudentDao dao = new StudentDaoImpl();
        String department = "信息工程系";
        int count = dao.deleteByDepartment(department);
        if (count > 0){
            System.out.println("恭喜,[" +department+"]学生记录删除成功!");
        }else {
            System.out.println("遗憾,[" +department+"]学生记录删除失败!");
        }
    }
    @Test
    public void testUpdate() {
        StudentDao dao = new StudentDaoImpl();
        Student student = dao.findById("10080301");
        student.setName("圆圆");
        int count = dao.update(student);
        if (count > 0){
            System.out.println("更新成功!");
        }else{
            System.out.println("更新失败!");
        }
    }
    @Test
    public void testFindByName(){
        StudentDao dao = new StudentDaoImpl();
        String name = "李";
        List<Student> students = dao.findByName(name);
        for (Student student:students){
            System.out.println(student);
        }
    }
    @Test
    public void testFindById(){
        StudentDao dao = new StudentDaoImpl();
        Student student = dao.findById("10080301");
        System.out.println(student);
        }
    @Test
    public void testFindByClass() {
        StudentDao dao = new StudentDaoImpl();
        String clazz = "10英教1班";
        List<Student> students = dao.findByClass(clazz);
        for (Student student : students) {
            System.out.println(student);
        }
    }
    @Test
    public void testFindByDepartment(){
        StudentDao dao = new StudentDaoImpl();
        String department = "外语系";
        List<Student> students = dao.findByDepartment(department);
        for (Student student : students){
            System.out.println(student);
        }
    }
    @Test
    public void FindAll(){
        StudentDao dao = new StudentDaoImpl();
        String all;
        List<Student> students = dao.findAll();
        for (Student student : students){
            System.out.println(student);
        }
    }
    @Test
    public void testFindRowsByClass(){
        StudentDao dao = new StudentDaoImpl();
        Vector rows = dao.findRowsByClass();
        Iterator iterator = rows.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
    @Test
    public void testFindRowsByDepartment(){
        StudentDao dao = new StudentDaoImpl();
        Vector rows = dao.findRowsByDepartment();
        Iterator iterator = rows.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }
    @Test
    public void testFindRowsBySex() {
        StudentDao dao = new StudentDaoImpl();
        Vector rows = dao.findRowsBySex();
        Iterator iterator = rows.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}

编写测试插入的方法testInsert()
在这里插入图片描述
打开数据库里的表,学生记录插入成功
在这里插入图片描述
编写测试插入的方法testDeleteById()
运行结果
在这里插入图片描述
编写测试方法testDeleteByClass()
运行结果
在这里插入图片描述
编写测试方法testFindByName()
运行结果
在这里插入图片描述
如何判断是否找到记录,可以修改代码后再运行
在这里插入图片描述
运行结果
在这里插入图片描述
创建测试方法testFindRowsBySex()
运行结果
在这里插入图片描述
创建测试方法deleteByDepartment(),运行结果为:
在这里插入图片描述
创建测试方法testUpdate(),运行结果为:
在这里插入图片描述
创建测试方法testFindById(),运行结果为:
在这里插入图片描述
创建测试方法testFindByClass(),运行结果为:
在这里插入图片描述
创建测试方法testFindByDepartment(),运行结果为:
在这里插入图片描述
创建测试方法testFindAll(),运行结果为:
在这里插入图片描述
创建测试方法testFindRowsByClass(),运行结果为:
在这里插入图片描述
创建测试方法testFindRowsByDepartment(),运行结果为:
在这里插入图片描述
创建测试方法testFindRowsBySex(),运行结果为:
在这里插入图片描述
在net.hw.student.test包里创建测试类TestUserDaoImpl

package net.xmh.student.bean.test;

import net.xmh.student.bean.User;
import net.xmh.student.bean.dao.UserDaoImpl;
import net.xmh.student.bean.dao.impl.UserDao;
import org.junit.Test;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * 功能:测试用户数据访问接口实现类
 * 作者:向铭涵
 * 日期:2019年6月19日
 */
public class TestUserDaoImpl {
    @Test
    public void testFindById(){
        UserDao dao = new UserDaoImpl();
        User user = dao.findById(1);
        System.out.println(user);
    }
    @Test
    public void testInsert() {
        User user = new User();
        user.setUsernname("王小小");
        user.setId(9);
        user.setTelephone("18086902766");
        user.setRegisterTime(new Timestamp(new Date().getTime()));
        user.setPassword("555");

        UserDao dao = new UserDaoImpl();
        int count = dao.insert(user);
        if (count > 0){
            System.out.println("恭喜,学生记录插入成功!");
        }else {
            System.out.println("遗憾,学生记录插入失败!");
        }
    }
    @Test
    public void testUpdate(){
        UserDao dao = new UserDaoImpl();
        User user = dao.findById(9);
        user.setUsernname("哒哒");
        int count = dao.update(user);
        if (count > 0){
            System.out.println("更新成功!");
        }else{
            System.out.println("更新失败!");
        }
    }
    @Test
    public void testFindAll(){
        UserDao dao = new UserDaoImpl();
        String all;
        List<User> students = dao.findAll();
        for (User student : students){
            System.out.println(student);
        }
    }
    @Test
    public void testDeleteById(){
        UserDao dao = new UserDaoImpl();
        User user = dao.findById(9);
        System.out.println(user);
        }

    @Test
    public void testLogin(){
        UserDao dao = new UserDaoImpl();
        String username,password;
        username = "howard";
        password ="903213";
        User user = dao.login(username,password);
        if (user != null){
            System.out.println("恭喜,用户名与密码正确,登录成功!");
        }else {
            System.out.println("遗憾,用户名与密码错误,登录失败!");
        }
    }
}


编写测试方法testFindById()
运行结果如下:
在这里插入图片描述
编写测试方法testLogin(),运行结果如下:
在这里插入图片描述
修改一下用户名与密码,再次运行,结果如下:
在这里插入图片描述
在这里插入图片描述
编写测试方法testInsert(),运行结果如下:
在这里插入图片描述
编写测试方法testUpdate(),运行结果如下:
在这里插入图片描述
编写测试方法testFindAll(),运行结果如下:
在这里插入图片描述
编写测试方法testDeleteById(),运行结果如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值