Added database operation: StudentOA, TeacherOA, CourseOA, AttendanceOA

This commit is contained in:
2022-06-09 11:17:13 +08:00
parent 4091f558d1
commit c35c84d66c
12 changed files with 566 additions and 20 deletions

1
.idea/sqldialects.xml generated
View File

@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="SqlDialectMappings"> <component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/Library/src/test/java/com/cfive/classroom/library/database/DatabaseTest.java" dialect="MySQL" />
<file url="file://$PROJECT_DIR$/MySQL.sql" dialect="MySQL" /> <file url="file://$PROJECT_DIR$/MySQL.sql" dialect="MySQL" />
<file url="PROJECT" dialect="MySQL" /> <file url="PROJECT" dialect="MySQL" />
</component> </component>

View File

@@ -1,19 +1,15 @@
package com.cfive.classroom.library.database; package com.cfive.classroom.library.database;
import com.cfive.classroom.library.database.bean.AClass; import com.cfive.classroom.library.database.bean.*;
import com.cfive.classroom.library.database.bean.Faculty; import com.cfive.classroom.library.database.operation.*;
import com.cfive.classroom.library.database.bean.Major;
import com.cfive.classroom.library.database.bean.Subject;
import com.cfive.classroom.library.database.operation.ClassOA;
import com.cfive.classroom.library.database.operation.FacultyOA;
import com.cfive.classroom.library.database.operation.MajorOA;
import com.cfive.classroom.library.database.operation.SubjectOA;
import com.cfive.classroom.library.database.util.AlreadyExistsException; import com.cfive.classroom.library.database.util.AlreadyExistsException;
import com.cfive.classroom.library.database.util.DependenciesNotFoundException; import com.cfive.classroom.library.database.util.DependenciesNotFoundException;
import com.cfive.classroom.library.database.util.InsertException; import com.cfive.classroom.library.database.util.InsertException;
import com.cfive.classroom.library.database.util.NoConfigException; import com.cfive.classroom.library.database.util.NoConfigException;
import com.sun.istack.internal.Nullable;
import java.sql.SQLException; import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
public class DatabaseHelper { public class DatabaseHelper {
@@ -177,7 +173,85 @@ public class DatabaseHelper {
return ClassOA.delete(majorID, grade, classNum); return ClassOA.delete(majorID, grade, classNum);
} }
public static List<Student> selectAllFromStudent() throws NoConfigException, SQLException {
return StudentOA.selectAll();
}
public static Student selectFromStudent(int stuID) throws NoConfigException, SQLException {
return StudentOA.select(stuID);
}
public static Student insertIntoStudent(long stuID, String stuName, Gender gender, long classID, String passwd, String salt) throws NoConfigException, SQLException, InsertException, AlreadyExistsException, DependenciesNotFoundException {
return StudentOA.insert(stuID, stuName, gender, classID, passwd, salt);
}
public static boolean isExistsInStudent(int stuID) throws NoConfigException, SQLException {
return StudentOA.isExists(stuID);
}
public static boolean deleteFromStudent(int stuID) throws NoConfigException, SQLException {
return StudentOA.delete(stuID);
}
public static List<Teacher> selectAllFromTeacher() throws NoConfigException, SQLException {
return TeacherOA.selectAll();
}
public static Teacher selectFromTeacher(int tchID) throws NoConfigException, SQLException {
return TeacherOA.select(tchID);
}
public static Teacher insertIntoTeacher(long tchID, String tchName, Gender gender, int facID, String passwd, String salt) throws NoConfigException, SQLException, InsertException, AlreadyExistsException, DependenciesNotFoundException {
return TeacherOA.insert(tchID, tchName, gender, facID, passwd, salt);
}
public static boolean isExistsInTeacher(int tchID) throws NoConfigException, SQLException {
return TeacherOA.isExists(tchID);
}
public static boolean deleteFromTeacher(int tchID) throws NoConfigException, SQLException {
return TeacherOA.delete(tchID);
}
public static List<Course> selectAllFromCourse() throws NoConfigException, SQLException {
return CourseOA.selectAll();
}
public static Course selectFromCourse(int courID) throws NoConfigException, SQLException {
return CourseOA.select(courID);
}
public static Course insertIntoCourse(long courID, int subID, long tchID, LocalDateTime courTimeStart, LocalDateTime courTimeEnd) throws NoConfigException, SQLException, InsertException, AlreadyExistsException, DependenciesNotFoundException {
return CourseOA.insert(courID, subID, tchID, courTimeStart, courTimeEnd);
}
public static boolean isExistsInCourse(int courID) throws NoConfigException, SQLException {
return CourseOA.isExists(courID);
}
public static boolean deleteFromCourse(int courID) throws NoConfigException, SQLException {
return CourseOA.delete(courID);
}
public static List<Attendance> selectAllFromAttendance() throws NoConfigException, SQLException {
return AttendanceOA.selectAll();
}
public static Attendance selectFromAttendance(String attID) throws NoConfigException, SQLException {
return AttendanceOA.select(attID);
}
public static Attendance insertIntoAttendance(long courID, long stuID, @Nullable LocalDateTime attTime, AttStatus attStatus) throws NoConfigException, SQLException, InsertException, AlreadyExistsException, DependenciesNotFoundException {
return AttendanceOA.insert(courID, stuID, attTime, attStatus);
}
public static boolean isExistsInAttendance(String attID) throws NoConfigException, SQLException {
return AttendanceOA.isExists(attID);
}
public static boolean deleteFromAttendance(String attID) throws NoConfigException, SQLException {
return AttendanceOA.delete(attID);
}
public static void close() { public static void close() {
PoolHelper.close(); PoolHelper.close();

View File

@@ -19,4 +19,29 @@ public enum AttStatus {
public String toString() { public String toString() {
return string; return string;
} }
public static AttStatus fromString(String s) {
if (s.equals("signed")) {
return signed;
}
if (s.equals("absence")) {
return absence;
}
if (s.equals("personal_leave")) {
return personal_leave;
}
if (s.equals("sick_leave")) {
return sick_leave;
}
if (s.equals("public_holiday")) {
return public_holiday;
}
if (s.equals("late")) {
return late;
}
if (s.equals("leave_early")) {
return leave_early;
}
return not_signed;
}
} }

View File

@@ -0,0 +1,111 @@
package com.cfive.classroom.library.database.operation;
import com.cfive.classroom.library.database.PoolHelper;
import com.cfive.classroom.library.database.bean.*;
import com.cfive.classroom.library.database.util.AlreadyExistsException;
import com.cfive.classroom.library.database.util.DependenciesNotFoundException;
import com.cfive.classroom.library.database.util.InsertException;
import com.cfive.classroom.library.database.util.NoConfigException;
import com.sun.istack.internal.Nullable;
import java.sql.*;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class AttendanceOA {
public static List<Attendance> selectAll() throws NoConfigException, SQLException {
String sql = "SELECT attID,attTime,attStatus,student.stuID,stuName,stuGender,student.passwd,student.salt,class.classID,grade,classNum,major.majorID,majorName,course.courID,courTimeFrom,courTimeEnd,subject.subID,teacher.tchID,tchName,tchGender,teacher.passwd,teacher.salt,faculty.facID,facName FROM attendance,student,class,major,course,subject,teacher,faculty where attendance.courID=course.courID AND attendance.stuID=student.stuID AND class.classID=student.classID AND class.majorID=major.majorID AND major.facID=faculty.facID AND course.subID=subject.subID AND course.tchID=teacher.tchID AND teacher.facID=faculty.facID ORDER BY courID,attTime,attStatus";
ArrayList<Attendance> attendances = new ArrayList<>();
try (Connection connection = PoolHelper.getConnection()) {
try (Statement statement = connection.createStatement()) {
try (ResultSet resultSet = statement.executeQuery(sql)) {
while (resultSet.next()) {
Subject subject = new Subject(resultSet.getInt("subID"), resultSet.getString("subName"));
Faculty faculty = new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"));
Teacher teacher = new Teacher(resultSet.getLong("tchID"), resultSet.getString("tchName"), resultSet.getString("tchGender").equals("m") ? Gender.m : Gender.f, faculty, resultSet.getString("teacher.passwd"), resultSet.getString("teacher.salt"));
Course course = new Course(resultSet.getLong("courID"), subject, teacher, LocalDateTime.ofEpochSecond(resultSet.getLong("courTimeFrom"), 0, ZoneOffset.UTC), LocalDateTime.ofEpochSecond(resultSet.getLong("courTimeEnd"), 0, ZoneOffset.UTC));
Major major = new Major(resultSet.getInt("majorID"), resultSet.getString("majorName"), faculty);
AClass aClass = new AClass(resultSet.getLong("classID"), major, resultSet.getInt("grade"), resultSet.getInt("classNum"));
Student student = new Student(resultSet.getLong("stuID"), resultSet.getString("stuName"), resultSet.getString("stuGender").equals("m") ? Gender.m : Gender.f, aClass, resultSet.getString("student.passwd"), resultSet.getString("student.salt"));
attendances.add(new Attendance(resultSet.getString("attID"), course, student, LocalDateTime.ofEpochSecond(resultSet.getLong("attTime"), 0, ZoneOffset.UTC), AttStatus.fromString(resultSet.getString("attStatus"))));
}
}
}
}
return attendances;
}
public static Attendance select(String attID) throws SQLException, NoConfigException {
String sql = "SELECT attID,attTime,attStatus,student.stuID,stuName,stuGender,student.passwd,student.salt,class.classID,grade,classNum,major.majorID,majorName,course.courID,courTimeFrom,courTimeEnd,subject.subID,teacher.tchID,tchName,tchGender,teacher.passwd,teacher.salt,faculty.facID,facName FROM attendance,student,class,major,course,subject,teacher,faculty where attendance.courID=course.courID AND attendance.stuID=student.stuID AND class.classID=student.classID AND class.majorID=major.majorID AND major.facID=faculty.facID AND course.subID=subject.subID AND course.tchID=teacher.tchID AND teacher.facID=faculty.facID AND attID=?";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setString(1, attID);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
Subject subject = new Subject(resultSet.getInt("subID"), resultSet.getString("subName"));
Faculty faculty = new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"));
Teacher teacher = new Teacher(resultSet.getLong("tchID"), resultSet.getString("tchName"), resultSet.getString("tchGender").equals("m") ? Gender.m : Gender.f, faculty, resultSet.getString("teacher.passwd"), resultSet.getString("teacher.salt"));
Course course = new Course(resultSet.getLong("courID"), subject, teacher, LocalDateTime.ofEpochSecond(resultSet.getLong("courTimeFrom"), 0, ZoneOffset.UTC), LocalDateTime.ofEpochSecond(resultSet.getLong("courTimeEnd"), 0, ZoneOffset.UTC));
Major major = new Major(resultSet.getInt("majorID"), resultSet.getString("majorName"), faculty);
AClass aClass = new AClass(resultSet.getLong("classID"), major, resultSet.getInt("grade"), resultSet.getInt("classNum"));
Student student = new Student(resultSet.getLong("stuID"), resultSet.getString("stuName"), resultSet.getString("stuGender").equals("m") ? Gender.m : Gender.f, aClass, resultSet.getString("student.passwd"), resultSet.getString("student.salt"));
return new Attendance(resultSet.getString("attID"), course, student, LocalDateTime.ofEpochSecond(resultSet.getLong("attTime"), 0, ZoneOffset.UTC), AttStatus.fromString(resultSet.getString("attStatus")));
}
}
}
}
return null;
}
public static Attendance insert(long courID, long stuID, @Nullable LocalDateTime attTime, AttStatus attStatus) throws NoConfigException, SQLException, AlreadyExistsException, DependenciesNotFoundException, InsertException {
if (!CourseOA.isExists(courID) || !StudentOA.isExists(stuID)) throw new DependenciesNotFoundException();
String sql = "INSERT INTO course VALUES (?,?,?,?,?)";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
UUID uuid;
do {
uuid = UUID.randomUUID();
} while (!isExists(uuid.toString()));
preparedStatement.setString(1, uuid.toString());
preparedStatement.setLong(2, courID);
preparedStatement.setLong(3, stuID);
preparedStatement.setLong(4, attTime.toEpochSecond(ZoneOffset.UTC));
preparedStatement.setString(5, attStatus.name());
if (preparedStatement.executeUpdate() == 1) {
return new Attendance(uuid.toString(), CourseOA.select(courID), StudentOA.select(stuID), attTime, attStatus);
} else {
throw new InsertException();
}
}
}
}
public static boolean isExists(String attID) throws SQLException, NoConfigException {
String sql = "SELECT EXISTS(SELECT 1 FROM attendance WHERE attID=?)";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setString(1, attID);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
return resultSet.getBoolean(1);
}
}
}
}
return false;
}
public static boolean delete(String attID) throws NoConfigException, SQLException {
String sql = "DELETE FROM attendance WHERE attID=?";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setString(1, attID);
return preparedStatement.executeUpdate() == 1;
}
}
}
}

