0

I have a question. How can I do asynchrous database in JavaFX. I know that exist SwingWoker but I read that I can't use this in JavaFX. I read about Task but I can do convert the result to ObservableList but I need normal LinkedList. I'm trying conntect to mysql database

I know that this forum has a lot of answering about database in javafx but all results are converted to ObservableList

Thank you for all the answers.

FlightControllerTask.java

public class FlightControllerTask extends Task<LinkedList<Flight>>{
private final static int MAX=10000;
ArrayList<Airport> airportList=new ArrayList<>()
@Override
protected LinkedList<Flight> call() throws Exception {
    for (int i = 0; i < MAX; i++) {  
        updateProgress(i, MAX);  
        Thread.sleep(5);  
   }  

    LinkedList<Flight> flightList = new LinkedList<>();
    Connection c ;
    c = DBConnector.connect(); 
    String SQL = "SELECT flight.idflight, airport.nameAirport, airport.nameAirport, flight.departureTime FROM flight INNER JOIN airport";
    ResultSet rs = c.createStatement().executeQuery(SQL);  
    while(rs.next()){  
        flightList.add(new Flight(rs.getInt("idflight"), rs.getString("flightFrom"), rs.getString("flightTo"), rs.getTime("departureTime")));       
   }  

    return flightList;
}

FlightControllerService

public class FlightControllerService extends Service<LinkedList<Flight>>{
@Override  
protected Task<LinkedList<Flight>> createTask() {  
     return new FlightControllerTask();  
}  

}

MainController.java

final FlightControllerService service= new FlightControllerService();
    ReadOnlyObjectProperty<LinkedList<Flight>> flightList =service.valueProperty();
    flightList.get();
Mile K
  • 3
  • 2
  • What has `Task` to do with `ObservableList`? You can use Task and inside it fetch results from database and store it in a `LinkedList` – ItachiUchiha Dec 15 '14 at 09:08
  • I got it :) Thanks , but How I can get a values, because I need to write to the console, but this LinkedList is ReadOnlyProperty – Mile K Dec 15 '14 at 09:39
  • I didn't get you. You need to return the `LinkedList` or iterate through the `LinkedList` and print the values. – ItachiUchiha Dec 15 '14 at 10:02
  • I put my code. Can you see this? – Mile K Dec 15 '14 at 10:18
  • Why use `ReadOnlyObjectProperty> flightList =service.valueProperty();`, when you can use `LinkedList flightList = service.getValue()`? – ItachiUchiha Dec 15 '14 at 11:51
  • How can I get my LinkedList? – Mile K Dec 15 '14 at 12:29
  • Your list is already in `flightList` reference. You should try to follow an example. [Try out this link](http://docs.oracle.com/javafx/2/threads/jfxpub-threads.htm#BABCCGCI) – ItachiUchiha Dec 15 '14 at 12:38

1 Answers1

0
public class FlightControllerTask extends Task<LinkedList<Flight>>{
  @Override
  protected LinkedList<Flight> call() throws Exception { 
    // load data
    return data;
  }
}

// usage:
FlightControllerTask task = new FlightControllerTask();
task.setOnSucceeded(ev -> task.getValue());
new Thread(task).start();

Now the part with task.getValue() is the crucial part, with this method you can retrieve the value that was computed with the task as soon as is is ready (thus the succeeded hook).

eckig
  • 10,964
  • 4
  • 38
  • 52
  • Are you sure I can use it in Javafx? cuz it doesn"t work – Mile K Dec 17 '14 at 10:33
  • Yes, that is why it its part of the JavaFX API... If you really want help, you need to explain what exactly is not working. – eckig Dec 17 '14 at 10:48