Question

I have jframe named LoginScreen for connect to DB. One textfield for username inputs, one password field for password inputs, one Login button and one checkbox for remember user name value. I want when user click Username Remember checkbox and attempt to login, when username and password is accepted from DB values must be stored in pref key. Let me share code;

public class LoginScreen extends javax.swing.JFrame {

    /**
     * Creates new form LoginScreen 
     */
    public LoginScreen() {
        initComponents(); //initialize components
        if ( "null" == PREF_NAME){
        rememberCheckBox.setSelected(false);
        }
        else if ("null" != PREF_NAME){
        rememberCheckBox.setSelected(true);    
        }

        if (true == rememberCheckBox.isSelected()){
        unameTextField.setText(PREF_NAME);
        }
        else if(false == rememberCheckBox.isSelected()){
        unameTextField.setText("your user name");
        }
    }

Preferences prefs = Preferences.userNodeForPackage(rememberexample.LoginScreen.class);
String PREF_NAME = "null";

private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:

        try {  
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        String connectionUrl = "jdbc:sqlserver://192.168.100.100;" + "databaseName=ExampleDB;" + "user=" + unameTextField.getText() + ";" + "password=" + new String (jPasswordField1.getPassword()) + ";"; 
        Connection con = DriverManager.getConnection(connectionUrl);
            if (true == rememberCheckBox.isSelected()){
            prefs.put(PREF_NAME, unameTextField.getText());
            System.out.println(PREF_NAME);
            }
            else if (false == rememberCheckBox.isSelected()){
            prefs.remove(PREF_NAME);
            System.out.println(PREF_NAME);
            }
        con.close();
        }
        catch (SQLException e) {
            JOptionPane.showMessageDialog(this, "Wrong id or password!!");
        }
        catch (ClassNotFoundException cE) {
            System.out.println("Class Not Found Exception: "+ cE.toString());
        }
    }   

public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoginScreen().setVisible(true);
            }
        });
    }

private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JButton loginButton;
private javax.swing.JCheckBox rememberCheckBox;
private javax.swing.JTextField unameTextField;  

But PREF_NAME always returns null. I have add one control to button action performed as you can see. System.out.println(PREF_NAME); returns null when button clicked. Any idea ?

Was it helpful?

Solution

you put:

 String PREF_NAME = "null";

then you are never changing its value (and you should not, see later..) and you are printing out:

 System.out.println(PREF_NAME);

==> obviously you get "null" as output


What you should do instead:

 final String PREF_NAME = "pref_name"; //define the name for your key

then later:

 prefs.put(PREF_NAME, unameTextField.getText()); //put something in your preference using your key
 System.out.println(prefs.get(PREF_NAME, "no value")); //output the preference *value* not the key

see the documentation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top