简体   繁体   中英

How to retrieve data from MySQL with Java threads?

I have a scenario where multiple clients connect to one server which retrieves data from MySQL table. In this case it is users table with id, username and password columns. On server side is following code:

Server.java (part of code)

public void run() {
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
        PrintWriter writer = new PrintWriter(client.getOutputStream(), true);
        writer.println("[type 'bye' to disconnect]");
        String commandType = null, username = null, password = null;
        while (true) {

            String line = reader.readLine();
            String[] parts = line.split(" ");

            for (int i = 0; i < parts.length; i++) {
                commandType = parts[0];
                username = parts[1];
                password = parts[2];
            }

            if (commandType.equals("!login")) {
                if (db.login(username, password)) {
                    writer.println("Welcome");

                } else {
                    writer.println("Wrong credentials");
                }
            }

            if (line.trim().equals("bye")) {
                writer.println("bye!");
                break;
            }

        }
    } catch (Exception e) {
        System.err.println("Exception caught: client disconnected.");
    } finally {
        try {
            client.close();
        } catch (Exception e) {
            ;
        }
    }
}

And in DBManager class I have login method:

public synchronized boolean login(String username, String password) throws SQLException {
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/database", "root", "root");

    Statement st = con.createStatement();

    String sql = "SELECT username, password FROM users WHERE username = " + username + " AND password = "
            + password;

    ResultSet rs = st.executeQuery(sql);

    while (rs.next()) {

        String uname, pass;
        uname = rs.getString("username");
        pass = rs.getString("password");

        if (username.equals(uname) && password.equals(pass)) {
            return true;
        }

    }
    rs.close();
    st.close();
    con.close();
    return false;
}

So, when client sends:

!login admin admin

i can outprint commandtype, username and password with

writer.println("Client sends commandType: " + commandType + " , username: " + username + " password: " + password);

, but if i want to check if there is admin admin in users table, the client automatically disconnects from server, and on server side exception is thrown: client is disconnected.

Client disconeccted though it sounds like a system error message, it is not! It's something that you produce. Instead of this:

} catch (Exception e) {
    System.err.println("Exception caught: client disconnected.");
} finally {

do

} catch (Exception e) {
    e.printStackTrace();
} finally {

that will show you what the real error is.

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