'Null Data when trying to insert Faculty Data during POST REQUEST
Hello I'm trying to insert the faculty data into the database with this json input, Faculty data needs to be inserted into the faculty table, student data into student table, subject data into subject table and grade data into grade table.
{
"id": 9,
"university_id": 8,
"name": "FSHMN",
"enrolled_students": [
{
"id": "9",
"username": "IN",
"password": "admin",
"fullname": "Depp",
"email": "[email protected]",
"subjects": [
{
"id": 5,
"name": "Programim 5",
"Grade": [
{
"grade_id": 0,
"mark": 10,
"description": "Excellent"
}
]
},
{
"id": 6,
"name": "Programim 6",
"Grade": [
{
"grade_id": 0,
"mark": 9,
"description": "Very Good"
}
]
},
{
"id": 7,
"name": "Calculus 2",
"Grade": [
{
"grade_id": 0,
"mark": 8,
"description": "Good"
}
]
},
{
"id": 8,
"name": "Discrete mathematics",
"Grade": [
{
"grade_id": 0,
"mark": 7,
"description": "Well"
}
]
}
]
},
{
"id": "8",
"username": "Student4",
"password": "stu55",
"fullname": "tudent",
"email": "[email protected]",
"subjects": [
{
"id": 1,
"name": "Programim 1",
"Grade": [
{
"grade_id": 0,
"mark": 6,
"description": "Barely Passed"
}
]
},
{
"id": 2,
"name": "Programim 2",
"Grade": [
{
"grade_id": 0,
"mark": 6,
"description": "Barely Passed"
}
]
},
{
"id": 3,
"name": "Calculus",
"Grade": [
{
"grade_id": 0,
"mark": 7,
"description": "Well"
}
]
},
{
"id": 4,
"name": "Discrete mathematics",
"Grade": [
{
"grade_id": 0,
"mark": 8,
"description": "Good"
}
]
}
]
},
{
"id": "5",
"username": "Johnn",
"password": "d123",
"fullname": "Doe",
"email": "[email protected]",
"subjects": [
{
"id": 1,
"name": "Programim 1",
"Grade": [
{
"grade_id": 0,
"mark": 9,
"description": "Very Good"
}
]
},
{
"id": 2,
"name": "Programim 2",
"Grade": [
{
"grade_id": 0,
"mark": 10,
"description": "Excellent"
}
]
},
{
"id": 3,
"name": "Calculus",
"Grade": [
{
"grade_id": 0,
"mark": 9,
"description": "Very Good"
}
]
},
{
"id": 4,
"name": "Discrete mathematics",
"Grade": [
{
"grade_id": 0,
"mark": 8,
"description": "Good"
}
]
}
]
}
]
}
I get this Error:
java.sql.SQLIntegrityConstraintViolationException: Column 'username' cannot be nullData insert unsuccessful
During Debugging I found out that the StudentList that is defined in the Faculty Class, the subjectList in the Student class and the markList in the Subject Class is filled with data based on the json input:
But when it gets to the preparedstatement of the student class the data is passed as null and the faculty id is passed as the student id:
I know that I have not defined my main method properly to handle Lists inside classes that contain data.
Here is the main method that is Called:
public Faculty insertFacultyStudentSubjectGrade(Faculty faculty,Student student, Subject subject, StudentMark mark) {
Connection connection = null;
String insertFaculty = "insert into faculty(fid,university_id,fname) values (?,?,?)";
String insertStudent="insert into student(user_id,username,password,fullname,email) values (?,?,?,?,?)";
String insertSubject="insert into subject(id,name) values (?,?)";
String insertGrade="insert into grade(mark,description) values(?,?)";
try {
connection = new MysqlDbConnectionService().getConnection();
PreparedStatement ps = connection.prepareStatement(insertFaculty);
ps.setInt(1, faculty.getFid());
ps.setInt(2, faculty.getUniversityid());
ps.setString(3, faculty.getFname());
ps.execute();
PreparedStatement ps1 = connection.prepareStatement(insertStudent);
ps1.setString(1, student.getId());
ps1.setString(2, student.getUsername());
ps1.setString(3, student.getPassword());
ps1.setString(4, student.getFullName());
ps1.setString(5, student.getEmail());
ps1.execute();
PreparedStatement ps2 = connection.prepareStatement(insertSubject);
ps2.setInt(1, subject.getId());
ps2.setString(2, subject.getName());
ps2.execute();
PreparedStatement ps3 = connection.prepareStatement(insertGrade);
ps3.setInt(1,mark.getMark());
ps3.setString(2,mark.getDescription());
ps3.execute();
} catch (Exception e) {
System.out.println(e + "Data insert unsuccessful");
}
return faculty;
}
Here is the Jersey method:
@Path("faculty/fs")
@POST
public Response addFacultyStudentSubjectGrade(String payload) {
Faculty faculty = new Gson().fromJson(payload, Faculty.class);
Student student = new Gson().fromJson(payload, Student.class);
Subject subject = new Gson().fromJson(payload, Subject.class);
StudentMark mark = new Gson().fromJson(payload, StudentMark.class);
return Response.ok(new Gson().toJson(facultyService.insertFacultyStudentSubjectGrade(faculty,student,subject,mark))).build();
Here are my Classes:
Faculty Class:
package com.common.db.domain;
import com.google.gson.annotations.SerializedName;
import java.util.Collections;
import java.util.List;
public class Faculty {
@SerializedName("id")
private int fid;
@SerializedName("university_id")
private int university_id;
@SerializedName("name")
private String fname;
@SerializedName("enrolled_students")
private List<Student> studentList;
public Faculty() {
this.fid = fid;
this.university_id=university_id;
}
public void setFid(int fid)
{
this.fid = fid;
}
public int getFid()
{
return fid;
}
public void setUniversityid(int university_id)
{
this.university_id=university_id;
}
public int getUniversityid()
{
return university_id;
}
public void setFname(String fname)
{
this.fname = fname;
}
public String getFname()
{
return fname;
}
public void setStudentList(List<Student> studentList) {
this.studentList = studentList;
}
public List<Student> getStudentList()
{
return studentList;
}
}
Student Class:
package com.common.db.domain;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Student {
@SerializedName("id")
private String id;
@SerializedName("username")
private String username;
@SerializedName("password")
private String password;
@SerializedName("fullname")
private String fullName;
@SerializedName("email")
private String email;
@SerializedName("subjects")
private List<Subject> subjectList;
public Student() {
}
public Student(String id, String username, String password, String fullName, String email) {
super();
this.id = id;
this.username = username;
this.password = password;
this.fullName = fullName;
this.email = email;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<Subject> getSubjectList() {
return subjectList;
}
public void setSubjectList(List<Subject> subjectList) {
this.subjectList = subjectList;
}
}
Subject Class:
package com.common.db.domain;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Subject {
@SerializedName("id")
private int id;
@SerializedName("name")
private String name;
@SerializedName("Professor")
private List<Professor> professorList;
@SerializedName("Grade")
private List<StudentMark> markList;
public Subject() {
this.id = id;
this.name=name;
}
public void setId(int id)
{
this.id=id;
}
public int getId()
{
return id;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
public List<Professor> getProfessorList() {
return professorList;
}
public void setProfessorList(List<Professor> professorList) {
this.professorList = professorList;
}
public List<StudentMark> getMarkList() {
return markList;
}
public void setMarkList(List<StudentMark> markList) {
this.markList = markList;
}
}
Mark Class:
package com.common.db.domain;
public class StudentMark {
private int grade_id;
private int mark;
private String description;
public int getGrade_id() {
return grade_id;
}
public void setGrade_id(int grade_id) {
this.grade_id = grade_id;
}
public int getMark() {
return mark;
}
public void setMark(int mark) {
this.mark = mark;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Here is the ER DIAGRAM:
I'm struggling to find a way to access the List Data individually so I can SQL insert the data to the corresponding table, so what are the ways to make this work because I don't know how to do it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|



