文章目录
- 八、项目开发实现步骤
- (七)创建数据访问接口实现类
- 1、创建学校数据访问接口实现类
- 1_、测试学校数据访问接口实现类
- 2、创建状态数据访问接口实现类
- 2_、测试状态数据访问接口实现类
- 3、创建学生数据访问接口实现类
- 3_、测试学生数据访问接口实现类
- (1)编写测试按标识符查询学校记录方法
- (2)编写测试方法testDeleteById()
- (3)编写测试方法testDeleteByClass()
- (4)编写测试方法testFindByName()
- (5)编写测试方法testFindAll()
- (6)编写测试方法testFindRowsBySex()
- (7)编写测试方法testDeleteByDepartment()
- (8)编写测试方法testUpdate()
- (9)编写测试方法testFindByld()
- (10)编写测试方法testFindByClass()
- (11)编写测试方法testFindByDepartment()
- (12)编写测试方法testFindRowsByClass()
- (13)编写测试方法testFindRowsByDepartment()
- 4、创建用户数据访问接口实现类
- 4_、测试用户数据访问接口实现类
八、项目开发实现步骤
(七)创建数据访问接口实现类
- 在
net.dongyan.student.dao
包里创建impl
子包(impl:implementation)
1、创建学校数据访问接口实现类
-
在
net.dongyan.student.dao.impl
包里创建CollegeDaoImpl
类
-
实现
CollegeDao
接口 -
选择要实现的抽象类
-
单击【OK】按钮
-
方法的空实现
(1)编写按标识符查询学校记录方法
package net.dongyan.student.dao.impl;
import net.dongyan.student.bean.College;
import net.dongyan.student.dao.CollegeDao;
import net.dongyan.student.dbutil.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 功能:学校数据访问接口实现类
* 作者:董艳
* 日期:2023年06月14日
*/
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);
//执行查询操作,返回结果集
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.getTimestamp("start_time"));
college.setEmail(rs.getString("email"));
college.setAddress(rs.getString("address"));
college.setProfile(rs.getString("profile"));
}
//关闭结果
rs.close();
//关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);//关闭数据库连接
}
//返回学校对象
return college;
}
@Override
public int update(College college) {
return 0;
}
}
(2)编写更新学校记录方法
@Override//更新学校记录
public int update(College college) {
//定义更新记录数
int count = 0;
//获取数据库连接
Connection conn = ConnectionManager.getConnection();
//定义SQL语句对象
String strSQL = "UPDATE t_college SET name = ?,president =?,start_time=?,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.getEmail());
pstmt.setString(5,college.getAddress());
pstmt.setString(6,college.getProfile());
pstmt.setInt(7,college.getId());
//执行更新操作,返回更新记录数
count = pstmt.executeUpdate();
//关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
}finally {
ConnectionManager.closeConnection(conn);//关闭数据库连接
}
//返回更新记录数
return count;
1_、测试学校数据访问接口实现类
- 在
test
目录里创建net.dongyan.student.dao.impl
包,在包里创建TestCollegeDaoImpl
类
(1)编写测试按标识符查询学校记录方法
package net.dongyan.student.dao.impl;
import net.dongyan.student.bean.College;
import net.dongyan.student.dao.CollegeDao;
import org.junit.Test;
/**
* 功能:
* 作者:董艳
* 日期:2023年06月14日
*/
public class TestCollegeDaoImpl {
@Test//测试按标识符查询学校记录
public void testFindById(){
//调用标识符变量
int id = 1;
//创建学校数据访问接口对象
CollegeDao collegeDao = new CollegeDaoImpl();
//调用按标识符查询学校记录方法
College college = collegeDao.findById(id);
//判断是否成功
if (college != null){
System.out.println("标识符:"+college.getId());
System.out.println("学校名称:"+ college.getName());
System.out.println("校长:"+ college.getPresident());
System.out.println("建校时间:"+ college.getStartTime());
System.out.println("电子邮箱:"+ college.getEmail());
System.out.println("通讯地址:"+ college.getAddress());
System.out.println("学校概况:"+ college.getProfile());
}else{
System.out.println("标识符为["+ id +"]的学校记录不存在~");
}
}
}
- 运行
testFindById()
方法,查看结果
- 修改标识符变量值,再运行测试方法,查看结果
(2)编写测试更新学校记录方法
@Test//测试更新学校记录
public void testUpdate(){
//创建学校数据访问接口对象
CollegeDao collegeDao = new CollegeDaoImpl();
//获取标识符为1的学校记录
College college = collegeDao.findById(1);
//输出更新前的学校xxi
System.out.println("更新前:"+college);
//设置学校对象属性
college.setName("泸职院");
college.setPresident("萌萌哒");
college.setProfile("泸职院是省双高建设单位......");
//调用更新学校记录方法
int count = collegeDao.update(college);
//判断更新是否成功
if (count > 0){
System.out.println("恭喜,学校记录更新成功~");
System.out.println("更新后:"+ collegeDao.findById(1));
}else{
System.out.println("遗憾,学校记录更新失败~");
}
}
- 运行
testUpdate()
方法,查看结果
2、创建状态数据访问接口实现类
- 在
net.dongyan.student.dao.impl
包里创建StatusDaoImpl
类
- 实现
StatusDao
接口,空实现两个抽象方法
(1)编写按标识符查询状态记录方法
package net.dongyan.student.dao.impl;
import net.dongyan.student.bean.Status;
import net.dongyan.student.dao.StatusDao;
import net.dongyan.student.dbutil.ConnectionManager;
import javax.xml.transform.Result;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* 功能:
* 作者:董艳
* 日期:2023年06月14日
*/
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);
//执行查询操作,返回结果集
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"));
}
//关闭结果集
rs.close();
//关闭预备语句对象
pstmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);
}
//返回状态对象
return status;
}
(2)编写更新状态记录方法
@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 (Exception e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);//关闭数据库连接
}
//返回更新记录数
return count;
}
2_、测试状态数据访问接口实现类
(1)编写测试按标识符查询状态记录方法
- 在
test
目录里创建net.dongyan.student.dao.impl
包,在包里创建TestStatusDaoImpl
类
package net.dongyan.student.dao.impl;
import net.dongyan.student.bean.Status;
import net.dongyan.student.dao.StatusDao;
import org.junit.Test;
/**
* 功能:测试状态数据访问接口实现类
* 作者:董艳
* 日期:2023年06月14日
*/
public class TestStatusDaoImpl {
@Test//测试按标识符查询状态记录
public void testFindById(){
//定义标识符变量
int id = 1;
//创建状态数据访问接口对象
StatusDao statusDao = new StatusDaoImpl();
//调用按标识符查询状态记录方法
Status status = statusDao.findById(id);
//判断查询是否成功
if (status != null){
System.out.println(status);
}else{
System.out.println("标识符为["+ id + "]的状态记录不存在");
}
}
- 运行
testFindById()
方法,查看结果
(2)编写测试更新状态记录方法
@Test//测试更新状态记录
public void testUpdate(){
//创建状态数据访问接口对象
StatusDao statusDao =new StatusDaoImpl();
//获取标识符为1的状态记录
Status status = statusDao.findById(1);
//输出更新的状态记录
System.out.println("更新前:"+ status);
//设置状态对象属性
status.setCollege("泸州职业技术学院");
status.setVersion("2.0");
status.setVersion("董艳");
status.setTelephone("17360487128");
status.setEmail("2407244780@qq.com");
status.setAddress("泸州市江阳区瓦窑坝40号");
//调用更新状态记录方法
int count = statusDao.update(status);
//判断更新是否成功
if (count > 0){
System.out.println("恭喜,状态记录更新成功~");
System.out.println("更新后:" + statusDao.findById(1));
}else{
System.out.println("遗憾,状态记录更新失败~");
}
}
testUpdate()
方法,查看结果
3、创建学生数据访问接口实现类
- 在
net.dongyan.student.dao.impl
包里创建StudentDaoImpl
类
- 实现
CollegeDao
接口
- 选择要实现的抽象方法
- 单击【OK】按钮
(1)编写按标识符查询学生记录方法
package net.dongyan.student.dao.impl;
import net.dongyan.student.bean.Student;
import net.dongyan.student.dao.StudentDao;
import net.dongyan.student.dbutil.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.Vector;
/**
* 功能:学生数据访问接口实现类
* 作者:董艳
* 日期:2023年06月14日
*/
public class StudentDaoImpl implements StudentDao {
@Override//按标识符查询学生记录
public int insert(Student student) {
//定义插入记录数
int count = 0;
//获取数据库连接
Connection conn = ConnectionManager.getConnection();
//定义SQL字符串
String strSQL = "insert into t_student (id,name,sex,age,department,class,telephone)"
+ "values(?,?,?,?,?,?,?)";
try {
//创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//设置占位符的值
pstmt.setString(1,student.getId());
pstmt.setString(2,student.getName());
pstmt.setString(3,student.getSex());
pstmt.setString(5,student.getDepartment());
pstmt.setString(6,student.getClazz());
pstmt.setString(7,student.getTelephone());
//执行SQL,返回插入记录数
count = pstmt.executeUpdate();
//关闭预备语句对象
pstmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);//关闭数据库连接
}
//返回插入记录数
return count;
}
(2)编写更新学生记录方法
@Override
public int deleteById(String id) {
//定义删除记录数
int count = 0;
//获取数据库连接
Connection conn = ConnectionManager.getConnection();
//定义SQL字符串
String strSQL = "delete from t_student where id = ?";
try {
//创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//设置占位符的值
pstmt.setString(1,id);
//执行SQL,返回删除记录数
count = pstmt.executeUpdate();
//关闭预备语句对象
pstmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);//关闭数据库连接
}
//返回删除记录数
return count;
}
@Override
public int deleteByClass(String clazz) {
//定义删除记录数
int count = 0;
//获取数据库连接
Connection conn = ConnectionManager.getConnection();
//定义SQL字符串
String strSQL = "delete from t_student where class = ?";
try {
//创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//设置占位符的值
pstmt.setString(1,clazz);
//执行SQL,返回删除记录数
count = pstmt.executeUpdate();
//关闭预备语句对象
pstmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);//关闭数据库连接
}
//返回删除记录数
return count;
}
@Override
public int deleteByDepartment(String department) {
//定义删除记录数
int count = 0;
//获取数据库连接
Connection conn = ConnectionManager.getConnection();
//定义SQL字符串
String strSQL = "delete from t_student where department = ?";
try {
//创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//设置占位符的值
pstmt.setString(1,department);
//执行SQL,返回删除记录数
count = pstmt.executeUpdate();
//关闭预备语句对象
pstmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);//关闭数据库连接
}
//返回删除记录数
return count;
}
@Override//更新学生记录
public int update(Student student) {
//定义更新记录数
int count = 0;
//获取数据库连接
Connection conn = ConnectionManager.getConnection();
//定义SQL字符串
String strSQL = "update t_student set name = ?,sex = ?,"
+"department = ?,class = ?,telephone = ? where id = ?";
try {
//创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//设置占位符的值
pstmt.setString(1,student.getName());
pstmt.setString(2,student.getSex());
pstmt.setString(3,student.getDepartment());
pstmt.setString(4,student.getClazz());
pstmt.setString(5,student.getTelephone());
pstmt.setString(6,student.getId());
//执行SQL,返回更新记录数
count = pstmt.executeUpdate();
//关闭预备语句对象
pstmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回更新记录数
return count;
}
@Override
public Student findById(String id) {
//定义学生对象
Student student =null;
//获取数据库连接
Connection conn = ConnectionManager.getConnection();
//定义SQL字符串
String strSQL = "select * from t_student where id = ?";
try {
//创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//设置占位符的值
pstmt.setString(1,id);
//执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
//判断结果集是否为空
if (rs.next()){
//创建学生实体类
student = new Student();
//利用当前记录各字段值设置学生实体属性
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
}
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回学生对象
return student;
}
@Override//按姓名查询学生记录
public List<Student> findByName(String name) {
//定义学生列表
List<Student> students = new ArrayList< >();
//获取数据库连接
Connection conn = ConnectionManager.getConnection();
//定义SQL字符串
String strSQL ="select * from t_student where name like ?";
try {
//创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//设置占位符的值
pstmt.setString(1,name + "%");
//执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
//遍历结果集
while (rs.next()) {
//创建学生实体类
Student student = new Student();
//利用当前记录各字段值设置学生实体属性
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
//将实体添加到学生列表
students.add(student);
}
//关闭结果集
rs.close();
//关闭预备语句对象
pstmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回学生列表
return students;
}
@Override//按班级查询学生列表
public List<Student> findByClass(String clazz) {
//定义学生列表
List<Student> students = new ArrayList< >();
//获取数据库连接
Connection conn = ConnectionManager.getConnection();
//定义SQL字符串
String strSQL = "select * from t_student where class like ?";
try {
//创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//设置占位符的值
pstmt.setString(1,clazz +"%");
//执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
//遍历结果集
while (rs.next()){
//创建学生实体
Student student =new Student();
//利用当前记录各字段值设置学生实体属性
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
//将学生实体添加到学生列表
students.add(student);
}
//关闭结果集
rs.close();
//关闭预备语句对象
pstmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回学生列表
return students;
}
@Override//按系部查询学生列表
public List<Student> findByDepartment(String department) {
//定义学生列表
List<Student> students = new ArrayList< >();
//获取数据库连接
Connection conn = ConnectionManager.getConnection();
//定义SQL字符串
String strSQL = "select * from t_student where department like ?";
try {
//创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//设置占位符的值
pstmt.setString(1,department + "%");
//执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
//遍历结果集
while (rs.next()){
//创建学生实体
Student student = new Student();
//利用当前记录各字段值设置学生实体属性
student.setId(rs.getString("id"));
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
//将学生实体添加到学生列表
students.add(student);
}
//关闭结果集
rs.close();
//关闭预备语句对象
pstmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回学生列表
return students;
}
@Override//查询全部学生记录
public List<Student> findAll() {
//定义学生列表
List<Student> students = new ArrayList< >();
//获取数据库连接
Connection conn = ConnectionManager.getConnection();
//定义SQL字符串
String strSQL = "select * from t_student";
try {
//创建语句对象
Statement stmt = conn.createStatement();
//执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
//遍历结果集
while (rs.next()){
//创建学生实体
Student student = new Student();
//利用当前记录各字段值设置学生实体属性
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
//将实体添加到学生列表
students.add(student);
}
//关闭结果集
rs.close();
//关闭预备语句对象
stmt.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回学生列表
return students;
}
@Override//按性别统计学生人数
public Vector findRowsBySex() {
//定义行集向量
Vector rows = new Vector();
//获取数据库连接
Connection conn = ConnectionManager.getConnection();
//定义SQL字符串
String strSQL ="select sex as '性别',count(*) as '人数'"
+"from t_student group by sex order by sex desc";
try {
//创建语句对象
Statement stmt = conn.createStatement();
//执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
//遍历结果集
while (rs.next()){
//定义当前行向量
Vector<String> currentRow = new Vector();
//利用当前记录字段值设置当前行向量的元素值
currentRow.addElement(rs.getString("性别"));
currentRow.addElement(rs.getInt("人数")+"");
//将当前行向量添加到行集向量
rows.addElement(currentRow);
}
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回行集向量
return rows;
}
@Override//按班级统计人数
public Vector findRowsByClass() {
//定义行集向量
Vector rows = new Vector();
//获取数据库连接
Connection conn = ConnectionManager.getConnection();
//定义SQL字符串
String strSQL ="select class as '班级',count(*) as '人数'"
+"from t_student group by class order by class desc";
try {
//创建语句对象
Statement stmt = conn.createStatement();
//执行SQL,查看结果集
ResultSet rs = stmt.executeQuery(strSQL);
//遍历结果集
while (rs.next()){
//定义当前行向量
Vector<String> currentRow = new Vector();
//利用当前记录字段值设置当前行向量的元素值
currentRow.addElement(rs.getString("班级"));
currentRow.addElement(rs.getInt("人数")+"");
//将当前行向量添加到行集向量
rows.addElement(currentRow);
}
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回行集向量
return rows;
}
@Override//按系部统计学生人数
public Vector findRowsByDepartment() {
//定义行集向量
Vector rows = new Vector();
//获取数据库连接
Connection conn = ConnectionManager.getConnection();
//定义SQL字符串
String strSQL = "select department as '系部',count(*) as '人数'"
+"from t_student group by department order by department desc";
try {
//创建语句对象
Statement stmt = conn.createStatement();
//执行SQL,查看结果集
ResultSet rs = stmt.executeQuery(strSQL);
//遍历结果集
while (rs.next()){
//定义当前行向量
Vector<String> currentRow = new Vector();
//利用当前记录字段值设置当前行向量的元素值
currentRow.addElement(rs.getString("系部"));
currentRow.addElement(rs.getInt("人数")+"");
//将当前行向量添加到行集向量
rows.addElement(currentRow);
}
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回行集向量
return rows;
}
3_、测试学生数据访问接口实现类
- 在
test
目录里创建net.dongyan.student.dao.impl
包,在包里创建TestStudentDaoImpl
类
(1)编写测试按标识符查询学校记录方法
package net.dongyan.student.dao.impl;
import net.dongyan.student.bean.Student;
import net.dongyan.student.dao.StudentDao;
import org.junit.Test;
/**
* 功能:测试学生数据访问接口实现类
* 作者:董艳
* 日期:2023年06月14日
*/
public class TestStudentDaoImpl {
//定义学生数据访问对象
StudentDao dao = new StudentDaoImpl();
@Test
public void testInsert(){
//创建学生对象
Student student = new Student();
//设置学生对象属性
student.setId("22180101");
student.setName("王欣宇");
student.setSex("女");
student.setDepartment("信息工程学院");
student.setClazz("2022信安1班");
student.setTelephone("17362346789");
//调用学生数据访问对象的插入方法
int count = dao.insert(student);
//判断学生记录是否插入成功
if (count>0){
System.out.println("恭喜,学生记录插入成功!");
System.out.println(dao.findById(student.getId()));
}else {
System.out.println("遗憾,学生记录插入失败!");
}
}
}
- 运行
testFindById()
方法,查看结果
(2)编写测试方法testDeleteById()
@Test
public void testDeleteById(){
String id = "19200446";
//调用学生数据访问对象的按id删除方法
int count = dao.deleteById(id);
//判断学生记录是否删除成功
if (count >0){
System.out.println("学生记录删除成功!");
}else {
System.out.println("遗憾,学生记录删除失败~");
}
}
- 运行
testUpdate()
方法,查看结果
(3)编写测试方法testDeleteByClass()
@Test
public void testDeleteByClass(){
String clazz = "2022小教3班";
//调用学生数据访问对象的按班级删除方法
int count = dao.deleteByClass(clazz);
if (count > 0){
System.out.println("恭喜,["+ clazz + "]学生记录删除成功!");
}else {
System.out.println("遗憾,["+ clazz + "]学生记录删除失败!");
}
}
- 运行
testDeleteByClass()
方法,查看结果
(4)编写测试方法testFindByName()
@Test
public void testFindByName(){
String name = "张三丰";
//调用学生数据访问对象的按姓名查找方法
List<Student> students = dao.findByName(name);
//判断列表里是否有元素
if (students.size()>0){
//通过增强for循环遍历学生列表
for (Student student : students){
System.out.println(student);
}
}else {
System.out.println("温馨提示:查无此人!");
}
}
- 运行
testFindByName()
方法,查看结果
- 修改待查学生姓名
- 运行该测试方法,查看结果
- 查找所有姓“李”的学生记录,修改查找目标
- 运行该测试方法,查找结果
(5)编写测试方法testFindAll()
@Test
public void testFindAll(){
//调用学生数据访问对象的全部方法
List<Student> students = dao.findAll();
//通过增强for循环遍历学生列表
for (Student student : students){
System.out.println(student);
}
}
- 运行
testFindAll()
方法,查看结果
(6)编写测试方法testFindRowsBySex()
@Test
public void testFindRowsBySex(){
//调用学生数据访问对象的按性别统计人数方法
Vector rows = dao.findRowsBySex();
//获取向量的迭代器
Iterator iterator = rows.iterator();
//遍历迭代器
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
- 运行
testFindRowsBySex()
方法,查看结果
(7)编写测试方法testDeleteByDepartment()
@Test
public void testDeleteByDepartment(){
String department = "大数据学院";
//调用学生数据访问对象的按department删除方法
int count = dao.deleteByDepartment(department);
//判断学生记录是否删除成功
if (count >0){
System.out.println("学生记录删除成功!");
}else {
System.out.println("遗憾,学生记录删除失败~");
}
}
- 运行
testDeleteByDepartment()
方法,查看结果
(8)编写测试方法testUpdate()
@Test//测试更新学生记录
public void testUpdate(){
//创建学生数据访问接口对象
StudentDao studentDao = new StudentDaoImpl();
//获取标识符为1的学生记录
Student student = studentDao.findById("22212160");
//输出更新的学生记录
System.out.println("更新前:" + student);
//设置学生对象属性
student.setAge(20);
student.setTelephone("11111000001");
student.setDepartment("信息工程学院学院");
//调用更新学生记录方法
int count = studentDao.update(student);
//判断更新是否成功
if (count > 0) {
System.out.println("恭喜,学生记录更新成功!");
System.out.println("更新后:" + studentDao.findById("22212160"));
} else {
System.out.println("遗憾,学生记录更新失败!");
}
}
- 运行
testUpdate()
方法,查看结果
(9)编写测试方法testFindByld()
@Test
public void testFindById() {
String id = "22212160";
Student student = dao.findById(id);
if (student != null) {
System.out.println("查询到了:" + student);
} else {
System.out.println("遗憾,学生记录查询失败!");
}
}
- 运行
testFindByld()
方法,查看结果
(10)编写测试方法testFindByClass()
@Test
public void testFindByClass() {
String clazz = "2022机电3班";
List<Student> students = dao.findByClass(clazz);
if (students.size() > 0) {
for (Student student : students) {
System.out.println(student);
}
} else {
System.out.println("遗憾,学生记录查询失败!");
}
}
- 运行
testFindByClass()
方法,查看结果
(11)编写测试方法testFindByDepartment()
@Test
public void testFindByDepartment() {
String department = "信息工程学院";
List<Student> students = dao.findByDepartment(department);
if (students.size() > 0) {
for (Student student : students) {
System.out.println(student);
}
} else {
System.out.println("遗憾,学生记录查询失败!");
}
}
- 运行
testFindByDepartment()
方法,查看结果
(12)编写测试方法testFindRowsByClass()
@Test
public void testFindRowsByClass() {
Vector rows = dao.findRowsByClass();
Iterator iterator = rows.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
- 运行
testFindRowsByClass()
方法,查看结果
(13)编写测试方法testFindRowsByDepartment()
@Test
public void testFindRowsByDepartment() {
Vector rows = dao.findRowsByDepartment();
Iterator iterator = rows.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
- 运行
testFindRowsByDepartment()
方法,查看结果
4、创建用户数据访问接口实现类
- 在
net.dongyan.student.dao.impl
包里创建UserDaoImpl
类
- 实现
UserDao
接口
- 选择要实现的抽象方法
- 单击【OK】按钮
(1)编写按标识符查询用户记录方法
package net.dongyan.student.dao.impl;
import net.dongyan.student.bean.User;
import net.dongyan.student.dao.UserDao;
import net.dongyan.student.dbutil.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* 功能:用户数据访问接口实现类
* 作者:董艳
* 日期:2023年06月15日
*/
public class UserDaoImpl implements UserDao {
@Override
public int insert(User user) {
//定义插入记录数
int count = 0;
//获取数据库连接
Connection conn = ConnectionManager.getConnection();
//定义SQL字符串
String strSQL = "insert into t_user (username,password,telephone,register_time)"
+"value(?,?,?,?)";
//不允许用户表里插入两条用户名相同的记录
if (!isUsernameExisted(user.getUsername())) {
try {
//创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//设置占位符的值
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getTelephone());
pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
//执行SQL,返回插入记录数
count = pstmt.executeUpdate();
//关闭预备语句对象
pstmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
}
//返回插入记录数
return count;
}
(2)编写更新用户记录方法
package net.dongyan.student.dao.impl;
import net.dongyan.student.bean.User;
import net.dongyan.student.dao.UserDao;
import net.dongyan.student.dbutil.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* 功能:用户数据访问接口实现类
* 作者:董艳
* 日期:2023年06月15日
*/
public class UserDaoImpl implements UserDao {
@Override
public int insert(User user) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "insert into t_user (username,password,telephone,register_time)"
+"value(?,?,?,?)";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getTelephone());
pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
count = pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int deleteById(int id) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "DELETE FROM t_user WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, id);
count = pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int update(User user) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "UPDATE t_user SET username = ?, password = ?, telephone = ?,"
+ "register_time = ? WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getTelephone());
pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
count = pstmt.executeUpdate();
pstmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public User findById(int id) {
User user = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = "SELECT * FROM t_user WHERE id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
}
pstmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);
}
return user;
}
@Override
public List<User> findAll() {
List<User> users = new ArrayList<>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "SELECT * FROM t_user";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(strSQL);
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
users.add(user);
}
rs.close();
stmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);
}
return users;
}
@Override
public User login(String username, String password) {
User user = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = "SELECT * FROM t_user WHERE username = ? and password = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, username);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
}
pstmt.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);
}
return user;
}
@Override
public boolean isUsernameExisted(String username) {
boolean existed = false;
Connection conn = ConnectionManager.getConnection();
String strSQL = "SELECT * FROM t_user WHERE username = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, username);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
existed = true;
}
pstmt.close();
rs.close();
} catch (Exception e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);
}
return existed;
}
}
4_、测试用户数据访问接口实现类
- 在
test
目录里创建net.dongyan.student.dao.impl
包,在包里创建TestUserDaoImpl
类
(1)编写测试按标识符查询用户记录方法
package net.dongyan.student.dao.impl;
import net.dongyan.student.bean.User;
import net.dongyan.student.dao.UserDao;
import org.junit.Test;
import java.util.Date;
import java.util.List;
/**
* 功能:
* 作者:董艳
* 日期:2023年06月16日
*/
public class TestUserDaoImpl {
UserDao dao = new UserDaoImpl();
@Test
public void testFindById() {
User user = dao.findById(1);
System.out.println("用户名:" + user.getUsername());
System.out.println("密码:" + user.getPassword());
System.out.println("电话:" + user.getTelephone());
System.out.println("注册时间:" + user.getRegisterTime());
}
- 运行
testFindById()
方法,查看方法
(2)编写测试testLogin()
@Test
public void testLogin() {
String username, password;
username = "只因";
password = "admin";
User user = dao.login(username, password);
if (user != null) {
System.out.println("用户名与密码正确,登录成功!!!");
} else {
System.out.println("遗憾,登录失败!用户名或密码输入错误!!!");
}
}
- 运行
testLogin()
方法,查看结果
(3)编写测试testIsUsernameExisted()
@Test
public void testIsUsernameExisted() {
String username = "张三丰";
boolean result = dao.isUsernameExisted(username);
if (result) {
System.out.println("温馨提示:用户名[" + username + "]已存在,不可用此名注册!!!");
} else {
System.out.println("温馨提示:用户名[" + username + "]不存在,可用此名注册");
}
}
- 运行
testIsUsernameExisted()
方法,查看结果
(4)编写测试testInsert()
@Test
public void testInsert() {
User user = new User();
user.setUsername("蔡徐坤");
user.setPassword("111111");
user.setTelephone("17652136666");
user.setRegisterTime(new Date());
int count = dao.insert(user);
if (count > 0) {
System.out.println("恭喜,用户记录插入成功!");
System.out.println(dao.findById(dao.findAll().size()));
} else {
System.out.println("遗憾,用户记录插入失败!");
}
}
- 运行
testInsert()
方法,查看结果
(5)编写测试testDeleteById()
@Test
public void testDeleteById() {
int id = 9;
int count = dao.deleteById(id);
if (count != 0) {
System.out.println("恭喜,删除用户记录成功!!!");
} else {
System.out.println("遗憾,删除记录失败!!!");
}
}
- 运行
testDeleteById()
方法,查看结果
(6)编写测试testUpdate()
@Test
public void testUpdate() {
UserDao userdao = new UserDaoImpl();
User user = userdao.findById(1);
System.out.println("更新前:" + user);
user.setTelephone("11111000111");
user.setUsername("只因");
int count = userdao.update(user);
if (count > 0) {
System.out.println("更新成功!!!");
System.out.println("更新后:" + userdao.findById(1));
} else {
System.out.println("遗憾,更新失败!");
}
}
- 运行
testUpdate()
方法,查看结果
(7)编写测试testFindAll()
@Test
public void testFindAll() {
List<User> users = dao.findAll();
for (User user : users) {
System.out.println(user);
}
}
- 运行
testFindAll()
方法,查看结果