gpt4 book ai didi

java - 在启动时启动方法

转载 作者:行者123 更新时间:2023-12-01 16:47:13 26 4
gpt4 key购买 nike

我有一个名为databaseConnection的类,我想在应用程序启动时启动/执行它,似乎我应该从start方法(我称之为MainApp)调用它,但我似乎找不到正确的方法这样做,因此我的问题

我觉得很简单,但我真的搞不懂

数据库连接:

package albertheijnhandterminal;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.ResourceBundle;

/**
*
* @author Stanly
*/
public class databaseConnection {

private static String dbURL = "jdbc:derby://localhost:1527/AlbertHeijnHandterminal;create=true;user=StanlySuijk;password=hoi55455";
private static Connection connection = null;
private static Statement statement = null;

public static void main(String[] args) {

createConnection();
//insertUsers("");
//selectUsers();
//shutdown();

}

private static void createConnection() {
try {
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
//Get a connection
connection = DriverManager.getConnection(dbURL);

} catch (Exception exception) {

}

}

}

主应用程序:

package albertheijnhandterminal;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import static javax.management.remote.JMXConnectorFactory.connect;

/*
1
9 @author Stanly
9
9
*/
public class MainApp extends Application {


public static FrameController theFrameController = null;

static StackPane displayPane;

public static Connection connection = null;
public static Statement statement = null;
public static PreparedStatement preparedStatement = null;
public static ResultSet resultSet = null;

@Override
public void start(Stage stage) throws Exception {

Parent root = FXMLLoader.load(getClass().getResource("Frame.fxml"));

Scene scene = new Scene(root);

stage.setTitle("Albert Heijn Handterminal");
stage.setScene(scene);
stage.show();

}

public static void main(String[] args) {
launch(args);
}

}

我只编程了大约 6 个月,所以如果您看到我的代码有一些改进,我们将不胜感激。

最佳答案

如果您的应用程序是 MainApp 类,那么该类应该具有 main 方法。那么你应该执行以下操作。

public static void main(String[] args) {
databaseConnection db = new databaseConnection ();
db.createConnection();
launch(args);
}

或者调用另一个类的main方法。

如果不需要参数

public static void main(String[] args) {
databaseConnection.main(null);
launch(args);
}

如果需要参数

 public static void main(String[] args) {
databaseConnection.main(args);
launch(args);
}

更新:

正如 James_D 在评论中指出的:

Under some circumstances you might want the application to be launched using a "pure JavaFX launch", which would bypass the main() method entirely

在这种情况下,最好移动这个

databaseConnection db = new databaseConnection ();
db.createConnection();

start() 方法。

public void start(Stage stage) throws Exception {

databaseConnection db = new databaseConnection ();
db.createConnection();

Parent root = FXMLLoader.load(getClass().getResource("Frame.fxml"));

Scene scene = new Scene(root);

stage.setTitle("Albert Heijn Handterminal");
stage.setScene(scene);
stage.show();

}

关于java - 在启动时启动方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48341351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com