文章目录(项目9)
实现步骤
(五)创建数据库访问接口
1、创建学校数据访问接口
2、创建状态数据访问接口
3、创建学生数据访问接口
4、创建用户数据访问接口
- 在net.hw.student包里创建子包dao
1、创建学校数据访问接口
- 创建学校数据访问接口CollegeDao
package net.zm.student.dao;
import net.zm.student.bean.College;
public interface CollegeDao {
College findById(int id);
int update(College college);
}
2、创建状态数据访问接口
- 创建状态数据访问接口StatusDao
package net.zm.student.dao;
import net.zm.student.bean.Status;
public interface StatusDao {
Status findById(int id);
int update(Status status);
}
3、创建学生数据访问接口
- 创建学生数据访问接口StudentDao
package net.zm.student.dao;
import net.zm.student.bean.Student;
import java.util.List;
import java.util.Vector;
public interface StudentDao {
int insert(Student student);
int deleteById(String id);
int deleteByClass(String clazz);
int deleteByDepartment(String department);
int update(Student student);
Student findById(String id);
List<Student> findByName(String name);
List<Student> findByClass(String clazz);
List<Student> findByDepartment(String department);
List<Student> findAll();
Vector findRowsBySex();
Vector findRowsByClass();
Vector findRowsByDepartment();
}
4、创建用户数据访问接口
- 创建用户数据访问接口UserDao
package net.zm.student.dao;
import net.zm.student.bean.User;
import java.util.List;
public interface UserDao {
int insert(User user);
int deleteById(int id);
int update(User user);
User findById(int id);
List<User> findAll();
User login(String username, String password);
boolean isUsernameExisted(String username);
}
文章目录(项目10)
实现步骤
(六)创建数据访问接口实现类
1、创建学校数据访问接口实现类
单元测试:对CollegeDaoImpl进行单元测试
(1)编写测试方法testFindById()
(2)编写测试方法testUpdate()
(3)同时运行多个测试方法
(4)完善测试代码
实现步骤
(六)创建数据访问接口实现类
- 在net.hw.student.dao包里创建impl子包,然后在里面创建四个数据访问接口的实现类。
1、创建学校数据访问接口实现类
- 创建学校数据访问接口实现类CollegeDaoImpl
package net.zm.student.dao.impl;
import net.zm.student.bean.College;
import net.zm.student.dao.CollegeDao;
import net.zm.student.dbutli.ConnectionManager;
import java.sql.*;
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.setTelephone(rs.getString("telephone"));
college.setEmail(rs.getString("email"));
college.setAddress(rs.getString("address"));
college.setProfile(rs.getString("profile"));
}
pstmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return college;
}
@Override
public int update(College college) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
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.setString(8,college.getId());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return 0;
}
}
单元测试:对CollegeDaoImpl进行单元测试
- 在根包net.hw.student里创建test子包,在里面创建测试类TestCollegeDaoImpl
package net.zm.student.test;
import net.zm.student.bean.College;
import net.zm.student.dao.CollegeDao;
import net.zm.student.dao.impl.CollegeDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestCollegeDaoImpl {
CollegeDao dao = new CollegeDaoImpl();
@Before
public void beforeTest(){
System.out.println("温馨提示:单元测试开始啦!");
}
@After
public void afterTest(){
System.out.println("温馨提示:单元测试结束啦!");
}
@Test
public void testFindById(){
College college = dao.findById(1);
if (college != null){
System.out.println("校名:"+college.getName());
System.out.println("校长:"+college.getPresident());
System.out.println("地址:"+college.getAddress());
System.out.println("邮箱:"+college.getEmail());
System.out.println("电话:"+college.getTelephone());
}else {
System.out.println("没有查找到学校记录!");
}
}
@Test
public void testUpdate(){
College college = dao.findById(1);
college.setPresident("张三丰");
int count = dao.update(college);
if (count>0){
System.out.println("学校记录更新成功!");
System.out.println("新校长:"+college.getPresident());
}else {
System.out.println("学校记录更新失败!");
}
}
}
- 运行测试方法testFindById(),查看结果
- 运行测试方法testUpdate(),查看结果
2、创建状态数据访问接口实现类
- 创建状态数据访问接口实现类StatusDaoImpl
package net.zm.student.dao.impl;
import net.zm.student.bean.Status;
import net.zm.student.dao.StatusDao;
import net.zm.student.dbutli.ConnectionManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StatusDaolmpl 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"));
}
pstmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return status;
}
@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) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
}
单元测试:对StatusDaoImpl进行单元测试
- 在net.zm.student.test包里创建测试类TestStatusDaoImpl
package net.zm.student.test;
import net.zm.student.bean.Status;
import net.zm.student.dao.StatusDao;
import net.zm.student.dao.impl.StatusDaolmpl;
import org.junit.Test;
public class TestStatusDaolmpl {
StatusDao dao = new StatusDaolmpl();
@Test
public void testFindById(){
Status status = dao.findById(1);
System.out.println("作者:"+status.getAuthor());
System.out.println("学校:"+status.getCollege());
System.out.println("版本:"+status.getVersion());
System.out.println("地址:"+status.getAddress());
System.out.println("电话:"+status.getTelephone());
System.out.println("邮箱:"+status.getEmail());
}
@Test
public void testUpdate(){
Status status = dao.findById(1);
status.setAuthor("曾梅");
status.setTelephone("18208303744");
status.setEmail("[email protected]");
int count = dao.update(status);
if (count>0){
System.out.println("状态记录更新成功!");
System.out.println(dao.findById(1));
}else {
System.out.println("状态记录更新失败!");
}
}
}
-
运行testFindById()方法,查看结果
-
运行testUpdate()方法,查看结果
3、学生数据访问接口实现类
- 学生数据访问接口实现类StudentDaoImpl
package net.zm.student.dao.impl;
import net.zm.student.bean.Student;
import net.zm.student.dao.StudentDao;
import net.zm.student.dbutli.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class StudentDaolmpl implements StudentDao {
@Override
public int insert(Student student) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
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.setInt(4, student.getAge());
pstmt.setString(5, student.getDepartment());
pstmt.setString(6, student.getClazz());
pstmt.setString(7, student.getTelephone());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int deleteById(String id) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "delete from t_student where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, id);
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int deleteByClass(String clazz) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "delete from t_student where class = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, clazz);
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int deleteByDepartment(String department) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "delete from t_student where department = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, department);
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public int update(Student student) {
int count = 0;
Connection conn = ConnectionManager.getConnection();
String strSQL = "update t_student set name = ?, sex = ?, age = ?,"
+ " department = ?, class = ?, telephone = ? where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
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());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return count;
}
@Override
public Student findById(String id) {
Student student = null;
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_student where id = ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, id);
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.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;
}
@Override
public List<Student> findByName(String name) {
List<Student> students = new ArrayList<Student>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_student where name like ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, name + "%");
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.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
students.add(student);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return students;
}
@Override
public List<Student> findByClass(String clazz) {
List<Student> students = new ArrayList<Student>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_student where class like ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, clazz + "%");
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.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
students.add(student);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return students;
}
@Override
public List<Student> findByDepartment(String department) {
List<Student> students = new ArrayList<Student>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_student where department like ?";
try {
PreparedStatement pstmt = conn.prepareStatement(strSQL);
pstmt.setString(1, department + "%");
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.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
students.add(student);
}
rs.close();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return students;
}
@Override
public List<Student> findAll() {
List<Student> students = new ArrayList<Student>();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select * from t_student";
try {
Statement stmt = conn.createStatement();
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.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
students.add(student);
}
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return students;
}
@Override
public Vector findRowsBySex() {
Vector rows = new Vector();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select sex as '性别', count(*) as '人数'"
+ " from t_student group by sex order by sex desc";
try {
Statement stmt = conn.createStatement();
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 (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return rows;
}
@Override
public Vector findRowsByClass() {
Vector rows = new Vector();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select class as '班级', count(*) as '人数'"
+ " from t_student group by class order by class desc";
try {
Statement stmt = conn.createStatement();
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 (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return rows;
}
@Override
public Vector findRowsByDepartment() {
Vector rows = new Vector();
Connection conn = ConnectionManager.getConnection();
String strSQL = "select department as '系部', count(*) as '人数'"
+ " from t_student group by department order by department desc";
try {
Statement stmt = conn.createStatement();
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 (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return rows;
}
}
单元测试:对TestStudentDaoImpl进行单元测试
- 在net.zm.student.test包里创建测试类TestStudentDaoImpl
package net.zm.student.test;
import net.zm.student.bean.Student;
import net.zm.student.dao.StudentDao;
import net.zm.student.dao.impl.StudentDaolmpl;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
public class TestStudentDaolmpl {
StudentDao dao = new StudentDaolmpl();
@Test
public void testInsert(){
Student student = new Student();
student.setId("19242094");
student.setName("张晓慧");
student.setSex("女");
student.setAge(19);
student.setDepartment("艺术传媒学院");
student.setClazz("2019数媒3班");
student.setTelephone("18208303744");
int count = dao.insert(student);
if (count>0){
System.out.println("恭喜,学生记录插入成功!");
System.out.println(dao.findById(student.getId()));
}else {
System.out.println("遗憾,学生记录插入失败!");
}
}
@Test
public void testDeleteById(){
String id = "19242094";
int count = dao.deleteById(id);
if (count>0){
System.out.println("恭喜,学生记录删除成功!");
}else {
System.out.println("遗憾,学生记录删除失败!");
}
}
@Test
public void testDeleteByClass(){
String clazz = "2019小教3班";
int count = dao.deleteByClass(clazz);
if (count>0){
System.out.println("恭喜,["+clazz+"]学生记录删除成功!");
}else {
System.out.println("遗憾,["+clazz+"]学生记录删除失败!");
}
}
@Test
public void testFindByName(){
String name = "李玉山";
List<Student> students = dao.findByName(name);
if (students.size()>0){
for (Student student:students){
System.out.println(student);
}
}else {
System.out.println("温馨提示:查无此人!");
}
}
@Test
public void testFindAll(){
List<Student> students = dao.findAll();
for (Student student :students){
System.out.println(student);
}
}
@Test
public void testFindRowsBySex(){
Vector rows = dao.findRowsBySex();
Iterator iterator = rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void testDeleteByDepartment(){
String dep = "外语系";
int count = dao.deleteByDepartment(dep);
if (count>0){
System.out.println("恭喜,学生记录删除成功!");
}else {
System.out.println("遗憾,学生记录删除失败!");
}
}
@Test
public void testUpdate(){
Student student=dao.findById("11012456");
student.setName("李玉刚");
student.setSex("男");
student.setAge(20);
student.setDepartment("外语系");
student.setClazz("19小教一班");
student.setTelephone("18208303744");
int count=dao.update(student);
if(count>0){
System.out.println("学生记录更新成功!");
System.out.println(dao.findById("11012456"));
}else {
System.out.println("学生记录更新失败!");
}
}
@Test
public void testFindById(){
Student student=dao.findById("11012456");
System.out.println("id:"+student.getId());
System.out.println("姓名:"+student.getName());
System.out.println("性别:"+student.getSex());
System.out.println("年龄:"+student.getAge());
System.out.println("系部:"+student.getDepartment());
System.out.println("班级:"+student.getClazz());
System.out.println("电话:"+student.getTelephone());
}
@Test
public void testFindByClass(){
List<Student> students=dao.findByClass("11建工1班");
for(Student student:students){
System.out.println("id:"+student.getId());
System.out.println("姓名:"+student.getName());
System.out.println("性别:"+student.getSex());
System.out.println("年龄:"+student.getAge());
System.out.println("系部:"+student.getDepartment());
System.out.println("班级:"+student.getClazz());
System.out.println("电话:"+student.getTelephone());
}
}
@Test
public void testFindByDepartment(){
List<Student> students=dao.findByDepartment("信息工程系");
if(students.size()>0){
for(Student student:students){
System.out.println("id:"+student.getId());
System.out.println("姓名:"+student.getName());
System.out.println("性别:"+student.getSex());
System.out.println("年龄:"+student.getAge());
System.out.println("系部:"+student.getDepartment());
System.out.println("班级:"+student.getClazz());
System.out.println("电话:"+student.getTelephone());
}
}else {
System.out.println("温馨提示:没有这个系部!");
}
}
@Test
public void testFindRowsByClass(){
Vector rows=dao.findRowsByClass();
Iterator iterator=rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void testFindRowsByDepartment(){
Vector rows=dao.findRowsByDepartment();
Iterator iterator=rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
4、创建用户数据访问接口实现类
- 创建用户数据访问接口实现类UserDaoImpl
package net.zm.student.dao.impl;
import net.zm.student.bean.User;
import net.zm.student.dao.UserDao;
import net.zm.student.dbutli.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class UserDaolmpl 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)"
+ " values (?, ?, ?, ?)";
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()));
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} 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 (SQLException e) {
e.printStackTrace();
} 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()));
pstmt.setInt(5, user.getId());
count = pstmt.executeUpdate();
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} 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"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return user;
}
@Override
public List<User> findAll() {
List<User> users = new ArrayList<User>();
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 (SQLException e) {
e.printStackTrace();
} 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"));
}
} catch (SQLException e) {
e.printStackTrace();
} 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 (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
return existed;
}
}
单元测试:对TestUserDaoImpl进行单元测试
- 在net.zm.student.test包里创建测试类TestUserDaoImpl
package net.zm.student.test;
import net.zm.student.bean.User;
import net.zm.student.dao.UserDao;
import net.zm.student.dao.impl.UserDaolmpl;
import org.junit.Test;
import java.util.Date;
import java.util.List;
public class TestUserDaolmpl {
UserDao dao = new UserDaolmpl();
@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());
}
@Test
public void testLogin(){
String username,password;
username = "admin";
password = "admin";
User user = dao.login(username,password);
if (user != null){
System.out.println("恭喜,用户名与密码正确,登陆成功!");
}else {
System.out.println("遗憾,用户名或密码错误,登陆失败!");
}
}
@Test
public void testlsUsernameExisted(){
String username = "张三丰";
boolean result = dao.isUsernameExisted(username);
if (result){
System.out.println("温馨提示:["+username+"]已存在,不可用此名注册!");
}else {
System.out.println("温馨提示:["+username+"]不存在,可用此名注册!");
}
}
@Test
public void testInsert(){
User user = new User();
user.setUsername("吴彦祖");
user.setPassword("111");
user.setTelephone("18208303744");
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("遗憾,用户记录插入失败!");
}
}
@Test
public void testDeleteById(){
int id=8;
int count=dao.deleteById(id);
if(count>0){
System.out.println("恭喜,用户记录删除成功!");
}else {
System.out.println("遗憾,用户记录删除失败!");
}
}
@Test
public void testUpdate(){
User user=dao.findById(1);
user.setUsername("adminn");
user.setPassword("adminn");
user.setTelephone("18922222222");
user.setRegisterTime(new Date());
int count=dao.update(user);
if(count>0){
System.out.println("状态记录更新成功!");
System.out.println(dao.findById(1));
}else {
System.out.println("状态记录更新失败!");
}
}
@Test
public void testFindAll(){
List<User> users=dao.findAll();
for(User user:users){
System.out.println(user);
}
}
}
第三,四实训小结,在敲代码的过程中总会导错包,导致报错。还有在创建学校数据访问接口类collegedaolmpl时定义字符串更新数据代码少大还有多打了逗号都会导致报错,所以在每次打代码的时候都要多注意。