title: Using threads to make database requests
body: <p>I'm trying to understand how threads works in java. This is a simple database request that returns a ResultSet. I'm using JavaFx.</p>
  
  <pre><code>    package application;
  
  import java.sql.ResultSet;
  import java.sql.SQLException;
  
  import javafx.fxml.FXML;
  import javafx.scene.control.Button;
  import javafx.scene.control.Label;
  import javafx.scene.control.TextField;
  
  public class Controller{
      @FXML
      private Button getCourseBtn;
      @FXML
      private TextField courseId;
      @FXML
      private Label courseCodeLbl;
      private ModelController mController;
  
      private void requestCourseName(){
          String courseName = "";
          Course c = new Course();
          c.setCCode(Integer.valueOf(courseId.getText()));
          mController = new ModelController(c);
          try {
              ResultSet rs = mController.&lt;Course&gt;get();
              if(rs.next()){
                  courseCodeLbl.setText(rs.getString(1));
              }
          } catch (SQLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  //      return courseName;
      }
  
      public void getCourseNameOnClick(){
          try {
  //              courseCodeLbl.setText(requestCourseName());
              Thread t = new Thread(new Runnable(){
                  public void run(){
                      requestCourseName();
                  }
              }, "Thread A");
              t.start();
          } catch (NumberFormatException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <p>This returns an exception: </p>
  
  <blockquote>
    <p>Exception in thread "Thread A" java.lang.IllegalStateException: Not on FX application thread; currentThread = Thread A</p>
  </blockquote>
  
  <p>How do I correctly implement threading so that every database request is executed in a second thread instead of the main thread?</p>
  
  <p>I've heard of implementing Runnable but then how do I invoke different methods in run method? </p>
  
  <p>Never worked with threading before but I thought it's time for it.</p>