我目前正在为我自己的项目开发一个登录系统。我是 Java 的新手,所以请不要苛刻。
我遇到的问题是: 我有一个工作正常的登录系统,但我想扩展它。在我的应用程序中,我有两种类型的员工。管理人员和员工。我想制作一个系统,如果您以经理身份登录,您会看到某个页面,如果您以员工身份登录,您会看到另一个页面。
到目前为止,我得到了这个: 代码:
数据库连接:
package databasetesten.connection;
import java.sql.*;
import javax.swing.*;
/**
*
* @author Matt Holland
*/
public class ConnectionUtil {
Connection conn = null;
public static Connection connectdb() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://mhhproject.nl:3306/Management", "root", "root");
return conn;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
}
登录申请:
package databasetesten;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Matt Holland
*/
public class LoginApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FXMLDocument.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="437.0" prefWidth="548.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40" fx:controller="databasetesten.FXMLDocumentController">
<children>
<Button fx:id="button" layoutX="204.0" layoutY="162.0" onAction="#handleButtonAction" text="Login" />
<Label fx:id="label" layoutX="204.0" layoutY="269.0" prefHeight="17.0" prefWidth="149.0" />
<TextField fx:id="textEmail" layoutX="200.0" layoutY="65.0" />
<Label layoutX="107.0" layoutY="69.0" text="Username" />
<Label layoutX="107.0" layoutY="119.0" text="Password" />
<PasswordField fx:id="textPassword" layoutX="200.0" layoutY="115.0" />
</children>
</AnchorPane>
FXMLMenu员工:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" `xmlns:fx="http://javafx.com/fxml/1">`
</AnchorPane>
FXMLMenuManager:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1">
</AnchorPane>
FXMLDocumentController:
package databasetesten;
import databasetesten.connection.ConnectionUtil;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
/**
*
* @author Matt Holland
*/
public class FXMLDocumentController implements Initializable {
@FXML
private TextField textEmail;
@FXML
private PasswordField textPassword;
Stage dialogStage = new Stage();
Scene scene;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
public FXMLDocumentController() {
connection = ConnectionUtil.connectdb();
}
@FXML
private void handleButtonAction(ActionEvent event) {
String email = textEmail.getText().toString();
String password = textPassword.getText().toString();
String sql = "SELECT * FROM Employee WHERE username = ? and password = ?";
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, email);
preparedStatement.setString(2, password);
resultSet = preparedStatement.executeQuery();
if (!resultSet.next()) {
infoBox("Enter Correct Email and Password", "Failed", null);
} else {
infoBox("Login Successfull", "Success", null);
Node source = (Node) event.getSource();
dialogStage = (Stage) source.getScene().getWindow();
dialogStage.close();
scene = new Scene(FXMLLoader.load(getClass().getResource(resultSet.getInt(1) == 0 ? "FXMLMenuEmployee.fxml" : "FXMLMenuManager.fxml")));
dialogStage.setScene(scene);
dialogStage.show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void infoBox(String infoMessage, String titleBar, String headerMessage) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(titleBar);
alert.setHeaderText(headerMessage);
alert.setContentText(infoMessage);
alert.showAndWait();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
}
名为 employee 的数据库表如下所示:
username password function
Bob1 Welcome123 0
Ben1 Bensw123 1
到目前为止,如果您以用户名 Bob1 和密码 Welcome123 登录,您将转到 FXMLMenuEmployee。如果您以用户名 Bob2 和密码 Bensw123 登录,您将转到 FXMLMenuManager。
在我的 FXMLDocumentController 中有一行代码:
scene = new Scene(FXMLLoader.load(getClass().getResource(resultSet.getInt(1) == 0 ? "FXMLMenuEmployee.fxml" : "FXMLMenuManager.fxml")));
我认为这段代码可以工作,但没有。 希望有人可以帮助我修复我的代码,使其正常工作!
最佳答案
鉴于您发布的表格表示:
username password function
Bob1 Welcome123 0
Ben1 Bensw123 1
好像是
resultSet.getInt(1);
在结果集的第一列中给出值,不会检查正确的字段。 (虽然我希望有一个异常(exception),因为字段值似乎不是数字。)
您可能需要 resultSet.getInt(3)
,尽管使用列标签而不是列索引更安全:
String resource = resultSet.getInt("function") == 0 ? "FXMLMenuEmployee.fxml" : "FXMLMenuManager.fxml"
scene = new Scene(FXMLLoader.load(getClass().getResource(resource)));
关于JavaFX登录不同页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47499836/