SmartfoxServer and MySQL With Example Programs

  • Step 1:- Configure the databaseManager under the zone in order to establish the communication between extension and database.
  • Step 2:- Open ZoneConfiguration using AdminPanel (default port is 8080 see this link to know adminpanel port ) and configure the database tab as
    adminpanel's DataBase Manager TAB
  • Step 3:- In the above Database manager tab we need to fill the fields, initially those are empty or filled with the data available in .zone.xml file.
  • Step 4:- Fill the Fields with these
    FieldName Description
    Activate enable it
    Database Driver Class com.mysql.jdbc.Driver
    ConnectionString jdbc:mysql://host:port/database
    Username root
    Password Password for the root user
    Test SQL select now()
    Max Active Connections 10
    Max Idle Connections 10
    Exhausted Pool Connections GROW (is prefered)
  • step 6 :- Follow This Step Only If you are unable to open ADMIN PANEL

Open the concerned .zone.xml file in any text editor. In SFS2X folder we will have a 'zones' folder in that our zone file will be there open it in any text editor (SFS2x->zones->

Find/Search the tag <databaseManager> and edit like this (sample mysql driver details are mentioned here fill with your db related entries there, for more about tags description check this link

<databaseManager active="true">
    <testSql>select now()</testSql>
  • Step 7:- Press the submit and reload buttons
  • Step 8:- Load the Mysql-Jdbc Jar File in the sfs2x’s lib folder or __ lib folder.
    Download the mysql-jdbc jar file from either of these links Mysql Dev JDBC Connector Official Link or from this link.
  • Step 7:- Assuming I have a class named "LoginRequestHandler" to handle the custom login in that I am writing the code as like this
 * @author
package com.tutorialtous.smartfoxserver;

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.entities.User;
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 LoginRequestHandler extends BaseServerEventHandler {

	public void handleServerEvent(ISFSEvent event) throws SFSException {
		User user = (User) event.getParameter(SFSEventParam.USER);
		ISession session = user.getSession();
		String username = user.getName();
		String pwd = (String) event.getParameter(SFSEventParam.LOGIN_PASSWORD);
		SFSErrorData errdata = null;
		String dbpwd = null;

		dbpwd = getPasswordFromDb(username);
		if (dbpwd != null) {
			if (getApi().checkSecurePassword(session, dbpwd, pwd)) {
				trace("login success for user " + username);
			} else {
				errdata = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD);
		} else {
			errdata = new SFSErrorData(SFSErrorCode.LOGIN_BAD_USERNAME);
		if (errdata != null) {
			throw new SFSLoginException("Login Failed",errdata);

	private String getPasswordFromDb(String username) {
		String password = null;
		try {
			IDBManager dbm = getParentExtension().getParentZone()
			ISFSArray resultset = dbm.executeQuery(
					"select pwd from users where userid=?",
					new Object[] { username });
			if (resultset != null) {
				ISFSObject obj = resultset.getSFSObject(0);
				password = obj.getUtfString("pwd");
		} catch (SQLException e) {
		return password;

 * @author
 * How to Run:-
 * Step1:- javac
 * step2:- java MainClass
package com.avishkarclient;

import java.util.Map;

import sfs2x.client.SmartFox;
import sfs2x.client.core.BaseEvent;
import sfs2x.client.core.IEventListener;
import sfs2x.client.core.SFSEvent;
import sfs2x.client.requests.LoginRequest;

import com.smartfoxserver.v2.exceptions.SFSException;

class SmartfoxJavaClient implements IEventListener {
	private SmartFox sfs;

	public SmartfoxJavaClient() {
		sfs = new SmartFox();
		sfs.addEventListener(SFSEvent.CONNECTION, this);
		sfs.addEventListener(SFSEvent.CONNECTION_LOST, this);
		sfs.addEventListener(SFSEvent.LOGIN, this);
		sfs.addEventListener(SFSEvent.LOGIN_ERROR, this);

	public void connectToServer() {
		sfs.connect("", 9934);

	public void sendLoginRequest(String username, String password,
			String zonename) {
		LoginRequest req = new LoginRequest(username, password, zonename);
		if (sfs.isConnected()) {
			System.out.println("LoginRequest Sent to Server");
		} else {
					.println("Please Connect to server to send the LoginRequest");

	public void dispatch(BaseEvent event) throws SFSException {
		Map params = event.getArguments();
		switch (event.getType()) {
			if ((Boolean) params.get("success")) {
				System.out.println("Connected to server");
			} else {
				System.out.println("Failed to connect server");
		case SFSEvent.LOGIN:
			System.out.println("Login Success");
		case SFSEvent.LOGIN_ERROR:
			System.out.println("Login Failed");


public class MainClass {

	public static void main(String args[]) {
		SmartfoxJavaClient obj = new SmartfoxJavaClient();
		obj.sendLoginRequest("player", "123456", "CasinoGameServer");