无法写入MySQL时重置连接

问题描述 投票:0回答:3

我在这里有此方法-每当我注释掉MySQL部分时,从String querypst.close()-代码都可以正常工作。但是,如果我不这样做,那么它将给我错误Connection Reset。我正在客户端和服务器之间使用套接字来获取信息。我该如何解决?还是它不能解决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;
}
java mysql jdbc
3个回答
1
投票

您可能应该在返回之前输入connection.close()

或者,如果您使用的是相对较新的JRE版本,则可以使用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;
}

这将确保您在try块结束时关闭连接。但是,这将需要Connection类实现AutoCloseable接口。


0
投票

我认为连接正在建立并且没有关闭,可能是问题所在。


0
投票

这是对我有用的,正如@Binaek Sarkar所建议的(我做了一点修改):

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.