Joe*_*ter 3 java exception smartfoxserver
我正在为SmartFox Server Extension创建一个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)
{
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您的第二个if语句终止,;这是一个有效的语句.并且你在下一个块中抛出异常,这就是错误的原因.
if(!getApi().checkSecurePassword(session, dbPword, pass));
Run Code Online (Sandbox Code Playgroud)
上面的if语句以分号结束,这是一个有效的语句,if语句将对它起作用,代码的其他部分正在执行而不管if语句是什么,并且最后抛出异常.
{
SFSErrorData errorData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD);
errorData.addParameter(email);
throw new SFSLoginException("Bad password for user: "+ email, errorData);
}
Run Code Online (Sandbox Code Playgroud)
这就是为什么你得到错误,因为你的线session.setProperty("DatabaseID", dbId);永远不会到达.
| 归档时间: |
|
| 查看次数: |
2909 次 |
| 最近记录: |