0

以下 servlet 片段:

ResultSet set = statement.executeQuery();
       // userName = set.getString(1);
        if(set.next()) {
            userName = set.getString("FirstName");
            Email = set.getString("Email");
        }
        if(set.wasNull()) {  //<<------------- line 33
            // turn to the error page
            response.sendRedirect("LoginFailure.jsp");
        } else {
            // start the session and take to his homepage
            HttpSession session = request.getSession();
            session.setAttribute("UserName", userName);
            session.setMaxInactiveInterval(900); // If the request doesn't come withing 900 seconds the server will invalidate the session
            RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp");
            rd.forward(request, response); // forward to the user home-page
        }

创建以下异常:

INFO: java.sql.SQLException: Invalid operation: wasNull() called with no data retrieved.
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.ResultSet.wasNull(Unknown Source)
at com.sun.gjc.spi.base.ResultSetWrapper.wasNull(ResultSetWrapper.java:141)
--------->> at projectcodes.ValidateDataForSignIn.doPost(ValidateDataForSignIn.java:33)
    ..........

为什么会出现这个异常?由于突出显示的行而发生异常:33

4

1 回答 1

4

ResultSet#next()返回时会发生此异常false。即根本没有行,因此根本没有检索到任何列。ResultSet#wasNull()仅适用于最后检索的列,而不适用于最后检索的行。

您需要重新排列代码逻辑。

    if(set.next()) {
        userName = set.getString("FirstName");
        Email = set.getString("Email");

        // start the session and take to his homepage
        HttpSession session = request.getSession();
        session.setAttribute("UserName", userName);
        session.setMaxInactiveInterval(900); // If the request doesn't come withing 900 seconds the server will invalidate the session
        RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp");
        rd.forward(request, response); // forward to the user home-page
    } else {
        // turn to the error page
        response.sendRedirect("LoginFailure.jsp");
    }

更清楚的是将所有 JDBC 混乱重构为UserDAO具有模型类的独立 DAO 类,User然后按如下方式使用它:

User user = userDAO.find(username, password);

if (user != null) {
    request.getSession().setAttribute("user", user);
    request.getRequestDispatcher("portfolio_one.jsp").forward(request, response);
} else {
    response.sendRedirect("LoginFailure.jsp");
}

find()方法看起来像这样:

public User find(String username, String password) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    User user = null;

    try {
        connection = database.getConnection();
        statement = connection.prepareStatement("SELECT id, username, email, firstname, lastname, FROM user WHERE username = ? AND password = MD5(?)");
        statement.setString(1, username);
        statement.setString(2, password);
        resultSet = statement.executeQuery();

        if (resultSet.next()) {
            user = new User();
            user.setId(resultSet.getLong("id"));
            user.setUsername(resultSet.getString("username"));
            user.setEmail(resultSet.getString("email"));
            user.setFirstname(resultSet.getString("firstname"));
            user.setLastname(resultSet.getString("lastname"));
        }
    } finally {
        close(resultSet, statement, connection);
    }

    return user;
}

通过这种方式,您最终会获得更多的自我记录和更好的可重用/可测试代码。

也可以看看:

于 2012-04-24T17:11:13.397 回答