Męczę sie od paru godzin z problemem asynchronicznosci bazy danych w JavieFX. W Swingu miałam SwingWorkera i dawalo to rade bez problemu. W JavieFx czytałam o Taskach ale nie umiem sobie poradzić z problemem aby wyniki z sql były zwracane do zwykłego LinkedListu abym miała swobodny dostep do zawartości.
public class DBConnector {
private static Connection conn;
private static String url = "jdbc:mysql://localhost:3306/FlightControllerDB";
private static String user = "root";
private static String pass = "Milena123?";
public static Connection connect() throws SQLException{
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(ClassNotFoundException cnfe){
System.err.println("Error: "+cnfe.getMessage());
}catch(InstantiationException ie){
System.err.println("Error: "+ie.getMessage());
}catch(IllegalAccessException iae){
System.err.println("Error: "+iae.getMessage());
}
conn = DriverManager.getConnection(url,user,pass);
return conn;
}
public static Connection getConnection() throws SQLException, ClassNotFoundException{
if(conn !=null && !conn.isClosed())
return conn;
connect();
return conn;
}
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;
}
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();
Zawsze flightlist zwraca mi null.
Umie ktoś pomóc z tym zagadnieniem?