2016-11-24 10 views
0

HI brauchen wirklich Hilfe bei dieser Aufgabe. Wie kann ich eine Verbindung von meinem Anmeldefenster zu meinem Hauptfenster weitergeben? Ich habe ein Login-Fenster, in dem ich den Benutzer nach einem ausgewählten Servertyp frage (Beispiel: MySQL, SQL, MySQL Local), Benutzertyp (Ex: Parent, Admin, Student), Datenbankname (der Name der Datenbank, die sie verbinden wollen) to), Servername (Root) und Serverpasswort. Wenn alle Informationen korrekt sind, wird das Hauptfenster geöffnet. Jetzt übergebe ich diese SQL-Verbindung an mein Hauptfenster, damit ich Daten aus meiner Datenbank in meine Tabelle laden kann.JavaFX und FXML: Passing Connection zwischen Szene, Bühne

Alle brauchen Hilfe bei der Verbindung zu meiner anderen Klasse oder Szene, Bühne. Dies ist in 3 Tagen fällig. Wenn Sie den FXML-Code benötigen, kann ich sie auch senden. Bitte, bitte ich wirklich Hilfe brauchen Verbindung zu passieren

Hier ist meine Verbindungscode ist

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.util.Enumeration; 
import java.util.Scanner; 
import javafx.scene.control.Alert; 


public class Connections { 

private static Alert al; 

public static final int MYSQLLOCAL = 1; 
public static final int MYSQLREMOTE= 2; 
public static final int SQLSERVERLOCAL = 3; 
public static final int SQLSERVER = 4; 
public static final int UNKNOWN = -1; 


public static java.sql.Connection getconnect(int which, String name, String uid, String pass) { 
    Scanner scan = new Scanner(System.in); 
    java.sql.Connection connection = null; 

    String driver = getDriver(which); 
    String url = getURL(which, name); 
    System.out.println(driver); 
    System.out.println(url); 
    try 
    { // load the driver 
      Class.forName(driver).newInstance(); 
      System.out.println("Known drivers that are registered:"); 
      Enumeration enumer = DriverManager.getDrivers(); 
      while (enumer.hasMoreElements()) 
        System.out.println(enumer.nextElement()); 
    } 
    catch(ClassNotFoundException | InstantiationException | IllegalAccessException e) 
    { 
     return null; 
    } 
    try 
    { 
     connection = DriverManager.getConnection(url, uid, pass); 
     System.out.println("Connection pass"); 
    } 
    catch(Exception e) 
    { 
     return null; 
    } 
    return connection; 

    } 

public static Connection connect(int which, String name) { 
    java.sql.Connection connection = null; 
    String driver = getDriver(which); 
    String url = getURL(which, name); 
    System.out.println(driver); 
    System.out.println(url); 
    try { // load the driver 
      Class.forName(driver).newInstance(); 
      System.out.println("Known drivers that are registered:"); 
      Enumeration enumer = DriverManager.getDrivers(); 
      while (enumer.hasMoreElements()) 
        System.out.println(enumer.nextElement()); 
    } 
    catch(ClassNotFoundException | InstantiationException | IllegalAccessException e) 
    { 
    return null; 
    } 

    try { 
     connection = DriverManager.getConnection(url, "", ""); 
     System.out.println("Connection successful!"); 

    } 
    catch(SQLException e) 
    { 
    al = new Alert(Alert.AlertType.INFORMATION); 
    al.setTitle("Error"); 
    al.setHeaderText(null); 
    al.setContentText("Login failed. Please make sure all information" 
      + " are correct"); 
    al.showAndWait(); 

    return null; 
    } 
    return connection; 
} 

public static String getDriver(int num) { 
    switch (num) { 
    case SQLSERVER: 
      return "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 
    case MYSQLLOCAL: 
      return "com.mysql.jdbc.Driver"; 
    case MYSQLREMOTE: 
      return "com.mysql.jdbc.Driver"; 
    case SQLSERVERLOCAL: 
      return "com.microsoft.sqlserver.jdbc.SQLServerDriver"; 
    default: 
      return "error"; 
    } 
} 
public static String getURL(int num, String names) { 
    Scanner scan = new Scanner(System.in); 
    String name = names; 
    switch (num) { 
     case SQLSERVER: 
     { 
      if (name.equals("default")) 
        return "jdbc:sqlserver://164.106.3.23:9012"; 
      else 
        return "jdbc:sqlserver://164.106.3.23:9012" + "; databaseName=" + name; 
      // change this to match your ODBC connection name 
     } 
     case MYSQLLOCAL: 
     { 
      return "jdbc:mysql://localhost:3306/"+name; 
     } 
     case MYSQLREMOTE: 
     { 
      return "jdbc:mysql://164.106.3.22:3098/"+ name; 
     } 
     default: 
      return "error"; 
    } 
} 
} 

