try { if (stmt != null) stmt.close(); if (conn != null) conn.close(); } catch (Exception ignore) { }
  public String getCategories(String regionName, String username, String password) throws RemoteException  {    StringBuffer html = new StringBuffer();    Connection        conn = null;    PreparedStatement stmt = null;    ResultSet rs           = null;    String categoryName;    int categoryId;    int regionId = -1;    int userId = -1;    if (regionName != null && regionName !="")    {      // get the region ID      try       {        conn = dataSource.getConnection();        stmt = conn.prepareStatement("SELECT id FROM regions WHERE name=?");        stmt.setString(1, regionName);        rs = stmt.executeQuery();      }      catch (SQLException e)      {        try        {          if (stmt != null) stmt.close();          if (conn != null) conn.close();        }        catch (Exception ignore)        {        }        throw new RemoteException("Failed to get region Id " +e);      }      try      {        if (rs.first())        {          regionId = rs.getInt("id");        }      }      catch (Exception e)      {        try        {          if (stmt != null) stmt.close();          if (conn != null) conn.close();        }        catch (Exception ignore)        {        }        throw new RemoteException(" Region "+regionName+" does not exist in the database!<br>(got exception: " +e+")");      }    }    else    {      // Authenticate the user who wants to sell items      if ((username != null && username !="") || (password != null && password !=""))      {        SB_AuthHome authHome = null;        SB_Auth auth = null;        try         {          authHome = (SB_AuthHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/SB_Auth"), SB_AuthHome.class);          auth = authHome.create();        }         catch (Exception e)        {          try          {            if (stmt != null) stmt.close();            if (conn != null) conn.close();          }          catch (Exception ignore)          {          }          throw new RemoteException("Cannot lookup SB_Auth: " +e);        }        try         {          userId = auth.authenticate(username, password);        }         catch (Exception e)        {          try          {            if (stmt != null) stmt.close();            if (conn != null) conn.close();          }          catch (Exception ignore)          {          }          throw new RemoteException("Authentication failed: " +e);        }        if (userId == -1)        {           html.append(" You don't have an account on RUBiS!<br>You have to register first.<br>");           return html.toString();        }      }    }    try     {      conn = dataSource.getConnection();      stmt = conn.prepareStatement("SELECT name, id FROM categories");      rs = stmt.executeQuery();    }    catch (SQLException e)    {      try      {        if (stmt != null) stmt.close();        if (conn != null) conn.close();      }      catch (Exception ignore)      {      }      throw new RemoteException("Failed to get categories list " +e);    }    try     {      if (!rs.first())        html.append("<h2>Sorry, but there is no category available at this time. Database table is empty</h2><br>");      else      {        do        {          categoryName = rs.getString("name");          categoryId = rs.getInt("id");          if (regionId != -1)          {            html.append(printCategoryByRegion(categoryName, categoryId, regionId));          }          else          {            if (userId != -1)              html.append(printCategoryToSellItem(categoryName, categoryId, userId));            else              html.append(printCategory(categoryName, categoryId));          }        }        while (rs.next());      }      if (stmt != null) stmt.close();      if (conn != null) conn.close();    }     catch (Exception e)     {      try      {        if (stmt != null) stmt.close();        if (conn != null) conn.close();      }      catch (Exception ignore)      {      }      throw new RemoteException("Exception getting category list: " + e);    }    return html.toString();  }