SFSDBManager class and Example Programs (com.smartfoxserver.v2.db.SFSDBManager)


> com.smartfoxserver.v2.db.SFSDBManager is a class which implements IDBManager interface and com.smartfoxserver.bitswarm.service.IService provided by the SFS2X platform.

> It manages the connection to a database using either JDBC native drivers or JDBC-ODBC bridge and providing configurable connection pooling for optimal performance and resource usage.

> Each Zone runs its own DbManager which can be configured via the Zone Configurator module in the SFS2X AdminTool. Additionally a Zone can instantiate multiple DbManagers via server side code.


Sno Method Syntax
1 getConnection() public Connection getConnection() throws SQLException
it will returns the sql.Connection instance with the specified drivers details at zone file.
2 executeInsert() public Object executeInsert(String sql, Object[] params) throws SQLException
Executes a SQL INSERT command returning the key of the inserted row
3 executeUpdate() void executeUpdate(java.lang.String sql)
void executeUpdate(java.lang.String sql, java.lang.Object[] params)

Executes a non-query SQL command such as INSERT, UPDATE, DELETE etc...
4 executeQuery() 1) ISFSArray executeQuery(java.lang.String sql)
2) ISFSArray executeQuery(java.lang.String sql, java.lang.Object[] params)
Perform a SQL query and return a structured object based on SFSArray and SFSObject
5 getActiveConnections() public int getActiveConnections()
Get the number of pooled connections currently active
6 isActive() public boolean isActive()
True if the Service is active
7 getIdleConnections() int getIdleConnections()
Get the number of pooled connections currently idle
8 getConfig() DBConfig getConfig()
Get the configuration details of the JDBC connection and connection pool

Sample Code Snippet to Illustrate above methods

</> SFSDBManager.executeQuery() Code Snippet
private void executeQueryExample() {
  try {
    IDBManager dbm = getParentZone().getDBManager();
    String query= "Select userid,password,chips,emailid,website from users";
    ISFSArray ret = dbm.executeQuery(query);
    StringBuffer msg = new StringBuffer();
    if (ret != null) {
      msg.append(" No Of Users Found "+ret.size()+"\n");
      for (int i = 0; i < ret.size(); i++) {
        ISFSObject temp = ret.getSFSObject(i);
        msg.append((i + 1) + "\tuserid" + temp.getUtfString("userid")
            + "\t password " + temp.getUtfString("password")
            + "\t chips " + temp.getDouble("chips")
            + "\t emailid " + temp.getUtfString("emailid")
            + "\n");
    } else {
      msg.append("NO records");
    } catch (SQLException e) {
</> SFSDBManager.executeUpdate() Code Snippet
private void executeUpdateExample() {
    try {
      SFSDBManager dbm = (SFSDBManager) getParentZone().getDBManager();
      dbm.executeUpdate("update users set website=?",
          new Object[] { "www.tutorialtous.com" });
    } catch (Exception e) {
</> SFSDBManager.executeInsert() Code Snippet
private void executeInsertExample(String userid, String password, Double chips, String emailid) {
    try {
      SFSDBManager dbm = (SFSDBManager) getParentZone().getDBManager();
      String query="insert into users(userid,password,regdate,chips,emailid) values(?,?,?,?,?)";
      Object obj = dbm.executeInsert(query,
              new Object[] { userid, password, new Date(), chips, emailid });
    } catch (Exception e) {

</> SFSDBManager.getDBConfig(),getActiveConnections(),getIdleConnections() Code Snippet
private void echoDbconnectionDetails() {
    try {
      SFSDBManager dbm = (SFSDBManager) getParentZone().getDBManager();
      DBConfig dbcfg = dbm.getConfig();
      trace("**** Db Configured With These Details ****");
      trace("Is Active: " + dbcfg.active + "\t Db Connection URL "
          + dbcfg.connectionString);
      trace("Driver Name: " + dbcfg.driverName
          + "\t MaxActiveConnections " + dbcfg.maxActiveConnections);
      trace("UserName: " + dbcfg.userName + "\t MaxActiveConnections "
          + dbcfg.maxActiveConnections);
      trace("TestSQL: " + dbcfg.testSql);
      trace("**** Done Db Configured With These Details Done ****");
      trace("**** getActiveConnections " + dbm.getActiveConnections());
      trace("**** getIdleConnections " + dbm.getIdleConnections());
    } catch (Exception e) {


trace() method is similar to System.out.println() it will echos the msg to console as well as log file.

Copyright © 2018-2020 TutorialToUs. All rights reserved.