簡體   English   中英

將用戶輸入從一個 class 傳遞到 java 中的另一個?

[英]Pass user input from one class to another in java?

我正在創建一個簡單的聊天應用程序,用戶需要登錄,然后它連接到 mysql 表,該表根據現有記錄檢查用戶名和密碼。 它檢索用戶名結果集並將其傳遞到一個字符串中,然后從該字符串中對其進行檢查。 我希望服務器 class 訪問輸入,以便可以使用用戶名將消息存儲到數據庫中。 然后,服務器通過Runtime.getRuntime().exec()打開充當聊天室的命令提示符 window。 我試過吸氣劑,但 java 告訴我字符串是 null。 這是我的客戶 class 我已經聲明了我的變量:

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;
    }

那么這是整個Client的class方法:

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);
}

我試過getter方法

    public static String getusername(){return username;}

然后在另一個 class 作為 Client.getusername() 調用該方法后,Java 告訴我字符串是 null。 我的代碼中有兩個用戶名。 其中之一稱為usern usern接受用戶輸入,而username用於從數據庫中檢索實際的用戶名。 如果用戶輸入與檢索到的username匹配,則登錄成功。 正如您在我的代碼中看到的那樣,我已將usern聲明為全局變量,它是public static String usern; . 使用常見的 Java 數據結構(例如列表和集合)不起作用。 有人可以幫幫我嗎? 我想避免每次用戶打開聊天 window 時再次詢問用戶名。 我在 Java 方面沒有經驗,所以請 go 容易。

我想您在使用 static 時遇到問題。 客戶端 class 必須首先實例化,然后才能調用其方法。 但是 login() 和 getusername() 都是 static 並且可以在沒有實例化客戶端的情況下調用它們

Client.login()

這對構造函數 Client() 毫無意義。 以下代碼向您展示了它是如何工作的:

    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());
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM