八,项目实训实现步骤
(7)创建数据访问接口实现类
- package net.zhao.student.dao包中建impl子包
![在这里插入图片描述](https://img-blog.csdnimg.cn/bec9db8bc6d2480f98df5ac6de4ab536.png)
1,创建学校数据访问接口实现类
- 在net.zi里创建mplhao.student.dao.impl创建CollegeDaoImpl类
![在这里插入图片描述](https://img-blog.csdnimg.cn/b7c79387bdaa4ca1a16c6e03a97b7726.png)
- 实现接口CollegeDao接口
![在这里插入图片描述](https://img-blog.csdnimg.cn/a657fa9a6bd7442f8480fc7bac2c9903.png)
- 选择要实现抽象实现的类
![在这里插入图片描述](https://img-blog.csdnimg.cn/0e5efd69103242afaca1150ee7bb6928.png)
- 单击ok按钮
- 方法的空实现
public class CollegeDaoImpl implements CollegeDao {
@Override
public College findByID(int id) {
return null;
}
@Override
public int update(College college) {
return 0;
}
}
(1)编写标识符
![在这里插入图片描述](https://img-blog.csdnimg.cn/7ced7f6599eb4df1904d8ee207905c6c.png)
package net.zhao.student.dao.impl;
import net.zhao.student.bean.College;
import net.zhao.student.dao.CollegeDao;
import net.zhao.student.dbutil.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class CollegeDaoImpl implements CollegeDao {
@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();
} catch (SQLException e) {
System.out.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();
String strSQL="UPDATE t_college SET id=?,name=?, president=?,start_time=email=?,address=?,profile=? WHERE id=?";
try {
PreparedStatement pstemt = conn.prepareStatement(strSQL);
pstemt.setString(1,college.getName());
pstemt.setString(2,college.getPresident());
pstemt.setTimestamp(3,new Timestamp(college.getStartTime().getTime()));
pstemt.setString(4,college.getEmail());
pstemt.setString(5,college.getAddress());
pstemt.setString(6,college.getProfile());
pstemt.setInt(7,college.getId());
count=pstemt.executeUpdate();
pstemt.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
}
return count;
}
}
1_,测试数据库访问
- 在test目录里创建net.zhao.student.bean.dao.impl
![在这里插入图片描述](https://img-blog.csdnimg.cn/c769051b4d9144c8b4d8d342f43da8d6.png)
(1)编写测试
package net.zhao.student.bean.dao.impl;
import net.zhao.student.bean.College;
import net.zhao.student.dao.CollegeDao;
import net.zhao.student.dao.impl.CollegeDaoImpl;
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+"]的学校记录不在");
}
}
}
- 运行测试方法
![在这里插入图片描述](https://img-blog.csdnimg.cn/718a0b51d79c4035a5b1fb6296ada915.png)
- 修改标识符变量值,测试方法,查看结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/bed9acaed21141d586d7f0b3cfcc53ff.png)
(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("遗憾,学校记录更新失败");
}
}
- 运行结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/d70c8bc1e867417094ac7cbd29fb84bb.png)
2,创建状态数据访问接口实现类
- 创建状态数据访问接口实现类StatusDaoImpl
![在这里插入图片描述](https://img-blog.csdnimg.cn/b023312628f447a194a9be66ec8f4350.png)
- 实现两个空实现
![在这里插入图片描述](https://img-blog.csdnimg.cn/889e158738e24dd9983c19638757d3da.png)
(1)编写标识状态查询方法
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 pstet =conn.prepareStatement(strSQL);
pstet.setInt(1, id);
ResultSet rs=pstet.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"));
}
pstet.close();
rs.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);
}
return status;
}
(2)编写更新记录方法