diff --git a/Library/src/main/java/com/cfive/classroom/library/database/DatabaseHelper.java b/Library/src/main/java/com/cfive/classroom/library/database/DatabaseHelper.java new file mode 100644 index 0000000..d7bcd3d --- /dev/null +++ b/Library/src/main/java/com/cfive/classroom/library/database/DatabaseHelper.java @@ -0,0 +1,185 @@ +package com.cfive.classroom.library.database; + +import com.cfive.classroom.library.database.bean.AClass; +import com.cfive.classroom.library.database.bean.Faculty; +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.DependenciesNotFoundException; +import com.cfive.classroom.library.database.util.InsertException; +import com.cfive.classroom.library.database.util.NoConfigException; + +import java.sql.SQLException; +import java.util.List; + +public class DatabaseHelper { + public static List selectAllFromFaculty() throws NoConfigException, SQLException { + return FacultyOA.selectAll(); + } + + public static Faculty selectFromFaculty(int facID) throws NoConfigException, SQLException { + return FacultyOA.select(facID); + } + + public static Faculty selectFromFaculty(String facName) throws NoConfigException, SQLException { + return FacultyOA.select(facName); + } + + public static Faculty insertIntoFaculty(String facName) throws NoConfigException, SQLException, InsertException, AlreadyExistsException { + return FacultyOA.insert(facName); + } + + public static Faculty insertIntoFaculty(int facID, String facName) throws NoConfigException, InsertException, SQLException, AlreadyExistsException { + return FacultyOA.insert(facID, facName); + } + + public static boolean isExistsInFaculty(int facID) throws NoConfigException, SQLException { + return FacultyOA.isExists(facID); + } + + public static boolean isExistsInFaculty(String facName) throws NoConfigException, SQLException { + return FacultyOA.isExists(facName); + } + + public static boolean deleteFromFaculty(int facID) throws NoConfigException, SQLException { + return FacultyOA.delete(facID); + } + + public static boolean deleteFromFaculty(String facName) throws NoConfigException, SQLException { + return FacultyOA.delete(facName); + } + + public static List selectAllFromSubject() throws NoConfigException, SQLException { + return SubjectOA.selectAll(); + } + + public static Subject selectFromSubject(int subID) throws NoConfigException, SQLException { + return SubjectOA.select(subID); + } + + public static Subject selectFromSubject(String subName) throws NoConfigException, SQLException { + return SubjectOA.select(subName); + } + + public static Subject insertIntoSubject(String subName) throws NoConfigException, SQLException, InsertException, AlreadyExistsException { + return SubjectOA.insert(subName); + } + + public static Subject insertIntoSubject(int subID, String subName) throws NoConfigException, SQLException, InsertException, AlreadyExistsException { + return SubjectOA.insert(subID, subName); + } + + public static boolean isExistsInSubject(int subID) throws NoConfigException, SQLException { + return SubjectOA.isExists(subID); + } + + public static boolean isExistsInSubject(String subName) throws NoConfigException, SQLException { + return SubjectOA.isExists(subName); + } + + public static boolean deleteFromSubject(int subID) throws NoConfigException, SQLException { + return SubjectOA.delete(subID); + } + + public static boolean deleteFromSubject(String subName) throws NoConfigException, SQLException { + return SubjectOA.delete(subName); + } + + public static List selectAllFromMajor() throws NoConfigException, SQLException { + return MajorOA.selectAll(); + } + + public static Major selectFromMajor(int majorID) throws NoConfigException, SQLException { + return MajorOA.select(majorID); + } + + public static Major selectFromMajor(String majorName) throws NoConfigException, SQLException { + return MajorOA.select(majorName); + } + + public static Major insertIntoMajor(String majorName, int facID) throws NoConfigException, SQLException, InsertException, AlreadyExistsException, DependenciesNotFoundException { + return MajorOA.insert(majorName, facID); + } + + public static Major insertIntoMajor(String majorName, String facName) throws NoConfigException, AlreadyExistsException, SQLException, InsertException, DependenciesNotFoundException { + return MajorOA.insert(majorName, facName); + } + + public static Major insertIntoMajor(int majorID, String majorName, int facID) throws NoConfigException, SQLException, InsertException, AlreadyExistsException, DependenciesNotFoundException { + return MajorOA.insert(majorID, majorName, facID); + } + + public static Major insertIntoMajor(int majorID, String majorName, String facName) throws NoConfigException, AlreadyExistsException, SQLException, InsertException, DependenciesNotFoundException { + return MajorOA.insert(majorID, majorName, facName); + } + + public static boolean isExistsInMajor(int majorID) throws NoConfigException, SQLException { + return MajorOA.isExists(majorID); + } + + public static boolean isExistsInMajor(String majorName) throws NoConfigException, SQLException { + return MajorOA.isExists(majorName); + } + + public static boolean deleteFromMajor(int majorID) throws NoConfigException, SQLException { + return MajorOA.delete(majorID); + } + + public static boolean deleteFromMajor(String majorName) throws NoConfigException, SQLException { + return MajorOA.delete(majorName); + } + + public static List selectAllFromClass() throws NoConfigException, SQLException { + return ClassOA.selectAll(); + } + + public static AClass selectFromClass(int classID) throws NoConfigException, SQLException { + return ClassOA.select(classID); + } + + public static AClass selectFromClass(int majorID, int grade, int classNum) throws NoConfigException, SQLException { + return ClassOA.select(majorID, grade, classNum); + } + + public static AClass insertIntoClass(int majorID, int grade, int classNum) throws NoConfigException, SQLException, InsertException, AlreadyExistsException, DependenciesNotFoundException { + return ClassOA.insert(majorID, grade, classNum); + } + + public static AClass insertIntoClass(String majorName, int grade, int classNum) throws NoConfigException, SQLException, InsertException, AlreadyExistsException, DependenciesNotFoundException { + return ClassOA.insert(majorName, grade, classNum); + } + + public static AClass insertIntoClass(long classID, int majorID, int grade, int classNum) throws NoConfigException, AlreadyExistsException, SQLException, InsertException, DependenciesNotFoundException { + return ClassOA.insert(classID, majorID, grade, classNum); + } + + public static AClass insertIntoClass(long classID, String majorName, int grade, int classNum) throws NoConfigException, AlreadyExistsException, SQLException, InsertException, DependenciesNotFoundException { + return ClassOA.insert(classID, majorName, grade, classNum); + } + + public static boolean isExistsInClass(int classID) throws NoConfigException, SQLException { + return ClassOA.isExists(classID); + } + + public static boolean isExistsInClass(int majorID, int grade, int classNum) throws NoConfigException, SQLException { + return ClassOA.isExists(majorID, grade, classNum); + } + + public static boolean deleteFromClass(int classID) throws NoConfigException, SQLException { + return ClassOA.delete(classID); + } + + public static boolean deleteFromClass(int majorID, int grade, int classNum) throws NoConfigException, SQLException { + return ClassOA.delete(majorID, grade, classNum); + } + + + + public static void close() { + PoolHelper.close(); + } +} diff --git a/Library/src/main/java/com/cfive/classroom/library/database/operation/ClassOA.java b/Library/src/main/java/com/cfive/classroom/library/database/operation/ClassOA.java new file mode 100644 index 0000000..b43b3f6 --- /dev/null +++ b/Library/src/main/java/com/cfive/classroom/library/database/operation/ClassOA.java @@ -0,0 +1,205 @@ +package com.cfive.classroom.library.database.operation; + +import com.cfive.classroom.library.database.PoolHelper; +import com.cfive.classroom.library.database.bean.Faculty; +import com.cfive.classroom.library.database.bean.AClass; +import com.cfive.classroom.library.database.bean.Major; +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; +import java.util.Objects; + +public class ClassOA { + public static List selectAll() throws NoConfigException, SQLException { + String sql = "SELECT classID,major.majorID,grade,classNum, majorname, faculty.facid, facname FROM class,major,faculty where class.majorID=major.majorID AND major.facID=faculty.facID ORDER BY classID"; + ArrayList AClasses = new ArrayList<>(); + try (Connection connection = PoolHelper.getConnection()) { + try (Statement statement = connection.createStatement()) { + try (ResultSet resultSet = statement.executeQuery(sql)) { + 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"))); + } + } + } + } + return AClasses; + } + + public static AClass select(long classID) throws SQLException, NoConfigException { + String sql = "SELECT classID,major.majorID,grade,classNum, majorname, faculty.facid, facname FROM class,major,faculty where class.majorID=major.majorID AND major.facID=faculty.facID AND classID=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setLong(1, classID); + AClass resultSet = getAClass(preparedStatement); + if (resultSet != null) return resultSet; + } + } + return null; + } + + public static AClass select(int majorID, int grade, int classNum) throws SQLException, NoConfigException { + String sql = "SELECT classID,major.majorID,grade,classNum, majorname, faculty.facid, facname FROM class,major,faculty where class.majorID=major.majorID AND major.facID=faculty.facID AND class.majorID=? AND grade=? AND classNum=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, majorID); + preparedStatement.setInt(2, grade); + preparedStatement.setInt(3, classNum); + AClass resultSet = getAClass(preparedStatement); + if (resultSet != null) return resultSet; + } + } + return null; + } + + private static AClass getAClass(PreparedStatement preparedStatement) throws SQLException { + try (ResultSet resultSet = preparedStatement.executeQuery()) { + 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")); + } + } + return null; + } + + public static AClass insert(int majorID, int grade, int classNum) throws NoConfigException, SQLException, AlreadyExistsException, DependenciesNotFoundException, InsertException { + if (isExists(majorID, grade, classNum)) throw new AlreadyExistsException(); + if (!MajorOA.isExists(majorID)) throw new DependenciesNotFoundException(); + + String sql = "INSERT INTO class(majorID,grade,classNum) VALUES (?,?,?)"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + preparedStatement.setInt(1, majorID); + preparedStatement.setInt(2, grade); + preparedStatement.setInt(3, classNum); + preparedStatement.executeUpdate(); + try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) { + if (generatedKeys.next()) { + return new AClass(generatedKeys.getLong(1), MajorOA.select(majorID), grade, classNum); + } else { + throw new InsertException(); + } + } + } + } + } + + public static AClass insert(String majorName, int grade, int classNum) throws NoConfigException, SQLException, AlreadyExistsException, DependenciesNotFoundException, InsertException { + if (!MajorOA.isExists(majorName)) throw new DependenciesNotFoundException(); + if (isExists(Objects.requireNonNull(MajorOA.select(majorName)).getMajorID(), grade, classNum)) throw new AlreadyExistsException(); + + String sql = "INSERT INTO class(majorID,grade,classNum) VALUES (?,?,?)"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + preparedStatement.setInt(1, Objects.requireNonNull(MajorOA.select(majorName)).getMajorID()); + preparedStatement.setInt(2, grade); + preparedStatement.setInt(3, classNum); + preparedStatement.executeUpdate(); + try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) { + if (generatedKeys.next()) { + return new AClass(generatedKeys.getLong(1), MajorOA.select(majorName), grade, classNum); + } else { + throw new InsertException(); + } + } + } + } + } + + public static AClass insert(long classID, int majorID, int grade, int classNum) throws NoConfigException, SQLException, AlreadyExistsException, DependenciesNotFoundException, InsertException { + if (isExists(classID) || isExists(majorID, grade, classNum)) throw new AlreadyExistsException(); + if (!MajorOA.isExists(majorID)) throw new DependenciesNotFoundException(); + + String sql = "INSERT INTO class VALUES (?,?,?,?)"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setLong(1, classID); + preparedStatement.setInt(2, majorID); + preparedStatement.setInt(3, grade); + preparedStatement.setInt(4, classNum); + if (preparedStatement.executeUpdate() == 1) { + return new AClass(classID, MajorOA.select(majorID), grade, classNum); + } else { + throw new InsertException(); + } + } + } + } + + public static AClass insert(long classID, String majorName, int grade, int classNum) throws NoConfigException, SQLException, AlreadyExistsException, DependenciesNotFoundException, InsertException { + if (!MajorOA.isExists(majorName)) throw new DependenciesNotFoundException(); + if (isExists(classID) || isExists(Objects.requireNonNull(MajorOA.select(majorName)).getMajorID(), grade, classNum)) throw new AlreadyExistsException(); + + String sql = "INSERT INTO class VALUES (?,?,?,?)"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setLong(1, classID); + preparedStatement.setInt(2, Objects.requireNonNull(MajorOA.select(majorName)).getMajorID()); + preparedStatement.setInt(3, grade); + preparedStatement.setInt(4, classNum); + if (preparedStatement.executeUpdate() == 1) { + return new AClass(classID, MajorOA.select(majorName), grade, classNum); + } else { + throw new InsertException(); + } + } + } + } + + public static boolean isExists(long classID) throws SQLException, NoConfigException { + String sql = "SELECT EXISTS(SELECT 1 FROM class WHERE classID=?)"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setLong(1, classID); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + return resultSet.getBoolean(1); + } + } + } + } + return false; + } + + public static boolean isExists(int majorID, int grade, int classNum) throws NoConfigException, SQLException { + String sql = "SELECT EXISTS(SELECT 1 FROM class WHERE majorID=? AND grade=? AND classNUM=?)"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, majorID); + preparedStatement.setInt(2, grade); + preparedStatement.setInt(3, classNum); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + return resultSet.getBoolean(1); + } + } + } + } + return false; + } + + public static boolean delete(long classID) throws NoConfigException, SQLException { + String sql = "DELETE FROM class WHERE classID=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setLong(1, classID); + return preparedStatement.executeUpdate() == 1; + } + } + } + + public static boolean delete(int majorID, int grade, int classNum) throws NoConfigException, SQLException { + String sql = "DELETE FROM class WHERE majorID=? AND grade=? AND classNum=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, majorID); + preparedStatement.setInt(2, grade); + preparedStatement.setInt(3, classNum); + return preparedStatement.executeUpdate() == 1; + } + } + } +} diff --git a/Library/src/main/java/com/cfive/classroom/library/database/operation/CommonOA.java b/Library/src/main/java/com/cfive/classroom/library/database/operation/CommonOA.java new file mode 100644 index 0000000..ee81c47 --- /dev/null +++ b/Library/src/main/java/com/cfive/classroom/library/database/operation/CommonOA.java @@ -0,0 +1,39 @@ +package com.cfive.classroom.library.database.operation; + +import com.cfive.classroom.library.database.PoolHelper; +import com.cfive.classroom.library.database.util.NoConfigException; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public abstract class CommonOA { + public static boolean isExists(String sql, int id) throws NoConfigException, SQLException { + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, id); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + return resultSet.getBoolean(1); + } + } + } + } + return false; + } + + public static boolean isExists(String sql, String name) throws NoConfigException, SQLException { + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setString(1, name); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + return resultSet.getBoolean(1); + } + } + } + } + return false; + } +} diff --git a/Library/src/main/java/com/cfive/classroom/library/database/operation/FacultyOA.java b/Library/src/main/java/com/cfive/classroom/library/database/operation/FacultyOA.java new file mode 100644 index 0000000..ed46fb6 --- /dev/null +++ b/Library/src/main/java/com/cfive/classroom/library/database/operation/FacultyOA.java @@ -0,0 +1,121 @@ +package com.cfive.classroom.library.database.operation; + +import com.cfive.classroom.library.database.PoolHelper; +import com.cfive.classroom.library.database.bean.Faculty; +import com.cfive.classroom.library.database.util.AlreadyExistsException; +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 FacultyOA extends CommonOA { + public static List selectAll() throws NoConfigException, SQLException { + String sql = "SELECT * FROM faculty ORDER BY facID"; + ArrayList faculties = new ArrayList<>(); + try (Connection connection = PoolHelper.getConnection()) { + try (Statement statement = connection.createStatement()) { + try (ResultSet resultSet = statement.executeQuery(sql)) { + while (resultSet.next()) { + faculties.add(new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"))); + } + } + } + } + return faculties; + } + + public static Faculty select(int facID) throws SQLException, NoConfigException { + String sql = "SELECT * FROM faculty WHERE facID=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, facID); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + return new Faculty(resultSet.getInt("facID"), resultSet.getString("facName")); + } + } + } + } + return null; + } + + public static Faculty select(String facName) throws SQLException, NoConfigException { + String sql = "SELECT * FROM faculty WHERE facName=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setString(1, facName); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + return new Faculty(resultSet.getInt("facID"), resultSet.getString("facName")); + } + } + } + } + return null; + } + + public static Faculty insert(String facName) throws NoConfigException, InsertException, SQLException, AlreadyExistsException { + if (isExists(facName)) throw new AlreadyExistsException(); + String sql = "INSERT INTO faculty(facName) VALUES (?)"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + preparedStatement.setString(1, facName); + preparedStatement.executeUpdate(); + try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) { + if (generatedKeys.next()) { + return new Faculty(generatedKeys.getInt(1), facName); + } else { + throw new InsertException(); + } + } + } + } + } + + public static Faculty insert(int facID, String facName) throws NoConfigException, InsertException, SQLException, AlreadyExistsException { + if (isExists(facName)) throw new AlreadyExistsException(); + String sql = "INSERT INTO faculty VALUES (?,?)"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, facID); + preparedStatement.setString(2, facName); + if (preparedStatement.executeUpdate()==1) { + return new Faculty(facID, facName); + } + throw new InsertException(); + } + } + } + + public static boolean isExists(int facID) throws NoConfigException, SQLException { + String sql = "SELECT EXISTS(SELECT 1 FROM faculty WHERE facID=?)"; + return isExists(sql, facID); + } + + public static boolean isExists(String facName) throws NoConfigException, SQLException { + String sql = "SELECT EXISTS(SELECT 1 FROM faculty WHERE facName=?)"; + return isExists(sql, facName); + } + + public static boolean delete(int facID) throws NoConfigException, SQLException { + String sql = "DELETE FROM faculty WHERE facID=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, facID); + return preparedStatement.executeUpdate() == 1; + } + } + } + + public static boolean delete(String facName) throws NoConfigException, SQLException { + String sql = "DELETE FROM faculty WHERE facName=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setString(1, facName); + return preparedStatement.executeUpdate() == 1; + } + } + } +} diff --git a/Library/src/main/java/com/cfive/classroom/library/database/operation/MajorOA.java b/Library/src/main/java/com/cfive/classroom/library/database/operation/MajorOA.java new file mode 100644 index 0000000..c0575de --- /dev/null +++ b/Library/src/main/java/com/cfive/classroom/library/database/operation/MajorOA.java @@ -0,0 +1,146 @@ +package com.cfive.classroom.library.database.operation; + +import com.cfive.classroom.library.database.PoolHelper; +import com.cfive.classroom.library.database.bean.Faculty; +import com.cfive.classroom.library.database.bean.Major; +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 MajorOA extends CommonOA { + public static List selectAll() throws NoConfigException, SQLException { + String sql = "SELECT majorID,majorName,faculty.facID,facName FROM major,faculty where major.facID=faculty.facID ORDER BY majorID"; + ArrayList majors = new ArrayList<>(); + try (Connection connection = PoolHelper.getConnection()) { + try (Statement statement = connection.createStatement()) { + try (ResultSet resultSet = statement.executeQuery(sql)) { + while (resultSet.next()) { + majors.add(new Major(resultSet.getInt("majorID"), resultSet.getString("majorName"), new Faculty(resultSet.getInt("facID"), resultSet.getString("facName")))); + } + } + } + } + return majors; + } + + public static Major select(int majorID) throws SQLException, NoConfigException { + String sql = "SELECT majorID,majorName,faculty.facID,facName FROM major,faculty WHERE major.facID=faculty.facID AND majorID=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, majorID); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + return new Major(resultSet.getInt("majorID"), resultSet.getString("majorName"), new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"))); + } + } + } + } + return null; + } + + public static Major select(String majorName) throws SQLException, NoConfigException { + String sql = "SELECT majorID,majorName,faculty.facID,facName FROM major,faculty WHERE major.facID=faculty.facID AND majorName=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setString(1, majorName); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + return new Major(resultSet.getInt("majorID"), resultSet.getString("majorName"), new Faculty(resultSet.getInt("facID"), resultSet.getString("facName"))); + } + } + } + } + return null; + } + + public static Major insert(String majorName, int facID) throws NoConfigException, InsertException, SQLException, AlreadyExistsException, DependenciesNotFoundException { + return insert(majorName, FacultyOA.isExists(facID), FacultyOA.select(facID)); + } + + public static Major insert(String majorName, String facName) throws InsertException, SQLException, NoConfigException, AlreadyExistsException, DependenciesNotFoundException { + return insert(majorName, FacultyOA.isExists(facName), FacultyOA.select(facName)); + } + + private static Major insert(String majorName, boolean isExistsFaculty, Faculty faculty) throws NoConfigException, SQLException, AlreadyExistsException, DependenciesNotFoundException, InsertException { + if (isExists(majorName)) throw new AlreadyExistsException(); + if (!isExistsFaculty) throw new DependenciesNotFoundException(); + String sql = "INSERT INTO major(majorName, facID) VALUES (?,?)"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + preparedStatement.setString(1, majorName); + assert faculty != null; + preparedStatement.setInt(2, faculty.getFacID()); + preparedStatement.executeUpdate(); + try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) { + if (generatedKeys.next()) { + return new Major(generatedKeys.getInt(1), majorName, faculty); + } else { + throw new InsertException(); + } + } + } + } + } + + public static Major insert(int majorID, String majorName, int facID) throws NoConfigException, InsertException, SQLException, AlreadyExistsException, DependenciesNotFoundException { + return insert(majorID, majorName, FacultyOA.isExists(facID), FacultyOA.select(facID)); + } + + public static Major insert(int majorID, String majorName, String facName) throws InsertException, SQLException, NoConfigException, AlreadyExistsException, DependenciesNotFoundException { + return insert(majorID, majorName, FacultyOA.isExists(facName), FacultyOA.select(facName)); + } + + private static Major insert(int majorID, String majorName, boolean isExistsFaculty, Faculty faculty) throws NoConfigException, SQLException, AlreadyExistsException, DependenciesNotFoundException, InsertException { + if (isExists(majorName)) throw new AlreadyExistsException(); + if (!isExistsFaculty) throw new DependenciesNotFoundException(); + String sql = "INSERT INTO major VALUES (?,?,?)"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + preparedStatement.setInt(1, majorID); + preparedStatement.setString(2, majorName); + assert faculty != null; + preparedStatement.setInt(3, faculty.getFacID()); + if (preparedStatement.executeUpdate() == 1) { + return new Major(majorID, majorName, faculty); + } else { + throw new InsertException(); + } + } + } + } + + public static boolean isExists(int majorID) throws NoConfigException, SQLException { + String sql = "SELECT EXISTS(SELECT 1 FROM major WHERE majorID=?)"; + return isExists(sql, majorID); + } + + public static boolean isExists(String majorName) throws NoConfigException, SQLException { + String sql = "SELECT EXISTS(SELECT 1 FROM major WHERE majorName=?)"; + return isExists(sql, majorName); + } + + public static boolean delete(int majorID) throws NoConfigException, SQLException { + String sql = "DELETE FROM major WHERE majorID=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, majorID); + return preparedStatement.executeUpdate() == 1; + } + } + } + + public static boolean delete(String majorName) throws NoConfigException, SQLException { + String sql = "DELETE FROM major WHERE majorID=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setString(1, majorName); + return preparedStatement.executeUpdate() == 1; + } + } + } +} diff --git a/Library/src/main/java/com/cfive/classroom/library/database/operation/SubjectOA.java b/Library/src/main/java/com/cfive/classroom/library/database/operation/SubjectOA.java new file mode 100644 index 0000000..0830c0e --- /dev/null +++ b/Library/src/main/java/com/cfive/classroom/library/database/operation/SubjectOA.java @@ -0,0 +1,121 @@ +package com.cfive.classroom.library.database.operation; + +import com.cfive.classroom.library.database.PoolHelper; +import com.cfive.classroom.library.database.bean.Subject; +import com.cfive.classroom.library.database.util.AlreadyExistsException; +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 SubjectOA extends CommonOA { + public static List selectAll() throws NoConfigException, SQLException { + String sql = "SELECT * FROM subject ORDER BY subID"; + ArrayList subjects = new ArrayList<>(); + try (Connection connection = PoolHelper.getConnection()) { + try (Statement statement = connection.createStatement()) { + try (ResultSet resultSet = statement.executeQuery(sql)) { + while (resultSet.next()) { + subjects.add(new Subject(resultSet.getInt("subID"), resultSet.getString("subName"))); + } + } + } + } + return subjects; + } + + public static Subject select(int subID) throws SQLException, NoConfigException { + String sql = "SELECT * FROM subject WHERE subID=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, subID); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + return new Subject(resultSet.getInt("subID"), resultSet.getString("subName")); + } + } + } + } + return null; + } + + public static Subject select(String subName) throws SQLException, NoConfigException { + String sql = "SELECT * FROM subject WHERE subName=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setString(1, subName); + try (ResultSet resultSet = preparedStatement.executeQuery()) { + if (resultSet.next()) { + return new Subject(resultSet.getInt("subID"), resultSet.getString("subName")); + } + } + } + } + return null; + } + + public static Subject insert(String subName) throws NoConfigException, InsertException, SQLException, AlreadyExistsException { + if (isExists(subName)) throw new AlreadyExistsException(); + String sql = "INSERT INTO subject(subName) VALUES (?)"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + preparedStatement.setString(1, subName); + preparedStatement.executeUpdate(); + try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) { + if (generatedKeys.next()) { + return new Subject(generatedKeys.getInt(1), subName); + } else { + throw new InsertException(); + } + } + } + } + } + + public static Subject insert(int subID, String subName) throws NoConfigException, InsertException, SQLException, AlreadyExistsException { + if (isExists(subName)) throw new AlreadyExistsException(); + String sql = "INSERT INTO subject VALUES (?,?)"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, subID); + preparedStatement.setString(2, subName); + if (preparedStatement.executeUpdate()==1) { + return new Subject(subID, subName); + } + throw new InsertException(); + } + } + } + + public static boolean isExists(int subID) throws NoConfigException, SQLException { + String sql = "SELECT EXISTS(SELECT 1 FROM subject WHERE subID=?)"; + return isExists(sql, subID); + } + + public static boolean isExists(String subName) throws NoConfigException, SQLException { + String sql = "SELECT EXISTS(SELECT 1 FROM subject WHERE subName=?)"; + return isExists(sql, subName); + } + + public static boolean delete(int subID) throws NoConfigException, SQLException { + String sql = "DELETE FROM subject WHERE subID=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setInt(1, subID); + return preparedStatement.executeUpdate() == 1; + } + } + } + + public static boolean delete(String subName) throws NoConfigException, SQLException { + String sql = "DELETE FROM subject WHERE subName=?"; + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setString(1, subName); + return preparedStatement.executeUpdate() == 1; + } + } + } +} diff --git a/Library/src/test/java/com/cfive/classroom/library/database/DatabaseTest.java b/Library/src/test/java/com/cfive/classroom/library/database/DatabaseTest.java new file mode 100644 index 0000000..fe1d917 --- /dev/null +++ b/Library/src/test/java/com/cfive/classroom/library/database/DatabaseTest.java @@ -0,0 +1,70 @@ +package com.cfive.classroom.library.database; + +import com.cfive.classroom.library.database.bean.Faculty; +import com.cfive.classroom.library.database.util.AlreadyExistsException; +import com.cfive.classroom.library.database.util.InsertException; +import com.cfive.classroom.library.database.util.NoConfigException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.Test; + +import java.sql.*; +import java.util.List; + +public class DatabaseTest { + private static final Logger LOGGER = LogManager.getLogger(); + + @Test + void poolTest() throws NoConfigException { + String sql = "INSERT INTO Faculty(facName) VALUES (?)"; + + try (Connection connection = PoolHelper.getConnection()) { + try (PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + preparedStatement.setString(1, "1计算机"); + preparedStatement.executeUpdate(); + try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) { + if (generatedKeys.next()) { + LOGGER.info("成功插入 facID=" + generatedKeys.getInt(1) + " facName=计算机"); + } + } + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + + PoolHelper.close(); + } + + @Test + void selectAllFacultyTest() throws NoConfigException, SQLException { + List faculties = DatabaseHelper.selectAllFromFaculty(); + faculties.forEach(LOGGER::debug); + + DatabaseHelper.close(); + } + + @Test + void insertIntoFacultyTest() throws NoConfigException, SQLException, InsertException, AlreadyExistsException { + Faculty faculty = DatabaseHelper.insertIntoFaculty("荣誉学院"); + LOGGER.debug(faculty.toString()); + DatabaseHelper.close(); + } + + @Test + void existsFacultyTest() throws NoConfigException, SQLException { + LOGGER.debug(DatabaseHelper.isExistsInFaculty("信息学院")); + LOGGER.debug(DatabaseHelper.deleteFromFaculty(18)); + } + + @Test + void deleteFacultyTest() throws NoConfigException, SQLException { + LOGGER.debug(DatabaseHelper.deleteFromFaculty("计算机学院")); + LOGGER.debug(DatabaseHelper.deleteFromFaculty(18)); + } + + @Test + void selectFromFacultyTest() throws NoConfigException, SQLException { + LOGGER.debug(DatabaseHelper.selectFromFaculty("计算机学院")); + LOGGER.debug(DatabaseHelper.selectFromFaculty(6)); + } +}