title: Q: How to get key value from a HashMap of a ComboBox and save it to the database
body: <p>
  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.</p>
  
  <p>Here is my insertion code</p>
  
  <pre><code>String query2 = ("INSERT INTO tblcourseinfo (user_id, course_id, school_id) VALUES (?,?,?)");
              QueryCourses qc = new QueryCourses();
              HashMap&lt;String, Integer&gt; 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();
  </code></pre>
  
  <p>this is how I call the HashMap to populate the ComboBoxes</p>
  
  <pre><code>public void populateCourse() throws SQLException, ClassNotFoundException {
      QueryCourses qc = new QueryCourses();
      HashMap&lt;String, Integer&gt; map = qc.hashMap();
  
      cmbCourse.getItems().setAll(map.keySet());
  }
  
  public void populateSchool() throws SQLException {
      QuerySchools qs = new QuerySchools();
      HashMap&lt;String, Integer&gt; map = qs.hashMap();
  
      cmbSchool.getItems().setAll(map.keySet());
  }
  </code></pre>
  
  <p>and here is the query to populate it</p>
  
  <pre><code>public class QueryCourses {    
  public static ArrayList&lt;Courses&gt; 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&lt;Courses&gt; courseList = new ArrayList&lt;&gt;();
      while (rs.next()) {
          Courses crse = new Courses(rs.getInt("course_id"), rs.getString("course_title"));
          courseList.add(crse);
      }
      return courseList;
  }
  
  public HashMap&lt;String, Integer&gt; hashMap() throws SQLException, ClassNotFoundException{
      HashMap&lt;String, Integer&gt; map = new HashMap&lt;&gt;();
      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;
  }
  }
  </code></pre>