提问者:小点点

使用数据库创建新对象


我想使用我的数据库来实现addmodule函数,但它必须是一个循环,而不仅仅是一个模块,我希望它添加数据库中的所有数据来创建addmodule函数。我的数据库中有相关信息,但我不知道如何将其放入addmodule函数中以创建新对象(模块)

timetable.addModule(1,“cs1”,“计算机科学”,new int[] {1,2 });

这是我的addMoules:

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

对于使用数据库添加模块,准备好的语句是什么?但是用数组把所有的都加起来


共1个答案

匿名用户

您可以创建一个模型类来存储数据库记录。

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;
    }

}

使用以下方法使用数据库值实例化对象。我假设您的数据库表中有一个 professorId 的逗号分隔值列表。此外,假设 professorIds 列的数据类型为 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);

    }

您可以通过以下方式使用您的方法来添加模块。

// 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)); 
}

希望这对你有帮助!!!

使用以下内容更新Timetable中的模块。

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

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

    ...
}