Hier ist meine FXML Controller für die Anmeldung

import java.sql.Connection; 
import java.sql.SQLException; 
import java.sql.Statement; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.event.Event; 
import javafx.fxml.*; 
import javafx.scene.*; 
import javafx.scene.control.*; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.Pane; 
import javafx.stage.Stage; 

public class GiantsLoginController { 

public String dataName, serverName, password; 
public int num; 

private Connection connect = null; 
private Statement stmt = null; 

private boolean userPass, connected; 

private Connections connection; 


@FXML 
private ComboBox<String> sType; 
@FXML 
public TextField dbName; 
@FXML 
private TextField sName; 
@FXML 
private Button loginB; 
@FXML 
private PasswordField sPassword; 
@FXML 
private Pane paneL; 
@FXML 
private GridPane gPane; 
@FXML 
private ComboBox<String> uType; 


ObservableList<String> sLists = FXCollections.observableArrayList("MySQL LOCAL", 
     "MYSQL REMOTE", "SQL SERVER LOCAL", "SQL SERVER"); 
ObservableList<String> uList = FXCollections.observableArrayList("Player", 
     "Admin"); 



@FXML 
public void initialize() { 
    sType.setItems(sLists); 
    uType.setItems(uList); 
} 

@FXML 
public void loginBClick (Event event) { 
    if (isAllFieldFillup()) { 

     switch(uType.getValue().trim()) { 
      case "Admin": 
       if (connectCheck()) { 
        try { 
         Parent giantsAdmin = FXMLLoader.load(getClass().getResource("GiantsAdmin.fxml"));      
         Scene gAdminScene = new Scene(giantsAdmin); 
         Stage gAdminStage = (Stage) ((Node) event.getSource()).getScene().getWindow(); 
         gAdminStage.hide(); 
         gAdminStage.setScene(gAdminScene); 
         gAdminStage.setTitle("Giants Admin"); 
         gAdminStage.getScene().getStylesheets().add(getClass().getResource("style.css").toExternalForm()); 
         gAdminStage.show(); 
        } 
        catch (Exception e) { 

        } 
       } 
      case "Player": 
       if (connectCheck()) { 
        try { 
         Parent giantsPlayer = FXMLLoader.load(getClass().getResource("GiantsPlayer.fxml")); 
         Scene gPlayerScene = new Scene(giantsPlayer); 
         Stage gPlayerStage = (Stage) ((Node) event.getSource()).getScene().getWindow(); 
         gPlayerStage.hide(); 
         gPlayerStage.setScene(gPlayerScene); 
         gPlayerStage.setTitle("Giants Playe"); 
         gPlayerStage.getScene().getStylesheets().add(getClass().getResource("style.css").toExternalForm()); 
         gPlayerStage.show(); 
        } 
        catch (Exception e) { 

        } 
       } 
     } 
    } 
} 

public void closeConnection() { 

    if (connect != null) { 
     try { 
      stmt.close(); 
      connect.close(); 
     } 
     catch (SQLException e) { 

     } 
    } 
} 

public boolean connectCheck() { 
    connected = false; 

    dataName = dbName.getText(); 
    serverName = sName.getText(); 
    password = sPassword.getText(); 


    switch (sType.getValue()) { 
     case "MySQL LOCAL": 
      num = 1; 
      break; 
     case "MYSQL REMOTE": 
      num = 2; 
      break; 
     case "SQL SERVER LOCAL": 
      num = 3; 
      break; 
     case "SQL SERVER": 
      num = 4; 
      break; 
     default: 

    } 

    if (connect == null) { 
     connect = Connections.getconnect(num, dataName, serverName, password); 
    } 

    if (connect == null) { 
     System.out.println("Still no connection"); 
    } 

    if (stmt == null) { 
     try { 
      stmt = connect.createStatement(); 
      connected = true; 
     } catch (SQLException e) { 
      Alert notify = new Alert(Alert.AlertType.INFORMATION); 
      notify.setTitle("Blank filed"); 
      notify.setHeaderText(null); 
      notify.setContentText("Incorrect login."); 
      notify.showAndWait(); 

      connected = false; 
     } 


    } 
    return connected; 
} 

private boolean isAllFieldFillup() { 
    boolean allInfo; 
    if (sType.getValue().equals("server type") && dbName.getText().isEmpty() 
      && sName.getText().isEmpty() && sPassword.getText().isEmpty()) { 
     Alert notify = new Alert(Alert.AlertType.INFORMATION); 
     notify.setTitle("Blank filed"); 
     notify.setHeaderText(null); 
     notify.setContentText("You are missing some information."); 
     notify.showAndWait(); 

     allInfo = false; 
    } 
    else { 
     allInfo = true; 
    } 
    return allInfo; 
} 

} 

