package student.service;
import java.util.List;
import student.entity.Student;
public interface IStudentService {
//查询全部学生信息
public List queryAllStudents();
//按照学号查询学生信息
public Student queryStudentBySno(int sno);
//删除学生信息
public boolean deleteStudentBySno(int sno) ;
//更改学生信息
public boolean updateStudentBySno(int sno, Student student) ;
//增加学生信息
public boolean addStudent(Student student) ;
//查询总数据
public int getTotalCount();
//分页
public List queryStudentsByPage(int current, int pageSize);
//检查登陆账户和密码
public boolean checkLoginID(String ID,String pwd);
//注册账户
public boolean addLoginID(String ID, String pwd);
//更改密码
public boolean updateLoginPwd(String ID,String pwd1);
//注销账号
public boolean deleteLoginID(String ID);
//判断ID是否存在
public boolean IDExist(String ID);
//判断上传作业输入的学生信息是否存在
public boolean upLoadWork(int sno, String name);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
接口的实现类(StudentServiceImpl.java)
package student.service.impl;
import java.util.List;
import student.dao.IStudentDao;
import student.dao.impl.StudentDaoImpl;
import student.entity.Student;
import student.service.IStudentService;
import student.util.DBUtil;
//业务逻辑层:逻辑性的增删改查(增:查+增),对dao层进行的组装
public class StudentServiceImpl implements IStudentService{
IStudentDao studentDao = new StudentDaoImpl();
//查询全部学生信息
public List queryAllStudents(){
return studentDao.queryAllStudents();
}
//按照学号查询学生信息
public Student queryStudentBySno(int sno) {
return studentDao.queryStudentBySno(sno);
}
//删除学生信息
public boolean deleteStudentBySno(int sno) {
if(studentDao.isExist(sno)) {
return studentDao.deleteStudentBySno(sno);
}
return false;
}
//更改学生信息
public boolean updateStudentBySno(int sno, Student student) {
return studentDao.updateStudentBySno(sno, student);
}
//增加学生信息
public boolean addStudent(Student student) {
if(!studentDao.isExist(student.getSno())) {
studentDao.addStudent(student);
return true;
}else {
System.out.println("学号重复!");
return false;
}
}
//查询总条数
@Override
public int getTotalCount() {
return studentDao.getTotalCount();
}
//查询当前页的数据集合
@Override
public List queryStudentsByPage(int current, int pageSize) {
return studentDao.queryStudentByPage(current, pageSize);
}
@Override
public boolean checkLoginID(String ID, String pwd) {
return studentDao.checkLoginID(ID, pwd);
}
@Override
public boolean addLoginID(String ID, String pwd) {
return studentDao.addLoginID(ID,pwd);
}
@Override
public boolean updateLoginPwd(String ID, String pwd1) {
return studentDao.updateLoginPwd(ID,pwd1);
}
@Override
public boolean deleteLoginID(String ID) {
return studentDao.deleteLoginID(ID);
}
@Override
public boolean IDExist(String ID) {
return studentDao.IDExist(ID);
}
@Override
public boolean upLoadWork(int sno, String name) {
return studentDao.upLoadWork(sno,name);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
③数据访问层Dao
接口(IStudentDao.java)
package student.dao;
import java.util.List;
import student.entity.Student;
public interface IStudentDao {
public boolean updateLoginPwd(String ID,String pwd1);
//查询全部学生信息
public List queryAllStudents();
//判断此人是否存在
public boolean isExist(int sno) ;
//增加学生信息
public boolean addStudent(Student student);
//删除学生信息
public boolean deleteStudentBySno(int sno);
//根据sno找到要修改的学生,然后再进行修改
public boolean updateStudentBySno(int sno,Student student);
//根据学号查询学生信息
public Student queryStudentBySno(int sno);
//查询总数据数
public int getTotalCount();
//currentPage:当前页(页码)pageSize:页面大小(每页显示的数据条数)
public List queryStudentByPage(int currentPage,int pageSize);
public boolean checkLoginID(String ID,String pwd);
public boolean addLoginID(String ID, String pwd);
public boolean deleteLoginID(String ID);
public boolean IDExist(String ID);
public boolean upLoadWork(int sno, String name);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
接口的实现类(StudentDaoImpl.java)
package student.dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import student.dao.IStudentDao;
import student.entity.Student;
import student.util.DBUtil;
public class StudentDaoImpl implements IStudentDao{
private final String URL = "jdbc:mysql://localhost:3306/STUDENT?useSSL=false&serverTimezone=UTC";
private final String UserName = "root";
private final String Pwd = "123456";
String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
//查询全部学生信息
public List queryAllStudents(){
PreparedStatement pstmt = null;
Student student = null;
List students = new ArrayList<>();
ResultSet rs = null;
try {
String sql = "select * from student1";
rs = DBUtil.executeQuery(sql, null);
while(rs.next()) {
int sno= rs.getInt("sno");
String name = rs.getString("name");
int age = rs.getInt("age");
String dept = rs.getString("dept");
student = new Student(name, sno, age, dept);
students.add(student);
}
return students;
} catch(Exception e) {
e.printStackTrace();
return null;
}
finally {
DBUtil.closeAll(rs, pstmt, DBUtil.connection);
}
}
//判断此人是否存在
public boolean isExist(int sno) {
return queryStudentBySno(sno) == null? false:true;
}
//增加学生信息
public boolean addStudent(Student student) {
String sql = "insert into student1(name,sno,age,dept) values(?,?,?,?)";
Object[] params = {student.getName(),student.getSno(),student.getAge(),student.getDept()};
return DBUtil.executeUpdate(sql, params);
}
//删除学生信息
public boolean deleteStudentBySno(int sno) {
String sql = "delete from student1 where sno =?";
Object[] params = {sno};
return DBUtil.executeUpdate(sql, params);
}
//根据sno找到要修改的学生,然后再进行修改
public boolean updateStudentBySno(int sno,Student student) {
String sql = "update student1 set name =?,age=?,dept=? where sno=?";
Object[] params = {student.getName(),student.getAge(),student.getDept(),sno};
return DBUtil.executeUpdate(sql, params);
}
//根据学号查询学生信息
public Student queryStudentBySno(int sno){
PreparedStatement pstmt = null;
Student student = null;
Connection connection = null;
ResultSet rs = null;
try {
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(URL,UserName,Pwd);
String sql = "select * from student1 where sno = ?";
pstmt = connection.prepareStatement(sql);
pstmt.setInt(1, sno);
rs = pstmt.executeQuery();
if(rs.next()) {
int no= rs.getInt("sno");
String name = rs.getString("name");
int age = rs.getInt("age");
String dept = rs.getString("dept");
student = new Student(name, no, age, dept);
}
return student;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch(SQLException e) {
e.printStackTrace();
return null;
}catch(Exception e) {
e.printStackTrace();
return null;
}
finally {
DBUtil.closeAll(rs, pstmt, DBUtil.connection);
}
}
@Override
public int getTotalCount() {//查询总数据数
String sql = "select count(1) from student1";
return DBUtil.getTotalCount(sql);
}
@Override
public List queryStudentByPage(int currentPage, int pageSize) {
String sql = "select * from student1 order by sno asc limit ?,?";
Object[] params = {currentPage*pageSize,pageSize};
List students = new ArrayList<>();
ResultSet rs = DBUtil.executeQuery(sql, params);
try {
while(rs.next()) {
Student student = new Student(rs.getString("name"),rs.getInt("sno"),rs.getInt("age"),rs.getString("dept"));
students.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return students;
}
@Override
public boolean checkLoginID(String ID, String pwd){
int count = 0;
String sql = "select * from login where ID=? and pwd=?";
Object[] params = {ID,pwd};
ResultSet rs = DBUtil.executeQuery(sql, params);
try {
while(rs.next()) {
count++;
}
if(count>0)
return true;
else
return false;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean addLoginID(String ID, String pwd) {
// TODO Auto-generated method stub
String sql = "insert into login(ID,pwd) values(?,?)";
Object[] params = {ID,pwd};
return DBUtil.executeUpdate(sql, params);
}
@Override
public boolean updateLoginPwd(String ID, String pwd1) {
String sql = "update login set pwd =? where ID=?";
Object[] params = {pwd1,ID};
return DBUtil.executeUpdate(sql, params);
}
@Override
public boolean deleteLoginID(String ID) {
String sql = "delete from login where ID =?";
Object[] params = {ID};
return DBUtil.executeUpdate(sql, params);
}
@Override
public boolean IDExist(String ID) {
String sql = "select *from login where ID = ?";
Object[] params = {ID};
return DBUtil.executeUpdate(sql, params);
}
@Override
public boolean upLoadWork(int sno, String name) {
int count = 0;
String sql = "select *from student1 where sno = ? and name = ?";
Object[] params = {sno,name};
ResultSet rs = DBUtil.executeQuery(sql, params);
try {
while(rs.next()) {
count++;
}
if(count>0)
return true;
else
return false;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
package student.entity;
import java.util.List;
//分页帮助类
public class Page {
private int currentPage;
private int pageSize;
private int totalCount;
private int totalPage;
private List students;
public Page() {
}
public Page(int currentPage, int pageSize, int totalCount, int totalPage, List students) {
this.currentPage = currentPage;
this.pageSize = pageSize;
this.totalCount = totalCount;
this.totalPage = totalPage;
this.students = students;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
this.totalPage = this.totalCount%this.pageSize==0?this.totalCount/this.pageSize:this.totalCount/this.pageSize+1;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public List getStudents() {
return students;
}
public void setStudents(List students) {
this.students = students;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
②封装学生信息(Student.java)
package student.entity;
public class Student {
private String name;
private int sno;
private int age;
private String dept;
public Student(int sno) {
this.sno = sno;
}
public Student() {
}
public Student(String name, int age, String dept) {
this.name = name;
this.age = age;
this.dept = dept;
}
public Student(String name, int sno, int age, String dept) {
this.name = name;
this.sno = sno;
this.age = age;
this.dept = dept;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSno() {
return sno;
}
public void setSno(int sno) {
this.sno = sno;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String toString() {
return this.getSno()+"-"+this.getName()+"-"+this.getAge()+"-"+this.getDept();
}
}