2

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.

4
  • That is not the exception. The Exception is what causes that error message to be printed. Please include the exception and the stacktrace. Commented May 4, 2016 at 15:33
  • When an Exception is caught, you just print "Exception caught: client disconnected.", and then, YOU disconnect the client. You should print the stack trace of the real Exception, as suggested below. Commented May 4, 2016 at 15:34
  • Also, your username and password will be incorrectly escaped (missing quotes) in the final query, that's a probable SQLException . Have a look at PreparedStatement to avoid escaping problems and SQL injection. Commented May 4, 2016 at 15:41
  • Yup, that was the problem. stacktrace wasn't included and the problem was with the unimported mysql jdbc driver. Thanks again! Commented May 4, 2016 at 15:48

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.