HI真的需要这项任务的帮助。如何将登录窗口中的连接传递到主窗口。我有一个登录窗口,我要求用户选择服务器类型(例如:MySQL,SQL,MySQL本地),用户类型(例如:父,管理员,学生),数据库名称(他们想要连接的数据库的名称) to),服务器名称(root)和服务器密码。如果所有信息都正确,那么它将打开主窗口。现在我将该sql连接传递给我的主窗口,以便我可以编写代码将数据从我的数据库加载到我的表中。
所有需要帮助的是将连接传递给我的其他类或场景,舞台。这将在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";
}
}
}
这是我用于登录的FXML控制器
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;
}
}
这是我的主窗口的控制器,我有桌子
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 :(得分:0)
在您的Controller中实施一个方法,您可以在其中设置相应的舞台,例如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");
您可以访问控制器,通过主舞台,从而可以访问控制器内的舞台。