| 1 | package service.impl;
|
|---|
| 2 |
|
|---|
| 3 | import DAO.impl.ProfessorDAOImpl;
|
|---|
| 4 | import model.Professor;
|
|---|
| 5 | import service.ProfessorService;
|
|---|
| 6 |
|
|---|
| 7 | import java.sql.Connection;
|
|---|
| 8 | import java.sql.DriverManager;
|
|---|
| 9 | import java.sql.SQLException;
|
|---|
| 10 | import java.util.List;
|
|---|
| 11 |
|
|---|
| 12 | public class ProfessorServiceImpl implements ProfessorService {
|
|---|
| 13 |
|
|---|
| 14 | private final ProfessorDAOImpl professorDAO;
|
|---|
| 15 |
|
|---|
| 16 | public ProfessorServiceImpl() {
|
|---|
| 17 | this.professorDAO = new ProfessorDAOImpl();
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | public class DatabaseConnection {
|
|---|
| 21 | public Connection getConnection() throws SQLException {
|
|---|
| 22 | return DriverManager.getConnection("jdbc:mysql://localhost:3306/Education?useSSL=false&allowPublicKeyRetrieval=true", "root", "12345Em!");
|
|---|
| 23 | }
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | @Override
|
|---|
| 27 | public List<Professor> getAllProfessors() throws SQLException {
|
|---|
| 28 | List<Professor> professor = professorDAO.getall();
|
|---|
| 29 | return professor;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | @Override
|
|---|
| 33 | public Professor getById(Long id) throws SQLException {
|
|---|
| 34 | try {
|
|---|
| 35 | return professorDAO.getByID(id);
|
|---|
| 36 | } catch (SQLException e) {
|
|---|
| 37 | System.out.println("Error fetching professor with ID " + id + ": " + e.getMessage());
|
|---|
| 38 | throw e;
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | @Override
|
|---|
| 43 | public void update(Professor professor) {
|
|---|
| 44 | try {
|
|---|
| 45 | professorDAO.update(professor);
|
|---|
| 46 | System.out.println("Successfully updated professor: " + professor);
|
|---|
| 47 | } catch (SQLException e) {
|
|---|
| 48 | System.out.println("Error updating professor: " + professor + ". " + e.getMessage());
|
|---|
| 49 | throw new RuntimeException("Failed to update professor", e);
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | @Override
|
|---|
| 54 | public void delete(Long id) {
|
|---|
| 55 | professorDAO.delete(id);
|
|---|
| 56 | System.out.println("Successfully deleted professor with ID: " + id);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | @Override
|
|---|
| 60 | public void save(Professor professor) {
|
|---|
| 61 | try {
|
|---|
| 62 | professorDAO.save(professor);
|
|---|
| 63 | System.out.println("Successfully saved professor: " + professor);
|
|---|
| 64 | } catch (SQLException e) {
|
|---|
| 65 | System.out.println("Error saving professor: " + professor + ". " + e.getMessage());
|
|---|
| 66 | throw new RuntimeException("Failed to save professor", e);
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|