Click here to Skip to main content
15,888,287 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one JavaFX Login Form and there are 2 Text Fields and 1 Login button. and they have connected to Database. What I want do is that when both Text Fields are empty and if user presses on login button there should be prompt saying that "INVALID DETAILS" and if user enters correct username and password then login successful.

I have done the Databse connection part and if condition too. but the if condition is not working. It is only working for one time. What I mean is Suppose I have run the program and directly click on login button without entering the data into the Text Fields it's showing the INVALID DETAILS error only for one time. If I again click on login without entering the data then I'm facing the error. If I entered data in Text Fields then it's working fine. It's saving the data into my table. But IF CONDITION is not working well.

Please help me to solve this problem.

What I have tried:

Java
package sample2;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javax.swing.JOptionPane;


public class FXMLDocumentController implements Initializable {
    
    Connection conn=null;
    PreparedStatement pst=null;
    
  @FXML
    private JFXTextField passwordtxt;

    @FXML
    private JFXTextField usernametxt;

    @FXML
    private JFXButton loginbtn;

    @FXML
    void loginbtnclick(ActionEvent event) 
    {
        String user=usernametxt.getText();
        String pass=passwordtxt.getText();
            
        if(user.contentEquals("") && pass.contains(""))
        {
            JOptionPane.showMessageDialog(null,"Login failed");
        }
       else
        {
        String sql="insert into new (nn,nnn) values(?,?)";
        try
        {
           
           Class.forName("com.mysql.jdbc.Driver");
           conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/userdatabase?zeroDateTimeBehavior=convertToNull","root","12345678");
            pst = (PreparedStatement)conn.prepareStatement(sql);
           pst.setString(1,user);
           pst.setString(2,pass);
            pst.execute();
           JOptionPane.showMessageDialog(null,"Record Inserted Successfully");
           usernametxt.setText(null);
        passwordtxt.setText(null);
        }
        
        
        catch(Exception e)
            
        {
       // e.printStackTrace(null);
        }
        }

    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        
    }    
    
}
Posted
Updated 21-Aug-17 6:24am
Comments
Richard MacCutchan 21-Nov-16 5:43am    
What error and where does it occur?
KD Palekar 21-Nov-16 12:43pm    
Suppose I have run the program and directly click on login button without entering the data into the text boxes it's giving me the error as I wanted. Now suppose I have entered the data into text boxes and click on login button it's also working fine as I wanted.

But the problem is Suppose now I directly click on login button without entering the data into the text boxes it's giving me list of error. I think that this is because of Database Connection still remains open somewhere. Because programs run smooth until I enter data something into table. After data entering if I leave the boxes empty and click on login button the problem starts

ERROR LIST -
Executing E:\Documents\NetBeansProjects\sample2\dist\run702429951\sample2.jar using platform C:\Program Files (x86)\Java\jdk1.8.0_91\jre/bin/java
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scen
Richard Deeming 21-Nov-16 12:47pm    
pass.contains("")

Is that really what you wanted to write? Depending on how the contains method is implemented, either every string contains the empty string, or none of them do.

Also, you probably want an OR, not an AND, in that condition. Otherwise, you're only rejecting the input if both fields are empty.

I suspect you want: if(user.contentEquals("") || pass.contentEquals(""))
KD Palekar 21-Nov-16 12:52pm    
Yes sir. I have made some changes to IF CONDITION code but still facing the same issue. Thanks for that password field suggestion but right now what I need is to get rid of that error. So please tell me how can I solve that issue.

CHANGES MADE TO THE CODE -

if(user.isEmpty()||pass.isEmpty())
{
Alert alert=new Alert(Alert.AlertType.ERROR);
alert.setHeaderText(null);
alert.setContentText("INVALID DETAILS");
alert.showAndWait();
return;
}
Richard Deeming 21-Nov-16 13:03pm    
So what is the error? The details in your previous comment seem to be incomplete - you've just got a stack trace for an unhandled exception in your event handler. You'd need the full details of the exception that gets thrown within the event handler.

so the answer is I have to write the IF CONDITION as below :

Java
if (user == null || user.isEmpty() || pass == null || pass.isEmpty())



THE ANSWER IS GIVEN BY
Richard Deeming


HUGE THANKS SIR!!!
 
Share this answer
 
It take me more than an hour trying to find ways how to solve it but I can't find solution online, until I realize my mistake.
What I've done is instead of setting my textfield to be cleared like this [ textfield.setText(null) ] , I set it to [ textfield.setText("") ] to avoid NullPointerException... Just in case this was not solved yet...
 
Share this answer
 
Comments
CHill60 21-Aug-17 15:05pm    
It is solved. That's why there is an accepted solution
It take me more than an hour trying to find ways how to solve it but I can't find solution online, until I realize my mistake.
What I've done is instead of setting my textfield to be cleared like this [ textfield.setText(null) ] , I set it to [ textfield.setText("") ] to avoid NullPointerException... Just in case this was not solved yet...


Try to set your code
usernametxt.setText(null);
passwordtxt.setText(null);
into
usernametxt.setText("");
passwordtxt.setText("");
and everything will work great!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900