Logger Al
Logger Al

Reputation: 3

create new object using the database

I want to use my database in order to do the addmodule function but it has to be in a loop and not just 1 module, i want it tot add all data from database to create the addModule function. My database has the relevant information but i dont know how to get it into addmodule function to create new object(module)

timetable.addModule(1, "cs1", "Computer Science", new int[] {1, 2});

and this is my addModule:

public void addModule(int moduleId, String moduleCode, String module, int professorIds[]) { this.modules.put(moduleId, new Module(moduleId, moduleCode, module, professorIds)); }

What would the prepared statement be for adding module using the database? but with array so adds all of it

Upvotes: 0

Views: 239

Answers (1)

Harshana
Harshana

Reputation: 534

You can create a model class to store the database records.

public class Course {

    int moduleId;
    String moduleCode;
    String module;
    List<Integer> professorIds;

    public Course() {

    }

    public Course(int moduleId, String moduleCode, String module, List<Integer> professorIds) {
        this.moduleId = moduleId;
        this.moduleCode = moduleCode;
        this.module = module;
        this.professorIds = professorIds;
    }

    public int getModuleId() {
        return moduleId;
    }

    public void setModuleId(int moduleId) {
        this.moduleId = moduleId;
    }

    public String getModuleCode() {
        return moduleCode;
    }

    public void setModuleCode(String moduleCode) {
        this.moduleCode = moduleCode;
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public List<Integer> getProfessorIds() {
        return professorIds;
    }

    public void setProfessorIds(List<Integer> professorIds) {
        this.professorIds = professorIds;
    }

}

Use the following way to instantiate your object with the database values. I assumed that you have a comma separated values list for professorIds in your database table. Also, assumed the data type of professorIds column is VARCHAR.

    String query = "SELECT moduleId, moduleCode, module, professorIds from Course";
    PreparedStatement ps = null;
    Connection conn = null;
    ResultSet rs = null;

    // initialize the conn variable before execute the query
    // use a try block to handle exceptions properly

    ps = conn.prepareStatement(query);

    rs = ps.executeQuery();

    List<Course> courseList = new ArrayList<Course>();

    while (rs.next()) {

        Course course = null;

        List<String> professorIdsStrList = Arrays.asList(rs.getString("professorIds").split("\\s*,\\s*"));
        List<Integer> professorIdsIntList = professorIdsStrList.stream()
                         .map(s -> Integer.parseInt(s))
                         .collect(Collectors.toList());

        course = new Course(rs.getInt("moduleId"), rs.getString("moduleCode"), rs.getString("module"), professorIdsIntList);

        courseList.add(course);

    }

You can use your method in the following way to add modules.

// use this method call inside the database records reading method
addModule(rs.getInt("moduleId"), rs.getString("moduleCode"), rs.getString("module"), professorIdsIntList));

public void addModule(int moduleId, String moduleCode, String module, List<Integer> professorIds) { 
    this.modules.put(moduleId, new Module(moduleId, moduleCode, module, professorIds)); 
}

Hope this helps you!!!

Use the following to update the modules in TimeTable.

int index = 1;
while (rs.next()) {
    ...

    timetable.modules.put(index, module);
    index++;

    ...
}

Upvotes: 0

Related Questions