1.在dao包下新建impl包并创建数据访问接口实现类
(1)学校数据访问接口实现类CollegeDaoImpl
package net.wj.student.dao.impl;
import net.wj.student.bean.College;
import net.wj.student.dao.CollegeDao;
import net.wj.student.dbutil.ConnectionManger;
import java.sql.*;
/**
* 功能:学校数据访问接口实现类
* 作者:王二白
* 日期:2019年6月18日
*/
public class CollegeDaoImpl implements CollegeDao {
@Override
public College findById(int id) {
//声明学校对象
College college = null;
//1.获取数据库连接
Connection conn = ConnectionManger.getConnection();
//2.定义SQL字符串
String strSQL = "select * from college where id = ?";
try {
//3.创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4.设置占位符的值
pstmt.setInt(1,id);
//5.执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
//6.判断结果集是否有记录
if(rs.next()){
//实例化学校对象
college = new College();
//利用当前记录字段值去设置学校对象的属性
college.setId(rs.getInt("id"));
college.setName(rs.getNString("name"));
college.setPresident(rs.getNString("president"));
college.setStarttime(rs.getDate("starttime"));
college.setPhone(rs.getNString("phone"));
college.setEmail(rs.getNString("email"));
college.setAddress(rs.getString("address"));
college.setProfile(rs.getString("profile"));
}
//7.关闭预备语句对象
pstmt.close();
//8.关闭结果集对象
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManger.closeConnection(conn);
}
//返回学校对象
return college;
}
@Override
public int update(College college) {
//定义更新记录数
int count = 0;
//1. 获取数据库连接
Connection conn = ConnectionManger.getConnection();
//2.定义SQL语句
String strSQL = "update college set name = ?,president = ?,starttime = ?,"
+ " phone = ?,email = ?,profile = ? where id = ?";
try {
//3.创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4.设置占位符的值
pstmt.setString(1,college.getName());
pstmt.setString(2,college.getPresident());
pstmt.setTimestamp(3,new Timestamp(college.getStarttime().getTime()));
pstmt.setString(4,college.getPhone());
pstmt.setString(5,college.getEmail());
pstmt.setString(6,college.getProfile());
pstmt.setInt(7,college.getId());
//5.执行SQL,返回更新记录数
count = pstmt.executeUpdate();
//6.关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManger.closeConnection(conn);
}
//返回更新记录数
return count;
}
}
创建net.wj.student.test包并创建测试类TestCollegeDaoImpl
package net.wj.student.test;
import net.wj.student.bean.College;
import net.wj.student.dao.CollegeDao;
import net.wj.student.dao.impl.CollegeDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* 功能:测试学校数据访问接口实现类
* 作者:王二白
* 日期:2019年6月18日
*/
public class TestCollegeDaoImol {
@Before
public void beforeTest() {
System.out.println("单元测试开始了~");
}
@After
public void afterTest() {
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);
int count = dao.update(college);
if(count >0) {
System.out.println("恭喜,学校记录更新成功!");
} else{
System.out.println("遗憾,学校记录更新失败!");
}
}
}
运行结果如下
这里有个小问题
修改校长名的时候没有修改成功,后面发现是数据库中表中字段的位置没有与CollegeDaoImpl中的位置相对应,
后面修改了表中字段的位置后就修改成功了
(2)状态数据访问接口实现类StatusDaoImpl
与学校数据访问接口实现类相似,只是修改了字段名
package net.wj.student.dao.impl;
import net.wj.student.bean.Status;
import net.wj.student.dao.StatusDao;
import net.wj.student.dbutil.ConnectionManger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 功能:状态数据访问接口实现类
* 作者:王洁
* 日期:2019年6月18日
*/
public class StatusDaoImpl implements StatusDao {
@Override
public Status findById(int id) {
//声明状态对象
Status status = null;
//1.获取数据库连接
Connection conn = ConnectionManger.getConnection();
//2.定义SQL字符串
String strSQL = "select * from status where id = ?";
try {
//3.创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4.设置占位符的值
pstmt.setInt(1,id);
//5.执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
//6.判断结果集是否有记录
if(rs.next()) {
//实例化状态对象
status = new Status();
//利用当前记录字段值去设置状态对象的属性
status.setId(rs.getInt("id"));
status.setName(rs.getNString("name"));
status.setVersion(rs.getNString("version"));
status.setAuthor(rs.getNString("author"));
status.setPhone(rs.getNString("phone"));
status.setAddress(rs.getString("address"));
status.setEmail(rs.getNString("email"));
}
//7.关闭预备语句对象
pstmt.close();
//8.关闭结果集对象
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManger.closeConnection(conn);
}
//返回状态对象
return status;
}
@Override
public int update(Status status) {
//定义更新记录数
int count = 0;
//1.获取数据库连接
Connection conn = ConnectionManger.getConnection();
//2.定义SQL语句
String strSQL = "update status set name = ?,version = ?,author = ?,"
+"phone = ?,address = ?,email = ? where id = ?";
try {
//3.创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
//4.设置占位符的值
pstmt.setString(1,status.getName());
pstmt.setString(2,status.getVersion());
pstmt.setString(3,status.getAuthor());
pstmt.setString(4,status.getPhone());
pstmt.setString(5,status.getAddress());
pstmt.setString(6,status.getAddress());
pstmt.setInt(7,status.getId());
//5.执行SQL,返回更新记录数
count = pstmt.executeUpdate();
//6关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//关闭数据库连接
ConnectionManger.closeConnection(conn);
}
//返回更新记录数
return count;
}
}
创建测试类TestCollegeDaoImpl
package net.wj.student.test;
import net.wj.student.bean.Status;
import net.wj.student.dao.StatusDao;
import net.wj.student.dao.impl.StatusDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* 功能:测试状态访问接口实现类
* 作者:王洁
* 日期:2019年6月18日
*/
public class TestStatusDaoImpl {
@Before
public void beforeTest() {
System.out.println("单元测试开始了~");
}
@After
public void afterTest() {
System.out.println("单元测试结束了~");
}
@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("王洁");
status.setAddress("泸州市龙马潭区长桥路2号");
status.setPhone("18113501011");
status.setEmail("1907835633@qq.com");
dao.update(status);
System.out.println(status);
int count = dao.update(status);
if(count >0) {
System.out.println("恭喜,学校记录更新成功!");
}else{
System.out.println("遗憾,学校记录更新失败!");
}
}
}
运行结果如下