2016-11-24 6 views
0

안녕하세요.이 과제에 도움이 필요합니다. 내 로그인 창에서 내 메인 창으로 연결을 어떻게 전달합니까? (예 : MySQL, SQL, MySQL 로컬), 사용자 유형 (예 : 부모, 관리자, 학생), 데이터베이스 이름 (연결하려는 데이터베이스의 이름)을 사용자에게 묻는 로그인 창이 있습니다. 서버 이름 (루트) 및 서버 암호. 모든 정보가 올 바르면 주 창이 열립니다. 이제 SQL 연결을 메인 윈도우에 전달하여 데이터베이스에서 내 테이블로 데이터를로드하도록 코드를 작성할 수 있습니다.JavaFX 및 FXML : 장면 간 연결 전달

다른 도움이 필요하면 다른 클래스 또는 장면, 스테이지에 연결하십시오. 이것은 3 일 안에 만기가됩니다. FXML 코드가 필요한 경우에도 보낼 수 있습니다. , 난 정말 여기

내 연결 코드 여기

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"; 
    } 
} 
} 

입니다 연결에게 전달하는 데 도움이 필요 제발입니다 로그인 여기

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; 
} 

} 

내 FXML 컨트롤러 내 여기에 대한 컨트롤러 내 테이블이있는 기본 창

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(); 
} 

} 

답변

0

Cont 롤러는 예를 들어 단계별로 설정할 수 있습니다. public void setMain(Stage main){}; 그런 시도 :

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

당신이 정적 메소드

을 사용하는 대신 로더를 인스턴스화 할 경우 Parent root = FXMLLoader.load("Style.fxml");

당신이 컨트롤러에 접근 할 수 귀하의 메인 무대를 통과하고, 따라서에 액세스 할 수 있습니다

컨트롤러 내부의 무대.