source: src/main/java/DAO/impl/StudentDAOImpl.java@ 1eb7a55

Last change on this file since 1eb7a55 was 1eb7a55, checked in by Elena Markovska <elena.elenamarkovska@…>, 11 days ago

Initial commit - Scholaris project code

  • Property mode set to 100644
File size: 5.3 KB
Line 
1package DAO.impl;
2
3import DAO.JDBCUtils;
4import DAO.StudentDAO;
5import model.Student;
6import model.Subject;
7
8import java.sql.*;
9import java.util.ArrayList;
10import java.util.List;
11
12public class StudentDAOImpl implements StudentDAO {
13
14 @Override
15 public List<Student> getAll() throws SQLException {
16 final String SELECT_ALL_STUDENT = "SELECT id, name, surname, location, studentindex, facultyid FROM student";
17
18 List<Student> studentList = new ArrayList<>();
19
20 try (Connection connection = JDBCUtils.getConnection();
21 PreparedStatement ps = connection.prepareStatement(SELECT_ALL_STUDENT)) {
22
23 ResultSet rs = ps.executeQuery();
24
25 while (rs.next()) {
26 long id = rs.getLong("id");
27 String name = rs.getString("name");
28 String surname = rs.getString("surname");
29 String location = rs.getString("location");
30 int studentindex = rs.getInt("studentindex");
31 long facultyid = rs.getLong("facultyid");
32
33 Student student = new Student(id, name, surname, location, studentindex, facultyid);
34 studentList.add(student);
35 }
36 } catch (SQLException e) {
37 System.err.println("Error fetching students: " + e.getMessage());
38 throw e;
39 }
40 return studentList;
41 }
42
43 @Override
44 public List<Student> getStudentsWithOddLengthNames() throws SQLException {
45 final String QUERY = "SELECT id, name, surname, location, studentindex, facultyid FROM student WHERE LENGTH(name) % 2 = 1";
46 List<Student> students = new ArrayList<>();
47
48 try (Connection connection = JDBCUtils.getConnection();
49 PreparedStatement preparedStatement = connection.prepareStatement(QUERY)) {
50
51 ResultSet rs = preparedStatement.executeQuery();
52
53 while (rs.next()) {
54 Long id = rs.getLong("id");
55 String name = rs.getString("name");
56 String surname = rs.getString("surname");
57 String location = rs.getString("location");
58 int studentindex = rs.getInt("studentindex");
59 Long facultyid = rs.getObject("facultyid") != null ? rs.getLong("facultyid") : null;
60
61 students.add(new Student(id, name, surname, location, studentindex, facultyid));
62 }
63 }
64 return students;
65 }
66
67 @Override
68 public Student getStudentById(Long id) throws SQLException {
69 final String QUERY = "SELECT id, name, surname, location, studentindex, facultyid FROM student WHERE id = ?";
70
71 try (Connection connection = JDBCUtils.getConnection();
72 PreparedStatement preparedStatement = connection.prepareStatement(QUERY)) {
73 preparedStatement.setLong(1, id);
74 ResultSet rs = preparedStatement.executeQuery();
75
76 if (rs.next()) {
77 String name = rs.getString("name");
78 String surname = rs.getString("surname");
79 String location = rs.getString("location");
80 int studentindex = rs.getInt("studentindex");
81 Long facultyid = rs.getLong("facultyid");
82
83 return new Student(id, name, surname, location, studentindex, facultyid);
84 }
85 }
86 return null;
87 }
88
89 @Override
90 public Student getByID(Long id) throws SQLException {
91 return getStudentById(id);
92 }
93
94 @Override
95 public void save(Student student) throws SQLException {
96 final String INSERT_SQL = "INSERT INTO Student (name, surname, location, studentindex, facultyid) VALUES (?, ?, ?, ?, ?)";
97
98 try (Connection connection = JDBCUtils.getConnection();
99 PreparedStatement ps = connection.prepareStatement(INSERT_SQL, Statement.RETURN_GENERATED_KEYS)) {
100
101 ps.setString(1, student.getName());
102 ps.setString(2, student.getSurname());
103 ps.setString(3, student.getLocation());
104 ps.setInt(4, student.getStudentindex()); // Matched to your model
105 ps.setLong(5, student.getFacultyid()); // Matched to your model
106 ps.executeUpdate();
107
108 ResultSet generatedKeys = ps.getGeneratedKeys();
109 if (generatedKeys.next()) {
110 student.setId(generatedKeys.getLong(1));
111 }
112 }
113 }
114
115 @Override
116 public void update(Student student) throws SQLException {
117 final String UPDATE_SQL = "UPDATE Student SET name = ?, surname = ?, location = ?, studentindex = ?, facultyid = ? WHERE id = ?";
118
119 try (Connection connection = JDBCUtils.getConnection();
120 PreparedStatement ps = connection.prepareStatement(UPDATE_SQL)) {
121
122 ps.setString(1, student.getName());
123 ps.setString(2, student.getSurname());
124 ps.setString(3, student.getLocation());
125 ps.setInt(4, student.getStudentindex());
126 ps.setLong(5, student.getFacultyid());
127 ps.setLong(6, student.getId());
128 ps.executeUpdate();
129 }
130 }
131
132 @Override
133 public void delete(Long id) throws SQLException {
134 final String DELETE_STUDENT_SQL = "DELETE FROM Student WHERE id = ?";
135
136 try (Connection connection = JDBCUtils.getConnection();
137 PreparedStatement ps = connection.prepareStatement(DELETE_STUDENT_SQL)) {
138 ps.setLong(1, id);
139 ps.executeUpdate();
140 }
141 }
142}
Note: See TracBrowser for help on using the repository browser.