Hier ist meine hier ist der Controller für meine Hauptfenster, wo ich die Tabelle habe

import java.io.IOException; 
import java.sql.Statement; 
import javafx.collections.*; 
import javafx.event.Event; 
import javafx.fxml.*; 
import javafx.scene.Node; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.stage.Stage; 

public class GiantsAdminController { 
@FXML 
private Button connect = null; 
private boolean connected; 

private Statement stmt; 

@FXML 
private TextField aRank; 
@FXML 
private TextField aName; 
@FXML 
private TextField aPosition; 
@FXML 
private TextField aSchool; 
@FXML 
private TextField aAge; 
@FXML 
private TextField aWar; 
@FXML 
private Button clearB; 
@FXML 
private Button addB; 
@FXML 
private TableColumn<?, ?> rank; 
@FXML 
private TableColumn<?, ?> name; 
@FXML 
private TableColumn<?, ?> position; 
@FXML 
private TableColumn<?, ?> school; 
@FXML 
private TableColumn<?, ?> age; 
@FXML 
private TableColumn<?, ?> war; 
@FXML 
private TextField qSearch; 
@FXML 
private Button search; 
@FXML 
private Button singout; 
@FXML 
private Button delete; 
@FXML 
private ComboBox<String> serverType; 
@FXML 
private TextField dbName; 
@FXML 
private TextField serverName; 
@FXML 
private TextField sPassword; 

ObservableList<String> sLists = FXCollections.observableArrayList("MySQL LOCAL", 
     "MYSQL REMOTE", "SQL SERVER LOCAL", "SQL SERVER"); 
@FXML 
public void initialize() { 
    serverType.setItems(sLists); 
} 

@FXML 
public void clearBClick (Event event) { 
    aRank.clear(); 
    aName.clear(); 
    aPosition.clear(); 
    aSchool.clear(); 
    aAge.clear(); 
    aWar.clear(); 
} 




@FXML 
public void SingOutClick(Event event) throws IOException { 


    ((Node)event.getSource()).getScene().getWindow().hide(); 
    Stage stage = new Stage(); 
    //stage.hide(); 
    Parent giantsLogin = FXMLLoader.load(getClass().getResource("/giants/GiantsLogin.fxml")); 

    Scene gLScene = new Scene(giantsLogin); 
    gLScene.getStylesheets().add(getClass().getResource("style.css").toExternalForm()); 
    stage.setScene(gLScene); 
    stage.show(); 
} 

} 

Antwort

0

Implementieren Sie eine Methode in Ihrem Cont Walze, in der Sie die entsprechende Stufe z. public void setMain(Stage main){}; Dann versuchen:

FXMLLoader loader = new FXMLLoader(getClass().getResource("Style.fxml")); 
Parent root = loader.load(); 
YourController cc = loader.getController(); 
cc.setMain(primaryStage); 

Wenn Sie den Loader anstelle der Verwendung der statischen Methode

Parent root = FXMLLoader.load("Style.fxml"); instatiate

Sie Zugang zu Ihrem Controller bekommen, Ihre Hauptbühne passieren und so den Zugriff auf die bekommen Bühne in Ihrem Controller.

Verwandte Themen