View File

@@ -22,7 +22,9 @@ public class ClassOA {
try (Statement statement = connection.createStatement()) { try (Statement statement = connection.createStatement()) {
try (ResultSet resultSet = statement.executeQuery(sql)) { try (ResultSet resultSet = statement.executeQuery(sql)) {
while (resultSet.next()) { while (resultSet.next()) {
AClasses.add(new AClass(resultSet.getLong("classID"), new Major(resultSet.getInt("majorID"), resultSet.getString("majorName"), new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"))), resultSet.getInt("grade"), resultSet.getInt("classNum"))); Faculty faculty = new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"));
Major major = new Major(resultSet.getInt("majorID"), resultSet.getString("majorName"), faculty);
AClasses.add(new AClass(resultSet.getLong("classID"), major, resultSet.getInt("grade"), resultSet.getInt("classNum")));
} }
} }
} }
@@ -59,7 +61,9 @@ public class ClassOA {
private static AClass getAClass(PreparedStatement preparedStatement) throws SQLException { private static AClass getAClass(PreparedStatement preparedStatement) throws SQLException {
try (ResultSet resultSet = preparedStatement.executeQuery()) { try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) { if (resultSet.next()) {
return new AClass(resultSet.getLong("classID"), new Major(resultSet.getInt("majorID"), resultSet.getString("majorName"), new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"))), resultSet.getInt("grade"), resultSet.getInt("classNum")); Faculty faculty = new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"));
Major major = new Major(resultSet.getInt("majorID"), resultSet.getString("majorName"), faculty);
return new AClass(resultSet.getLong("classID"), major, resultSet.getInt("grade"), resultSet.getInt("classNum"));
} }
} }
return null; return null;

View File

@@ -9,7 +9,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
public abstract class CommonOA { public abstract class CommonOA {
public static boolean isExists(String sql, int id) throws NoConfigException, SQLException { protected static boolean isExists(String sql, int id) throws NoConfigException, SQLException {
try (Connection connection = PoolHelper.getConnection()) { try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setInt(1, id); preparedStatement.setInt(1, id);
@@ -23,7 +23,7 @@ public abstract class CommonOA {
return false; return false;
} }
public static boolean isExists(String sql, String name) throws NoConfigException, SQLException { protected static boolean isExists(String sql, String name) throws NoConfigException, SQLException {
try (Connection connection = PoolHelper.getConnection()) { try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setString(1, name); preparedStatement.setString(1, name);

View File

@@ -0,0 +1,95 @@
package com.cfive.classroom.library.database.operation;
import com.cfive.classroom.library.database.PoolHelper;
import com.cfive.classroom.library.database.bean.*;
import com.cfive.classroom.library.database.util.*;
import java.sql.*;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
public class CourseOA {
public static List<Course> selectAll() throws NoConfigException, SQLException {
String sql = "SELECT courID,courTimeFrom,courTimeEnd,subject.subID,teacher.tchID,tchName,tchGender,passwd,salt,faculty.facID,facName FROM course,subject,teacher,faculty where course.subID=subject.subID AND course.tchID=teacher.tchID AND teacher.facID=faculty.facID ORDER BY courID";
ArrayList<Course> courses = new ArrayList<>();
try (Connection connection = PoolHelper.getConnection()) {
try (Statement statement = connection.createStatement()) {
try (ResultSet resultSet = statement.executeQuery(sql)) {
while (resultSet.next()) {
Subject subject = new Subject(resultSet.getInt("subID"), resultSet.getString("subName"));
Faculty faculty = new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"));
Teacher teacher = new Teacher(resultSet.getLong("tchID"), resultSet.getString("tchName"), resultSet.getString("tchGender").equals("m") ? Gender.m : Gender.f, faculty, resultSet.getString("passwd"), resultSet.getString("salt"));
courses.add(new Course(resultSet.getLong("courID"), subject, teacher, LocalDateTime.ofEpochSecond(resultSet.getLong("courTimeFrom"), 0, ZoneOffset.UTC), LocalDateTime.ofEpochSecond(resultSet.getLong("courTimeEnd"), 0, ZoneOffset.UTC)));
}
}
}
}
return courses;
}
public static Course select(long courID) throws SQLException, NoConfigException {
String sql = "SELECT courID,courTimeFrom,courTimeEnd,subject.subID,teacher.tchID,tchName,tchGender,passwd,salt,faculty.facID,facName FROM course,subject,teacher,faculty where course.subID=subject.subID AND course.tchID=teacher.tchID AND teacher.facID=faculty.facID AND courID=?";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setLong(1, courID);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
Subject subject = new Subject(resultSet.getInt("subID"), resultSet.getString("subName"));
Faculty faculty = new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"));
Teacher teacher = new Teacher(resultSet.getLong("tchID"), resultSet.getString("tchName"), resultSet.getString("tchGender").equals("m") ? Gender.m : Gender.f, faculty, resultSet.getString("passwd"), resultSet.getString("salt"));
return new Course(resultSet.getLong("courID"), subject, teacher, LocalDateTime.ofEpochSecond(resultSet.getLong("courTimeFrom"), 0, ZoneOffset.UTC), LocalDateTime.ofEpochSecond(resultSet.getLong("courTimeEnd"), 0, ZoneOffset.UTC));
}
}
}
}
return null;
}
public static Course insert(long courID, int subID, long tchID, LocalDateTime courTimeStart, LocalDateTime courTimeEnd) throws NoConfigException, SQLException, AlreadyExistsException, DependenciesNotFoundException, InsertException {
if (isExists(courID)) throw new AlreadyExistsException();
if (!SubjectOA.isExists(subID) || !TeacherOA.isExists(tchID)) throw new DependenciesNotFoundException();
String sql = "INSERT INTO course VALUES (?,?,?,?,?)";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setLong(1, courID);
preparedStatement.setInt(2, subID);
preparedStatement.setLong(3, tchID);
preparedStatement.setLong(4, courTimeStart.toEpochSecond(ZoneOffset.UTC));
preparedStatement.setLong(5, courTimeEnd.toEpochSecond(ZoneOffset.UTC));
if (preparedStatement.executeUpdate() == 1) {
return new Course(courID, SubjectOA.select(subID), TeacherOA.select(tchID), courTimeStart, courTimeEnd);
} else {
throw new InsertException();
}
}
}
}
public static boolean isExists(long courID) throws SQLException, NoConfigException {
String sql = "SELECT EXISTS(SELECT 1 FROM course WHERE courID=?)";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setLong(1, courID);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
return resultSet.getBoolean(1);
}
}
}
}
return false;
}
public static boolean delete(long courID) throws NoConfigException, SQLException {
String sql = "DELETE FROM course WHERE courID=?";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setLong(1, courID);
return preparedStatement.executeUpdate() == 1;
}
}
}
}

