我知道这个问题可能看起来与之前发布的几个问题相似,但我尝试了多种解决方案,但似乎都没有用。所以基本上我有这个公共静态变量当前图书馆员,它在一个私有方法中被实例化,我需要能够从这个类之外访问这个当前图书馆员对象,从我读过的公共静态应该已经解决了这个但是它没有,我真的迷失了这个问题。
public class LoginForm extends javax.swing.JFrame {
/**
* Creates new form LoginForm
*/
public static Librarian currentLibrarian;
public static int loginAttemptsRemaining = 5;
/**
* failedAttempt() -- decrements the remaining login attempts, then updates the displayed value
* if the number of login attempts is exceeded, the login button is disabled and cannot be clicked again
*/
public void failedAttempt(){
loginAttemptsRemaining--;
attemptCountLabel.setText(""+loginAttemptsRemaining);
if(loginAttemptsRemaining ==0){
loginButton.setEnabled(false);
}
}
public Librarian getNewLibrarian(){
return this.currentLibrarian;
}
public LoginForm() {
initComponents();
}
private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {
if(usernameField.getText().isEmpty()||passwordField.getText().isEmpty()){
JOptionPane.showMessageDialog(null, "Please Appropriately Fill all Fields");
}
else{
String inputUsername = usernameField.getText();
int inputPassword = Integer.parseInt(passwordField.getText());
ConnectionDB connect= new ConnectionDB();
try {
Statement stm = (connect.connectDB()).createStatement();
String query = "select * from library.username_password where username = '"+inputUsername+"';";
ResultSet usernamePasswordResultSet = stm.executeQuery(query);
/**
* When an invalid username is requested from mySQL database, a 'false' boolean value is returned in Result Set
* Thus if the username input is invalid the result of userInfoResultSet.next() should be false
* furthermore .next() moves cursor to start position of the result set and allows us to begin reading values
*/
if(!usernamePasswordResultSet.next()){
failedAttempt();
JOptionPane.showMessageDialog(null, "Username is Invalid");
}
else{
/**if the username is valid we can retrieve the stored password at that location
* then verify password validity
*/
int databasePassword = usernamePasswordResultSet.getInt("password");
if(databasePassword != inputPassword){
failedAttempt();
JOptionPane.showMessageDialog(null, "Password is Invalid");
}
/**
* if both username and password are valid we need to verify what type of user we have
* then redirect them to the respective main menu
*/
else{
boolean isLibrarian = usernamePasswordResultSet.getBoolean("isLibrarian");
if(isLibrarian){
currentLibrarian = new Librarian(usernamePasswordResultSet.getBoolean("isHeadLibrarian"));
this.setVisible(false);
new LibrarianMainMenuForm().setVisible(true);