0

HI really need help with this assignment. How can I pass a connection from my login window to my main window. I have a login window where I ask the user to a select server type (Ex: MySQL, SQL, MySQL Local), user type (Ex: Parent, Admin, Student), database name (the name of the database they want to connect to), server name (root), and server password. If all information are correct, then it will open the main window. Now I pass that sql connection to my main window so I can code to load data from my database to my table.

All need help with is to pass connect to my other class or scene, stage. This is due in 3 days. If you need the FXML code I can send them too. Please, Please I really need the help to pass connection

Here is my connection code

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

Here is my FXML controller for login

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

}

Here is my here is the controller for my main window where I have the table

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

}
Jabateh
  • 65
  • 1
  • 13

1 Answers1

1

Implement a method in your Controller in which you can set the according stage e.g. public void setMain(Stage main){}; Then try:

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

If you instatiate the loader instead of using the static method

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

you can get access to your controller, pass your main stage and thus get access to the stage inside your controller.

SilverMonkey
  • 1,003
  • 7
  • 16