View File

@@ -20,7 +20,8 @@ public class MajorOA extends CommonOA {
try (Statement statement = connection.createStatement()) { try (Statement statement = connection.createStatement()) {
try (ResultSet resultSet = statement.executeQuery(sql)) { try (ResultSet resultSet = statement.executeQuery(sql)) {
while (resultSet.next()) { while (resultSet.next()) {
majors.add(new Major(resultSet.getInt("majorID"), resultSet.getString("majorName"), new Faculty(resultSet.getInt("facID"), resultSet.getString("facName")))); Faculty faculty = new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"));
majors.add(new Major(resultSet.getInt("majorID"), resultSet.getString("majorName"), faculty));
} }
} }
} }
@@ -35,7 +36,8 @@ public class MajorOA extends CommonOA {
preparedStatement.setInt(1, majorID); preparedStatement.setInt(1, majorID);
try (ResultSet resultSet = preparedStatement.executeQuery()) { try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) { if (resultSet.next()) {
return new Major(resultSet.getInt("majorID"), resultSet.getString("majorName"), new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"))); Faculty faculty = new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"));
return new Major(resultSet.getInt("majorID"), resultSet.getString("majorName"), faculty);
} }
} }
} }

View File

@@ -0,0 +1,97 @@
package com.cfive.classroom.library.database.operation;
import com.cfive.classroom.library.database.PoolHelper;
import com.cfive.classroom.library.database.bean.*;
import com.cfive.classroom.library.database.util.AlreadyExistsException;
import com.cfive.classroom.library.database.util.DependenciesNotFoundException;
import com.cfive.classroom.library.database.util.InsertException;
import com.cfive.classroom.library.database.util.NoConfigException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class StudentOA {
public static List<Student> selectAll() throws NoConfigException, SQLException {
String sql = "SELECT stuID,stuName,stuGender,passwd,salt,class.classID,grade,classNum,major.majorID,majorName,faculty.facID,facName FROM student,class,major,faculty where student.classID=class.classID AND class.majorID=major.majorID AND major.facID=faculty.facID ORDER BY stuID";
ArrayList<Student> students = new ArrayList<>();
try (Connection connection = PoolHelper.getConnection()) {
try (Statement statement = connection.createStatement()) {
try (ResultSet resultSet = statement.executeQuery(sql)) {
while (resultSet.next()) {
Faculty faculty = new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"));
Major major = new Major(resultSet.getInt("majorID"), resultSet.getString("majorName"), faculty);
AClass aClass = new AClass(resultSet.getLong("classID"), major, resultSet.getInt("grade"), resultSet.getInt("classNum"));
students.add(new Student(resultSet.getLong("stuID"), resultSet.getString("stuName"), resultSet.getString("stuGender").equals("m") ? Gender.m : Gender.f, aClass, resultSet.getString("passwd"), resultSet.getString("salt")));
}
}
}
}
return students;
}
public static Student select(long stuID) throws SQLException, NoConfigException {
String sql = "SELECT stuID,stuName,stuGender,passwd,salt,class.classID,grade,classNum,major.majorID,majorName,faculty.facID,facName FROM student,class,major,faculty where student.classID=class.classID AND class.majorID=major.majorID AND major.facID=faculty.facID AND stuID=?";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setLong(1, stuID);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
Faculty faculty = new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"));
Major major = new Major(resultSet.getInt("majorID"), resultSet.getString("majorName"), faculty);
AClass aClass = new AClass(resultSet.getLong("classID"), major, resultSet.getInt("grade"), resultSet.getInt("classNum"));
return new Student(resultSet.getLong("stuID"), resultSet.getString("stuName"), resultSet.getString("stuGender").equals("m") ? Gender.m : Gender.f, aClass, resultSet.getString("passwd"), resultSet.getString("salt"));
}
}
}
}
return null;
}
public static Student insert(long stuID, String stuName, Gender gender, long classID, String passwd, String salt) throws NoConfigException, SQLException, AlreadyExistsException, DependenciesNotFoundException, InsertException {
if (isExists(stuID)) throw new AlreadyExistsException();
if (!ClassOA.isExists(classID)) throw new DependenciesNotFoundException();
String sql = "INSERT INTO student VALUES (?,?,?,?,?,?)";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setLong(1, stuID);
preparedStatement.setString(2, stuName);
preparedStatement.setString(3, gender.name());
preparedStatement.setLong(4, classID);
preparedStatement.setString(5, passwd);
preparedStatement.setString(6, salt);
if (preparedStatement.executeUpdate() == 1) {
return new Student(classID, stuName, gender, ClassOA.select(classID), passwd, salt);
} else {
throw new InsertException();
}
}
}
}
public static boolean isExists(long stuID) throws SQLException, NoConfigException {
String sql = "SELECT EXISTS(SELECT 1 FROM student WHERE stuID=?)";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setLong(1, stuID);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
return resultSet.getBoolean(1);
}
}
}
}
return false;
}
public static boolean delete(long stuID) throws NoConfigException, SQLException {
String sql = "DELETE FROM student WHERE stuID=?";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setLong(1, stuID);
return preparedStatement.executeUpdate() == 1;
}
}
}
}

