3

SmartFoxServerExtension用のJavaクラスを作成しています。MySQLデータベースにアクセスしようとしています。

Unreachable Code回線で呼び出されたエラーを受信して​​いますsession.setProperty("DatabaseID", dbId);

package sfs2x.extension.test.dblogin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.smartfoxserver.bitswarm.sessions.ISession;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.db.IDBManager;
import com.smartfoxserver.v2.exceptions.SFSErrorCode;
import com.smartfoxserver.v2.exceptions.SFSErrorData;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.exceptions.SFSLoginException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;

public class LoginEventHandler extends BaseServerEventHandler 
{
    @Override
    public void handleServerEvent(ISFSEvent e) throws SFSException 
    {
        String email = (String)e.getParameter(SFSEventParam.LOGIN_NAME);
        String pass = (String)e.getParameter(SFSEventParam.LOGIN_PASSWORD);
        ISession session = (ISession)e.getParameter(SFSEventParam.SESSION);

        IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
        Connection connection = null;

        try
        {
            connection = dbManager.getConnection();

            PreparedStatement stmt = connection.prepareStatement("SELECT * FROM users WHERE email=?");
            stmt.setString(1, email);

            ResultSet res = stmt.executeQuery();

            if(!res.first())
            {
                SFSErrorData errData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_USERNAME);
                errData.addParameter(email);

                throw new SFSLoginException("Bad user name: "+ email, errData);
            }

            String dbPword = res.getString("password");
            int dbId = res.getInt("id");

            if(!getApi().checkSecurePassword(session, dbPword, pass));
            {
                SFSErrorData errorData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD);
                errorData.addParameter(email);

                throw new SFSLoginException("Bad password for user: "+ email, errorData);
            }

            session.setProperty("DatabaseID", dbId);
           //UNREACHABLE CODE
           //IF I COMMENT THIS OUT, THERE IS NO UNREACHABLE CODE ERROR

        }

        catch(SQLException eve)
        {
            SFSErrorData erroData = new SFSErrorData(SFSErrorCode.GENERIC_ERROR);
            erroData.addParameter("SQL Error: " + eve.getMessage());

            throw new SFSLoginException("A SQL Error occurred: " + eve.getMessage(), erroData);
        }

        finally
        {
            try 
            {
                connection.close();
            }
            catch (SQLException e1) 
            {

            }
        }
    }

}
4

3 に答える 3

7

2番目のifステートメントがそれで終了する;のは有効なステートメントです。次のブロックで例外をスローしているので、エラーが発生します。

if(!getApi().checkSecurePassword(session, dbPword, pass));

上記のifステートメントは有効なステートメントであるセミコロンで終了し、ifステートメントがそれに作用する場合、コードの他の部分はifステートメントに関係なく実行され、最後に例外がスローされます。

{
    SFSErrorData errorData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD);
    errorData.addParameter(email);

    throw new SFSLoginException("Bad password for user: "+ email, errorData);
}

session.setProperty("DatabaseID", dbId);これが、回線が到達しないためにエラーが発生する理由です。

于 2013-02-22T13:00:17.530 に答える
3

;前のコードブロックの前に偽物があります:

if(!getApi().checkSecurePassword(session, dbPword, pass));
                                                      // ^
                                                      // |
                                                      // +---- remove this ';'
{
   ...
   throw new SFSLoginException("Bad password for user: "+ email, errorData);
}

session.setProperty("DatabaseID", dbId);

したがってthrow、は常に実行されるため、コードがに到達することはありませんsession.setProperty()

于 2013-02-22T13:00:23.530 に答える
0

;2番目の直後に不要なものがあるためif、次の{}ブロックは常に実行されます。つまり、aSFSLoginException常にスローされ、実行はにジャンプしcatchます。

これにより、setPropertyメソッドが呼び出されることはありません。

コード内の次のステートメントからセミコロンを削除する必要があります。

if(!getApi().checkSecurePassword(session, dbPword, pass));

于 2013-02-22T13:04:35.950 に答える