简体   繁体   中英

Pass user input from one class to another in java?

I am creating a simple chat application where the user is needed to login then it connects to a mysql table which checks the username and the password against existing records. It retrieves the username resultset and passes it into a string from where it is then checked. I want the input to be accessed by a server class so that messages can be stored using the username into the database. The server then, through Runtime.getRuntime().exec() , opens a command prompt window which acts as the chatroom. I tried getters but java tells me that the string is null. This is my Client class where I have declared my variables:

public class Client {
    static final String url = "jdbc path to database";
    static final String user = "myuser";
    static final String passwd = "mypassword";
    final static int ServerPort = 5000;
    static String serverAddress;
    static Scanner in;
    static PrintWriter out;
    static Scanner scan = new Scanner(System.in);
    static boolean menuloop = true;
    public static String usern;

    static int choice, choice0, choice1;

    public Client(String serverAddress) {
        this.serverAddress = serverAddress;
    }

Then this is the entire Client's class method:

public static void login() {
    boolean validate = false;
    do {
        try {
            validate = true;
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, user, passwd);
            System.out.println("Connection established");
            Scanner s = new Scanner(System.in);
            System.out.println("Enter username");
            String username = "";
            usern = s.nextLine();
            System.out.println("Enter password");
            String password = "";
            String pass = s.nextLine();
            Statement stmt = conn.createStatement();
            String SQL = "SELECT username,password FROM mytable WHERE username='" + usern + "' && password='" + pass + "'";

            ResultSet rs = stmt.executeQuery(SQL);

            while (rs.next()) {
                username = rs.getString("username");
                password = rs.getString("password");
            }

            if (usern.equals(username) && pass.equals(password)) {
                System.out.println("Successful Login!\n----");
            } else {
                System.out.println("Incorrect Username or Password\n----");
                validate = false;
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    } while (!validate);
}

I had tried the getter method

    public static String getusername(){return username;}

Then after calling the method in another class as Client.getusername(), Java tells me that the string is null. There are two usernames in my code. One of which is called usern . usern accepts user input while username is used to retrieve the actual username from the database. If the user input matches the retrieved username , then the login is successful. I had declared usern as a global variable as you can see in my code it is the public static String usern; . Using common Java data structures such as lists and sets didn't work. Can someone please help me? I want to avoid asking for the username again every time the user opens the chat window. I am not experienced in Java so please go easy.

I suppose you have problems with the usage of static. The Client class must be firstly instantiated before its methods could be invoked. But login() and getusername() are both static and they can be invoked WITHOUT the Client being instantiated

Client.login()

and that make no sense with the constructor Client(). The following codes show you how it works:

    import java.util.Scanner;
    import java.io.PrintWriter;
    public class Client {
    static final String url = "jdbc path to database";
    static final String user = "myuser";
    static final String passwd = "mypassword";
    final static int ServerPort = 5000;
    static String serverAddress;
    static Scanner in;
    static PrintWriter out;
    static Scanner scan = new Scanner(System.in);
    static boolean menuloop = true;
    public static String usern;

    static int choice, choice0, choice1;

    public Client(String serverAddress) {
        this.serverAddress = serverAddress;
    }
    public static void login() {
    boolean validate = false;
    do {
        try {
            Scanner s = new Scanner(System.in);
            System.out.println("Enter username");
            String username = "Joe";
            usern = s.nextLine();
            System.out.println("Enter password");
            String password = "Joe";
            String pass = s.nextLine();
            
            if (usern.equals(username) && pass.equals(password)) {
              validate = true;
                System.out.println("Successful Login!\n----");
            } else {
                System.out.println("Incorrect Username or Password\n----");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } while (!validate);
  }
  public static String getusername(){
     return usern;
  }
  public static void main(String... argv) {
     Client c = new Client("hello");
     c.login();
     System.out.println(c.getusername());
  }
}

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