实训第三天 学生信息管理系统
实训目的:综合运用J2SE有关知识开发【学生信息管理系统】。主要涉及程序控制结构、面向对象编程、图形用户界面、Java数据库应用、MySQL数据库这几个方面的内容。通过本项目的开发,让学生了解系统开发的一般流程,初步了解MVC模式与单元测试,巩固所学的Java理论知识,提高Java数据库应用编程的基本技能。
知识点:
(1)Java基本语法
(2)Java流程控制(顺序、选择、循环)
(3)Java数据库编程(JDBC)
(4)Java面向对象编程(封装、继承、多态、接口)
(5)Java核心API(List、Vector……)
(6)MySQL数据库操作
(7)MVC模式分层架构(M:模型层、V:视图层、C:控制层)
一、测试Dao里面的,接着前一天的测试:(前一天已经测试了1,2,3,4)
测试StudentDaolmpl里面共有13个
1. int insert(Student student);
2. int deleteById(String id);
3. int deleteByClass(String clazz);
4.int deleteByDepartment(String department);
5. int update(Student student);
6.Student findById(String id);
7. List findByName(String name);
8. List findByClass(String clazz);
9.List findByDepartment(String department);
10.List findAll();
11. Vector findRowsBySex();
12. Vector findRowsByClass();
13. Vector findRowsByDepartment();
5.用update更新数据
6.编写测试插入的方法 findById():
@Test
public void testFindById(){
StudentDao dao=new StudentDaoImpl();
String id=“11040201”;
Student student=dao.findById(id);
System.out.println(student);
}
运行该测试方法,结果如下:
7.编写测试插入的方法 findByName():
@Test
public void testFindByName() {
StudentDao dao = new StudentDaoImpl();
String name = “李”;
List students = dao.findByName(name);
if (students.size() > 0) {
for (Student student : students) {
System.out.println(student);
}
} else {
System.out.println(“温馨提示:查无此人”);
}
}
运行该测试方法,结果如下:
可以打开数据库中的t_student检验一下是否正确
8.编写测试插入的方法 findByClass():
@Test
public void testFindByClass(){
StudentDao dao=new StudentDaoImpl();
String clazz=“10英教1班”;
List students=dao.findByClass(clazz);
for(Student student:students){
System.out.println(student);
}
}
运行该测试方法,结果如下:
可以打开数据库中的t_student检验一下是否正确
9.编写测试插入的方法 findByDepartment():
@Test
public void testFindByDepartment(){
StudentDao dao=new StudentDaoImpl();
String department=“外语系”;
List students=dao.findByDepartment(department);
for (Student student:students){
System.out.println(student);
}
}
运行该测试方法,结果如下:
可以打开数据库中的t_student检验一下是否正确
10.编写测试插入的方法 findAll():
@Test
public void testFindAll(){
StudentDao dao=new StudentDaoImpl();
String name="*";
List students=dao.findAll();
for (Student student:students){
System.out.println(student);
}
}
运行该测试方法,结果如下:
可以打开数据库中的t_student检验一下是否正确
11.编写测试插入的方法 findRowsBySex():
@Test
public void testFindRowsBySex(){
StudentDao dao=new StudentDaoImpl();
Vector rows=dao.findRowsBySex();
Iterator iterator=rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
运行该测试方法,结果如下:
可以打开数据库中的t_student检验一下是否正确
12.编写测试插入的方法 findRowsByClass():
@Test
public void testFindRowsByClass(){
StudentDao dao=new StudentDaoImpl();
String clazz=“10英教1班”;
Vector rows=dao.findRowsByClass();
List students=dao.findRowsByClass();
Iterator iterator=rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
运行该测试方法,结果如下:
可以打开数据库中的t_student检验一下是否正确
13.编写测试插入的方法 findRowsByDepartment():
@Test
public void testFindRowsByDepartment(){
StudentDao dao=new StudentDaoImpl();
String department=“外语系”;
Vector rows=dao.findRowsByDepartment();
Iterator iterator=rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
运行该测试方法,结果如下:
可以打开数据库中的t_student检验一下是否正确
二、测试UserDaolmpl:(一共有6个)
1.int insert(User user);
**2.**int deleteById(int id);
3. int update(User user);
4.User findById(int id);
5. List findAll();
**6.**User login(String username, String password);
1.编写测试插入的方法 insert():
@Test
public void testInsert(){
User user=new User();
user.setId(7);
user.setUsername(“张正敏”);
user.setPassword(“666666”);
user.setTelephone(“18780636650”);
user.setRegisterTime(new Timestamp(new Date().getTime()));
UserDao dao=new UserDaolmpl();
int count=dao.insert(user);
if(count>0){
System.out.println("恭喜,添加用户成功");
}else {
System.out.println("遗憾,添加用户失败");
}
}
运行该测试方法,结果如下:
可以打开数据库中的t_user检验一下是否正确
2.编写测试插入的方法 deleteById():
@Test
public void testDeleteById(){
UserDao dao=new UserDaolmpl();
int id=3;
int count=dao.deleteById(id);
if (count>0){
System.out.println(“恭喜,学生删除成功”);
}else {
System.out.println(“遗憾,学生删除失败”);
}
}
运行该测试方法,结果如下:
可以打开数据库中的t_user检验一下是否正确
3.编写测试插入的方法 update():
@Test
public void testUpdate(){
UserDao dao=new UserDaolmpl();
User user = dao.findById(1);
user.setUsername(“张3敏”);
user.setTelephone(“18780636650”);
int count = dao.update(user);
if (count > 0) {
System.out.println(“恭喜,状态更新记录成功”);
user = dao.findById(1);
System.out.println(user);
} else {
System.out.println(“遗憾,状态更新记录失败”);
}
}
运行该测试方法,结果如下:
可以打开数据库中的t_user检验一下是否正确
4.编写测试插入的方法findById():
@Test
public void testFindById(){
UserDao dao=new UserDaolmpl();
int id=3;
User user=dao.findById(id);
System.out.println(user);
}
运行该测试方法,结果如下:
可以打开数据库中的t_user检验一下是否正确
5.编写测试插入的方法findAll():
@Test
public void testFindAll(){
UserDao dao=new UserDaolmpl();
String username="*";
List users=dao.findAll();
for (User user:users){
System.out.println(user);
}
}
运行该测试方法,结果如下:
可以打开数据库中的t_user检验一下是否正确
6.编写测试插入的方法login():
@Test
public void testLogin(){
UserDao dao=new UserDaolmpl();
String username,password;
username=“admin”;
password=“12345”;
User user=dao.login(username,password);
if(user!=null){
System.out.println(“恭喜,用户名与密码正确”);
}else {
System.out.println(“遗憾,用户名与密码错误”);
}
}
运行该测试方法,结果如下:
可以打开数据库中的t_user检验一下是否正确
**三、**服务接口
1)学校服务接口CollegeService
package net.zzm.student.service;
import net.zzm.student.bean.College;
public interface CollegeService {
College findCollegeById(int id);
int updateCollege(College college);
}
2)状态服务接口StatusService
package net.zzm.student.service;
import net.zzm.student.bean.Status;
public interface StatusService {
Status findStatusById(int id);
int updateStatus(Status status);
}
3)学生服务接口StudentService
package net.zzm.student.service;
import net.zzm.student.bean.Student;
import java.awt.*;
import java.util.List;
import java.util.Vector;
public interface StudentService {
int addStudent(Student student);
int deleteStudentById(String id);
int deleteStudentByClass(String clazz);
int deleteStudentByDepartment(String department);
int updateStudent(Student student);
Student findStudentById(String id);
List findStudentByName(String name);
List findStudentByClass(String clazz);
List findStudentByDepartment(String department);
List findAllStudent();
Vector findRowsBySex();
Vector findRowsByClass();
Vector findRowsByDepartment();
}
4)用户服务接口UserService
import net.zzm.student.bean.User;
import java.util.List;
public interface UserService {
int addUser(User user);
int deleteUserById(int id);
int updateUser(User user);
User findUserId(int id);
List findAllUsers();
User login(String username,String password);
}
四、服务接口实现类
1)学校服务接口实现类CollegeServiceImpl
package net.zzm.student.service.impl;
import net.zzm.student.bean.College;
import net.zzm.student.dao.CollegeDao;
import net.zzm.student.dao.impl.CollegeDaolmpl;
import net.zzm.student.service.CollegeService;
public class CollegeServicelmpl implements CollegeService {
private CollegeDao collegeDao=new CollegeDaolmpl();
@Override
public College findCollegeById(int id) {
return collegeDao.findById(id);
}
@Override
public int updateCollege(College college) {
return collegeDao.update(college);
}
}
在net.zzm.student.test包里创建测试类TestCollegeServiceImpl:
在这里面要测试两个
College findCollegeById(int id);
int updateCollege(College college);
@1。编写测试插入的方法FindCollegeByIdl():
@Test
public void testFindCollegeById(){
CollegeService service=new CollegeServicelmpl();
College college=service.findCollegeById(1);
System.out.println(college);
}
运行该测试方法,结果如下:
@2.编写测试插入的方法UpdateCollegel()
@Test
public void testUpdateCollege(){
CollegeService service=new CollegeServicelmpl();
College college=service.findCollegeById(1);
college.setPresident(“贺成”);
college.setTelephone(“3152639”);
int count=service.updateCollege(college);
if (count>0){
System.out.println(“恭喜,学校记录更新成功”);
college=service.findCollegeById(1);
System.out.println(college);
}else {
System.out.println(“遗憾,学校记录更新失败”);
}
}
}
运行该测试方法,结果如下:
2)状态服务接口实现类StatusServiceImpl
package net.zzm.student.service.impl;
import net.zzm.student.bean.Status;
import net.zzm.student.dao.StatusDao;
import net.zzm.student.dao.impl.StatusDaolmpl;
import net.zzm.student.service.StatusService;
public class StatusServiceImpl implements StatusService {
private StatusDao statusDao=new StatusDaolmpl();
@Override
public Status findStatusById(int id){
return statusDao.findById(id);
}
@Override
public int updateStatus(Status status) {
return statusDao.update(status);
}
}
在net.zzm.student.test包里创建测试类TestStatusServiceImpl:
这个里面要测试两个
Status findStatusById(int id);
int updateStatus(Status status);
@1.编写测试插入的方法findStatusById():
@Override
public Status findStatusById(int id){
return statusDao.findById(id);
}
编写测试方法testFindStatusById()
@Test
public void testFindStatusById(){
StatusService service=new StatusServiceImpl();
Status status=service.findStatusById(1);
System.out.println(status);
}
运行该测试方法,结果如下:
@2.编写测试插入的方法updateStatus()
@Override
public int updateStatus(Status status) {
return statusDao.update(status);
}
编写测试方法updateStatus()
3)学生服务接口实现类StudentServiceImpl
package net.zzm.student.service.impl;
import net.zzm.student.bean.Student;
import net.zzm.student.dao.StudentDao;
import net.zzm.student.dao.impl.StudentDaoImpl;
import net.zzm.student.service.StudentService;
import java.util.List;
import java.util.Vector;
public class StudentServicelmpl implements StudentService {
private StudentDao studentDao=new StudentDaoImpl();
@Override
public int addStudent(Student student) {
return studentDao.insert(student);
}
@Override
public int deleteStudentById(String id) {
return studentDao.deleteById(id);
}
@Override
public int deleteStudentByClass(String clazz) {
return studentDao.deleteByClass(clazz);
}
@Override
public int deleteStudentByDepartment(String department) {
return studentDao.deleteByDepartment(department);
}
@Override
public int updateStudent(Student student) {
return studentDao.update(student);
}
@Override
public Student findStudentById(String id) {
return studentDao.findById(id);
}
@Override
public List<Student> findStudentByName(String name) {
return studentDao.findByName(name);
}
@Override
public List<Student> findStudentByClass(String clazz) {
return studentDao.findByClass(clazz);
}
@Override
public List<Student> findStudentByDepartment(String department) {
return studentDao.findByDepartment(department);
}
@Override
public List<Student> findAllStudent() {
return studentDao.findAll();
}
@Override
public Vector findRowsBySex() {
return studentDao.findRowsBySex();
}
@Override
public Vector findRowsByClass() {
return studentDao.findRowsByClass();
}
@Override
public Vector findRowsByDepartment() {
return studentDao.findRowsByDepartment();
}
}
在net.zzm.student.test包里创建测试类TestStudentServiceImpl:
编写测试方法testFindStudentsByName():
一共有13种方法要测试
1.int addStudent(Student student);
2. int deleteStudentById(String id);
3. int deleteStudentByClass(String clazz);
4. int deleteStudentByDepartment(String department);
5. int updateStudent(Student student);
6. Student findStudentById(String id);
7. List findStudentByName(String name);
8. List findStudentByClass(String clazz);
9. List findStudentByDepartment(String department);
10. List findAllStudent();
11. Vector findRowsBySex();
12. Vector findRowsByClass();
13. Vector findRowsByDepartment();
@1…编写测试插入的方法addStudent():
@Test
public void testAddStudent(){
Student student=new Student();
student.setId(“18242026”);
student.setName(“张敏”);
student.setSex(“女”);
student.setAge(19);
student.setDepartment(“信息工程学院”);
student.setClazz(“18大数据1班”);
student.setTelephone(“18780636650”);
StudentService service=new StudentServicelmpl();
int count=service.addStudent(student);
if (count>0){
System.out.println( "恭喜,添加学生成功");
}else {
System.out.println("遗憾,添加学生失败");
}
}
运行该测试方法,结果如下: @2.编写测试插入的方法deleteStudentById():
@Test
public void testDeleteStudentById(){
StudentService service=new StudentServicelmpl();
String id=“10080301”;
int count=service.deleteStudentById(id);
if(count>0){
System.out.println(“恭喜,学生删除成功”);
}else {
System.out.println(“遗憾,学生删除失败”);
}
}
运行该测试方法,结果如下:
可打开数据库查看是否正确
@3…编写测试插入的方法deleteStudentByClass():
@Test
public void testDeleteStudentByClass(){
StudentService service=new StudentServicelmpl();
String clazz = “18大数据1班”;
int count=service.deleteStudentByClass(clazz);
if (count >0){
System.out.println(“恭喜,班级删除成功”);
}else {
System.out.println(“遗憾,班级删除失败”);
}
}
运行该测试方法,结果如下:
如果没有改班级则删除不成功
可以打开t_student查看是否正确
@4…编写测试插入的方法deleteStudentByDepartment():
@Test
public void testDeleteStudentByDepartment(){
StudentService service=new StudentServicelmpl();
String department=“信息工程学院”;
int count=service.deleteStudentByDepartment(department);
if (count>0){
System.out.println(“恭喜,系部删除成功”);
}else {
System.out.println(“遗憾,系部删除失败”);
}
}
运行该测试方法,结果如下:
如果没有改班级则删除不成功
可以打开t_student查看是否正确如果没有改系部则不成功
可以打开t_student查看是否正确
@5…编写测试插入的方法updateStuden():
可以打开t_student查看是否正确
@6.编写测试插入的方法 findStudentById():
@Test
public void testFindStudentById(){
StudentService service=new StudentServicelmpl();
String id=“11040201”;
Student student=service.findStudentById(id);
System.out.println(student);
}
运行该测试方法,结果如下:
@7.编写测试插入的方法 findStudentByName():
@Test
public void testFindStudentByName(){
StudentService service=new StudentServicelmpl();
String name = “李”;
List students = service.findStudentByName(name);
if (students.size() > 0) {
for (Student student : students) {
System.out.println(student);
}
} else {
System.out.println(“温馨提示:查无此人”);
}
}
运行该测试方法,结果如下:
可打开数据库查看
@8.编写测试插入的方法 findStudentByClass():
@Test
public void testFindStudentByClass(){
StudentService service=new StudentServicelmpl();
String clazz=“10英教1班”;
List students=service.findStudentByClass(clazz);
for(Student student:students){
System.out.println(student);
}
}
运行该测试方法,结果如下:
可打开数据库查看
@9.编写测试插入的方法findStudentByDepartment():
@Test
public void testFindStudentByDepartment(){
StudentService service=new StudentServicelmpl();
String department=“外语系”;
List students=service.findStudentByDepartment(department);
for (Student student:students){
System.out.println(student);
}
}
运行该测试方法,结果如下:
可打开数据库查看
@10.编写测试插入的方法 findAllStudent():
@Test
public void testFindAllStudent(){
StudentService service=new StudentServicelmpl();
String name="*";
List students=service.findAllStudent();
for (Student student:students){
System.out.println(student);
}
}
运行该测试方法,结果如下:
可打开数据库查看
@11.编写测试插入的方法findRowsBySex():
@Test
public void testFindRowsBySex(){
StudentService service=new StudentServicelmpl();
Vector rows=service.findRowsBySex();
Iterator iterator=rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
运行该测试方法,结果如下:
可打开数据库查看
@12.编写测试插入的方法 findRowsByClass():
@Test
public void testFindRowsByClass(){
StudentService service=new StudentServicelmpl();
String clazz=“10英教1班”;
Vector rows=service.findRowsByClass();
List students=service.findStudentByClass(clazz);
Iterator iterator=rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
运行该测试方法,结果如下:
可打开数据库查看
@13.编写测试插入的方法findRowsByDepartment():
@Test
public void testFindRowsByDepartment(){
StudentService service=new StudentServicelmpl();
String department=“外语系”;
Vector rows=service.findRowsByDepartment();
Iterator iterator=rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
运行该测试方法,结果如下:
可打开数据库查看
4)用户服务接口实现类UserServiceImpl
package net.zzm.student.service.impl;
import net.zzm.student.bean.User;
import net.zzm.student.dao.UserDao;
import net.zzm.student.dao.impl.UserDaolmpl;
import net.zzm.student.service.UserService;
import java.util.List;
public class UserServiceImpl implements UserService {
private UserDao userDao=new UserDaolmpl();
@Override
public int addUser(User user) {
return userDao.insert(user);
}
@Override
public int deleteUserById(int id) {
return userDao.deleteById(id);
}
@Override
public int updateUser(User user) {
return userDao.update(user);
}
@Override
public User findUserId(int id) {
return userDao.findById(id);
}
@Override
public List<User> findAllUsers() {
return userDao.findAll();
}
@Override
public User login(String username, String password) {
return userDao.login(username, password);
}
}
在net.zzm.student.test包里创建测试类TestUserServiceImpl:
这李一共有6种方法:
**1.**int addUser(User user);
2. int deleteUserById(int id);
**3.**int updateUser(User user);
**4.**User findUserId(int id);
5.List findAllUsers();
6.User login(String username,String password);
aaa@qq.com编写测试方法addUser()方法:
@Test
public void tesAddUsers() {
User user = new User();
user.setUsername(“张琴”);
user.setPassword(“777777”);
user.setRegisterTime(new Timestamp(new Date().getTime()));
user.setTelephone(“18780636650”);
UserDao dao = new UserDaolmpl();
int count = dao.insert(user);
if (count > 0) {
System.out.println("恭喜,添加学生成功");
} else {
System.out.println("遗憾,添加学生失败");
}
运行该测试方法,结果如下:
@2.编写测试方法deleteUserById()方法:
@Test
public void testDeleteUserById() {
UserService service = new UserServiceImpl();
int id = 1;
int count = service.deleteUserById(id);
if (count > 0) {
System.out.println(“恭喜,学生删除成功”);
} else {
System.out.println(“遗憾,学生删除失败”);
}
}
运行该测试方法,结果如下:
@3. 编写测试方法updateUser()方法:
运行该测试方法,结果如下:
@4.编写测试方法findUserId()方法:
@Test
public void testFindUserId() {
UserService service = new UserServiceImpl();
int id = 1;
User user = service.findUserId(id);
System.out.println(user);
}
运行该测试方法,结果如下:
@5.编写测试方法 findAllUsers()方法:
@Test
public void testFindAllUsers() {
UserService service = new UserServiceImpl();
String username = “*”;
List users = service.findAllUsers();
for (User user : users) {
System.out.println(user);
}
}
运行该测试方法,结果如下:
@6.编写测试方法 login()方法:
@Test
public void testLogin() {
UserService service = new UserServiceImpl();
String username, password;
username = "admin";
password = "12345";
User user = service.login(username, password);
if (user != null) {
System.out.println("恭喜,用户名与密码正确");
} else {
System.out.println("遗憾,用户名与密码错误");
}
}
运行该测试方法,结果如下: