I am trying to get the Key value of my HashMap from a ComboBox, but every time I use the variable of my ComboBox it just returns the name of the ComboBox: cmbCourse. I want the code to get the key value and save it into my database.
Here is my insertion code
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();
this is how I call the HashMap to populate the 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());
}
and here is the query to populate it
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;
}
}