View File

@@ -0,0 +1,93 @@
package com.cfive.classroom.library.database.operation;
import com.cfive.classroom.library.database.PoolHelper;
import com.cfive.classroom.library.database.bean.*;
import com.cfive.classroom.library.database.util.AlreadyExistsException;
import com.cfive.classroom.library.database.util.DependenciesNotFoundException;
import com.cfive.classroom.library.database.util.InsertException;
import com.cfive.classroom.library.database.util.NoConfigException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class TeacherOA {
public static List<Teacher> selectAll() throws NoConfigException, SQLException {
String sql = "SELECT tchID,tchName,tchGender,passwd,salt,faculty.facID,facName FROM teacher,faculty where teacher.facID=faculty.facID ORDER BY tchID";
ArrayList<Teacher> teachers = new ArrayList<>();
try (Connection connection = PoolHelper.getConnection()) {
try (Statement statement = connection.createStatement()) {
try (ResultSet resultSet = statement.executeQuery(sql)) {
while (resultSet.next()) {
Faculty faculty = new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"));
teachers.add(new Teacher(resultSet.getLong("tchID"), resultSet.getString("tchName"), resultSet.getString("tchGender").equals("m")?Gender.m:Gender.f, faculty, resultSet.getString("passwd"), resultSet.getString("salt")));
}
}
}
}
return teachers;
}
public static Teacher select(long tchID) throws SQLException, NoConfigException {
String sql = "SELECT tchID,tchName,tchGender,passwd,salt,faculty.facID,facName FROM teacher,faculty where teacher.facID=faculty.facID AND tchID=?";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setLong(1, tchID);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
Faculty faculty = new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"));
return new Teacher(resultSet.getLong("tchID"), resultSet.getString("tchName"), resultSet.getString("tchGender").equals("m")?Gender.m:Gender.f, faculty, resultSet.getString("passwd"), resultSet.getString("salt"));
}
}
}
}
return null;
}
public static Teacher insert(long tchID, String tchName, Gender gender, int facID, String passwd, String salt) throws NoConfigException, SQLException, AlreadyExistsException, DependenciesNotFoundException, InsertException {
if (isExists(tchID)) throw new AlreadyExistsException();
if (!FacultyOA.isExists(facID)) throw new DependenciesNotFoundException();
String sql = "INSERT INTO teacher VALUES (?,?,?,?,?,?)";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setLong(1, tchID);
preparedStatement.setString(2, tchName);
preparedStatement.setString(3, gender.name());
preparedStatement.setInt(4, facID);
preparedStatement.setString(5, passwd);
preparedStatement.setString(6, salt);
if (preparedStatement.executeUpdate() == 1) {
return new Teacher(tchID, tchName, gender, FacultyOA.select(facID), passwd, salt);
} else {
throw new InsertException();
}
}
}
}
public static boolean isExists(long tchID) throws SQLException, NoConfigException {
String sql = "SELECT EXISTS(SELECT 1 FROM teacher WHERE tchID=?)";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setLong(1, tchID);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
return resultSet.getBoolean(1);
}
}
}
}
return false;
}
public static boolean delete(long tchID) throws NoConfigException, SQLException {
String sql = "DELETE FROM teacher WHERE tchID=?";
try (Connection connection = PoolHelper.getConnection()) {
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setLong(1, tchID);
return preparedStatement.executeUpdate() == 1;
}
}
}
}

