79833508

Date: 2025-11-29 20:18:41
Score: 1
Natty:
Report link

This is the solution I have come up with after implementing everyone's input in case someone else runs into a similar problem.

Starting with the student table:

registration Number firstName lastName dateofBirth password idGroup

The updated addStudent function with SQL injection safety (Thanks for the clarification Tim Moore) :

public boolean addStudent( Student s) throws SQLException {
        String request="INSERT INTO student values (?,?,?,?,?,?);";
        PreparedStatement pst=myConnection.getMyConnection().prepareStatement(request);
        pst.setString(1, s.getRegistrationNumber());
        pst.setString(2, s.getFirstName());
        pst.setString(3, s.getLastName());
        pst.setDate(4, s.getDateOfBirth());
        pst.setString(5, s.getPassword());
        pst.setInt(6, s.getGroup().getIdGroup());
        return pst.executeUpdate()>0;
    }

s.getGroup().getIdGroup() saves the student's group to the foreign key idGroup.

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Mohamed Selmi