简体   繁体   中英

How can I get the username after successful login?

I am trying to display username after user Login to the system in Java. There have any way?

login.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // JDBC driver name and database URL
               final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
                final String DB_URL = "jdbc:mysql://localhost/unimas";

               //  Database credentials
                final String USER = "root";
                final String PASS = "shojib420";
                Connection conn = null;
                Statement stmt = null;
                try {
                    dec();
                } catch (NoSuchAlgorithmException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
            //String name=name2.getText();
            String username=user1.getText();
            //String faculty=fac1.getText();
            if(username.equalsIgnoreCase("") ||  password.equalsIgnoreCase(""))
            {
                JOptionPane.showMessageDialog(contentPane, "OOPS You miss some filed");
            }
            else
            {
            try {

                  Class.forName("com.mysql.jdbc.Driver");
                  conn = (Connection) DriverManager.getConnection(DB_URL, USER, PASS);
                  stmt = (Statement) conn.createStatement();

              /* String sql="INSERT INTO useruser " +
                       "VALUES (username,password, name,faculty)";*/
                String sql="SELECT * FROM useruser WHERE username='"+username+"' and password='"+password+"' ";

               ResultSet rs=stmt.executeQuery(sql);
               if(rs.next())
               {

                   dashboard d=new dashboard();
                   d.setVisible(true);
                   d.name=username;
                   dispose();

               }
               else
               {
                   JOptionPane.showMessageDialog(contentPane, "Sorry "+password);
               }

            }catch(SQLException se){
                  //Handle errors for JDBC
                  se.printStackTrace();
               }catch(Exception e1){
                      //Handle errors for Class.forName
                      e1.printStackTrace();
                   }
            }

        }
    });

Here d.name=username; name is in the dashboard class where I am trying to set d.name=username; , But i am not able to get the value of username

    JTextArea showName = new JTextArea();
    showName.setBounds(29, 25, 105, 22);
    contentPane.add(showName);
    showName.setName(name);

I am calling that name in dashboard class. Any idea where I am doing wrong or better suggestion?

dashboard d=new dashboard();
d.setVisible(true);
d.name=username;

Here you just set class variable but the variable is not set in any label (or textarea or textfield).

Should be

dashboard d=new dashboard();
d.nameLabel.setText(username);
d.setVisible(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM