问:如何从ComboBox的HashMap获取键值并将其保存到数据库中

时间:2017-12-06 05:01:09

标签: java sql javafx

我试图从ComboBox获取我的HashMap的Key值,但每次我使用我的ComboBox的变量时它只返回ComboBox的名称:cmbCourse。我希望代码获取键值并将其保存到我的数据库中。

这是我的插入代码

String query2 = ("INSERT INTO tblcourseinfo (user_id, course_id, school_id) VALUES (?,?,?)");
            QueryCourses qc = new QueryCourses();
            HashMap<String, Integer> map = qc.hashMap();
            stmt = conn.prepareStatement(query2);
            System.out.println(cmbCourse);
            System.out.println(cmbSchool);
            stmt.setInt(1, key);
            stmt.setObject(2, cmbCourse.getValue());
            stmt.setObject(3, cmbSchool.getValue());
            stmt.executeUpdate();

这就是我如何调用HashMap来填充ComboBoxes

public void populateCourse() throws SQLException, ClassNotFoundException {
    QueryCourses qc = new QueryCourses();
    HashMap<String, Integer> map = qc.hashMap();

    cmbCourse.getItems().setAll(map.keySet());
}

public void populateSchool() throws SQLException {
    QuerySchools qs = new QuerySchools();
    HashMap<String, Integer> map = qs.hashMap();

    cmbSchool.getItems().setAll(map.keySet());
}

这是填充它的查询

public class QueryCourses {    
public static ArrayList<Courses> getCourses() throws ClassNotFoundException, SQLException {
    String queryCourses = "SELECT course_id, course_title FROM tblcourses";
    Connection conn = DbConnection.ConnectDB();
    Statement stmt = conn.createStatement();
    ResultSet rs;
    rs = stmt.executeQuery(queryCourses);
    ArrayList<Courses> courseList = new ArrayList<>();
    while (rs.next()) {
        Courses crse = new Courses(rs.getInt("course_id"), rs.getString("course_title"));
        courseList.add(crse);
    }
    return courseList;
}

public HashMap<String, Integer> hashMap() throws SQLException, ClassNotFoundException{
    HashMap<String, Integer> map = new HashMap<>();
    try {            
        for (Courses crse : getCourses()) {
            map.put(crse.getCourseTitle(), crse.getCourseId());
        }
    } catch (SQLException ex) {
        Logger.getLogger(NewStudentController.class.getName()).log(Level.SEVERE, null, ex);
    }
    return map;
}
}

1 个答案:

答案 0 :(得分:1)

关注主要问题

  

如何获取所选项目的ID

最简单的方法是不使用HashMap,而是将Course对象直接添加到ComboBox ObservableList并更改项目的呈现方式

通过这种方式,您不必担心提供查找或其他花哨的pancy解决方案,因为您需要的所有信息都存储在Course对象本身中。

所以,我无法访问您的代码,所以我必须做一些事情,从Course对象开始......

public class Course {

    private long id;
    private String title;

    public Course(long id, String title) {
        this.id = id;
        this.title = title;
    }

    public long getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    @Override
    public String toString() {
        return "Course{" + "id=" + id + ", title=" + title + '}';
    }

}

当手头的时候我们可以创建自定义ListCell ...

public class CourseCell extends ListCell<Course> {

    @Override
    protected void updateItem(Course item, boolean empty) {
        super.updateItem(item, empty);
        if (item != null) {
            setText(item.getTitle());
        } else {
            setText(null);
        }
    }
}

然后我们需要将数据和渲染器一起应用......

// I can imagine this been loaded from a ResultSet
ObservableList<Course> options = FXCollections.observableArrayList(
                new Course(0, "JavaFX"),
                new Course(1, "Swing"),
                new Course(2, "Objective C"),
                new Course(3, "Swift")
);
ComboBox<Course> comboBox = new ComboBox<Course>(options);
comboBox.setCellFactory(new Callback<ListView<Course>, ListCell<Course>>() {
    @Override
    public ListCell<Course> call(ListView<Course> courses) {
        return new CourseCell();
    }
});
comboBox.setButtonCell(new CourseCell());

然后在某些时候,我们需要获得所选的值......

Course course = comboBox.getValue();
if (course != null) {
    System.out.println(course);
    System.out.println(course.getId());
}

getValue现在将返回Course个对象,包括titleid,方便。

请记住,您正在处理OO语言,尽可能保持数据的封装,它只会让生活变得更加简单。

可运行的示例...

这就像我刚写的第三个JavaFX应用程序,其中大部分是从教程,其他答案和一些博客中发现的,所以请原谅我任何明显的错误,我试着关注{{1}实施

YAML spec

ListCell