我正在为 SmartFox 服务器扩展创建一个 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)
{
}
}
}
}