0

I have this method here - whenever I comment out the MySQL section, from String query to pst.close() - the code works fine. However, if I don't, then it gives me the error of Connection Reset. I am using a socket between a client and a server for your information. How would I fix this? Or it it a problem that does not deal with MySQL?

public static String doAddUser(String username, String password, String email, String fullname) throws SQLException {
    String returnStatement = "";
    Connection connection = establishConnection();

    if(email.isEmpty() || password.isEmpty()) {
        returnStatement = "CreateFailure: Some fields are left blank. Please note that all fields are required. Please try again";
    }

    else {

        String query = "insert into test(`username`, `password`, `email`, `fullname`) values (?,?,?,?)";
        PreparedStatement pst = connection.prepareStatement(query);
        pst.setString(1, username);
        pst.setString(2, password);
        pst.setString(3, email);
        pst.setString(4, fullname);
        pst.executeUpdate();
        pst.close();
        returnStatement = "CreateSuccess: You are now registered!";
    }
    return returnStatement;
}
1
  • What does establishConnection do? Commented Apr 17, 2020 at 7:17

3 Answers 3

1

You should probably have a connection.close() before the return.

Or, if you are in a relatively new version of JRE, you could possibly achieve the same with try-with-resource.

public static String doAddUser(String username, String password, String email, String fullname) throws SQLException {

    String returnStatement = "";

    try (Connection connection = establishConnection()) {

        if(email.isEmpty() || password.isEmpty()) {
            returnStatement = "CreateFailure: Some fields are left blank. Please note that all fields are required. Please try again";
        }
        else {
            String query = "insert into test(`username`, `password`, `email`, `fullname`) values (?,?,?,?)";
            PreparedStatement pst = connection.prepareStatement(query);
            pst.setString(1, username);
            pst.setString(2, password);
            pst.setString(3, email);
            pst.setString(4, fullname);
            pst.executeUpdate();
            pst.close();
            returnStatement = "CreateSuccess: You are now registered!";
        }
    }

    return returnStatement;
}

This will ensure that your connection closes when the try block finishes. However, that would require that the Connection class implements the AutoCloseable interface.

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

1 Comment

For some reasons, I tried both closing the connection and try statement but both could not work. :(
0

I think the connection is getting established and not getting closed, might be the issue.

1 Comment

For some reasons, I tried both closing the connection and try statement but both could not work. :(
0

Here is what works for me, as suggested by @Binaek Sarkar (I modified it a little bit):

public static String doAddUser(String username, String password, String email, String fullname) throws SQLException {
    String returnStatement = "";
    try {
        if(email.isEmpty() || password.isEmpty()) {
            returnStatement = "CreateFailure: Some fields are left blank. Please note that all fields are required. Please try again";
            }
        else {
            Connection connection = null;
            connection = MySQLConnection.dbConnector();
            String query = "INSERT INTO `test`(`username`, `password`, `email`, `fullname`) VALUES (?,?,?,?)";
            PreparedStatement pst = connection.prepareStatement(query);
            pst.setString(1, username);
            pst.setString(2, password);
            pst.setString(3, email);
            pst.setString(4, fullname);
            pst.executeUpdate();
            pst.close();

            returnStatement = "CreateSuccess: You are now registered! You will redirected to the login page now. ";
        }
    }catch(Exception e) {returnStatement = "Invalid. Please try again!";}

    return returnStatement;
}

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.