文章目录
八项目开发实现步骤
(七)创建数据访问接口实现类
在net.huawei.student.dao包里创建impl子包(impl: implementation)
1、创建学校数据访问接口实现类
在net.huawei.student.dao.impl包里创建CollegeDaoImpl类
实现CollegeDao接口
选择要实现的抽象方法
单击【OK】按钮
(1)编写按标识符查询学校记录方法
@Override
public College findById(int id) {
College college = null;
Connection conn = ConnectionManager.getConnection();
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;
}
(2)编写更新学校记录方法
@Override
public int update(College college) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
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.huawei.student.dao.impl包,在包里创建TestCollegeDaoImpl类
(1)编写测试按标识查询学校记录方法
package net.huawei.student.dao.impl;
import net.huawei.student.bean.College;
import net.huawei.student.dao.CollegeDao;
import org.junit.Test;
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();
College college = collegeDao.findById(1);
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.huawei.student.dao.impl包里创建StatusDaoImpl类
实现StatusDao接口,空实现两个抽象方法
(1)编写按标识符查询状态记录方法
package net.huawei.student.dao.impl;
import net.huawei.student.bean.Status;
import net.huawei.student.dao.StatusDao;
import net.huawei.student.dbutil.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StatusDaoImpl implements StatusDao {
@Override
public Status findById(int id) {
Status status = null;
Connection conn = ConnectionManager.getConnection();
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 (SQLException e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);
}
return status;
}
@Override
public int update(Status status) {
return 0;
}
}
(2)编写更新状态记录方法
@Override
public int update(Status status) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
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) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
2、测试状态数据访问接口实现类
(1)编写测试标识符查询状态记录方法
在test目录的net.huawei.student.dao.impl包里创建TestStatusDaoImpl类
package net.huawei.student.dao.impl;
import net.huawei.student.bean.Status;
import net.huawei.student.dao.StatusDao;
import org.junit.Test;
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();
Status status = statusDao.findById(1);
System.out.println("更新前:" + status);
status.setCollege("泸州职业技术学院");
status.setVersion("2.0");
status.setVersion("无心剑");
status.setTelephone("15834345670");
status.setEmail("375912360@qq.com");
status.setAddress("泸州江阳区上平远路10号");
int count = statusDao.update(status);
if (count > 0) {
System.out.println("恭喜,状态记录更新成功~");
System.out.println("更新后:" + statusDao.findById(1));
} else {
System.out.println("遗憾,状态记录更新失败~");
}
}
运行testUpdate()方法,查看结果
说明:参看《Java实训项目10:GUI学生信息管理系统 - 实现步骤 - 创建数据访问接口实现类》完成下面两个数据访问接口实现类的编写与测试工作。
3、创建学生数据访问接口实现类
学生数据访问接口实现类StudentDaoImpl
(1)、创建学校数据访问接口实现类
创建学校数据访问接口实现类CollegeDaoImpl