View File

@@ -10,6 +10,7 @@ import org.junit.jupiter.api.Test;
import java.sql.*; import java.sql.*;
import java.util.List; import java.util.List;
import java.util.UUID;
public class DatabaseTest { public class DatabaseTest {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
@@ -67,4 +68,9 @@ public class DatabaseTest {
LOGGER.debug(DatabaseHelper.selectFromFaculty("计算机学院")); LOGGER.debug(DatabaseHelper.selectFromFaculty("计算机学院"));
LOGGER.debug(DatabaseHelper.selectFromFaculty(6)); LOGGER.debug(DatabaseHelper.selectFromFaculty(6));
} }
@Test
void TempTest() {
LOGGER.debug(String.valueOf(UUID.randomUUID()));
}
} }

View File

@@ -127,7 +127,7 @@ create table Attendance
attID char(36) not null comment '考勤编号', attID char(36) not null comment '考勤编号',
courID bigint not null comment '隶属课程', courID bigint not null comment '隶属课程',
stuID int not null comment '隶属学生', stuID int not null comment '隶属学生',
attTie timestamp null comment '考勤时间', attTime timestamp null comment '考勤时间',
attStatus enum ('not_signed', 'signed', 'absence', 'personal_leave', 'sick_leave', 'public_holiday', 'late', 'leave_early') default 'not_signed' not null comment '考勤状态', attStatus enum ('not_signed', 'signed', 'absence', 'personal_leave', 'sick_leave', 'public_holiday', 'late', 'leave_early') default 'not_signed' not null comment '考勤状态',
constraint Attendance_pk constraint Attendance_pk
primary key (attID) primary key (attID)
@@ -143,12 +143,52 @@ from faculty;
ALTER TABLE faculty ALTER TABLE faculty
AUTO_INCREMENT = 19; AUTO_INCREMENT = 19;
delete
from faculty;
INSERT INTO faculty INSERT INTO faculty
values (2, '计算机学院'), values (2, '计算机学院'),
(18, '工程训练中心'); (18, '工程训练中心');
SELECT EXISTS(SELECT 1 FROM faculty WHERE facName = '信息学院') SELECT EXISTS(SELECT 1 FROM faculty WHERE facName = '信息学院');
SELECT attID,
attTime,
attStatus,
student.stuID,
stuName,
stuGender,
student.passwd,
student.salt,
class.classID,
grade,
classNum,
major.majorID,
majorName,
course.courID,
courTimeFrom,
courTimeEnd,
subject.subID,
teacher.tchID,
tchName,
tchGender,
teacher.passwd,
teacher.salt,
faculty.facID,
facName
FROM attendance,
student,
class,
major,
course,
subject,
teacher,
faculty
where attendance.courID = course.courID
AND attendance.stuID = student.stuID
AND class.classID = student.classID
AND class.majorID = major.majorID
AND major.facID = faculty.facID
AND course.subID = subject.subID
AND course.tchID = teacher.tchID
AND teacher.facID = faculty.facID
ORDER BY courID, attTime, attStatus;