how to connect to database in drupal in order to display the content in a table
You need add the followings lines in settings.php for connecting 'external' database.

$databases['default']['default'] = array(
      'database' =&gt; 'drupal8',
      'username' =&gt; 'username',
      'password' =&gt; 'password',
      'prefix' =&gt; '',
      'host' =&gt; 'localhost',
      'port' =&gt; '3306',
      'namespace' =&gt; 'Drupal\\Core\\Database\\Driver\\mysql',
      'driver' =&gt; 'mysql',
    );

$databases['external']['default'] = array(
  'database' =&gt; 'external',
  'username' =&gt; 'username',
  'password' =&gt; 'password',
  'prefix' =&gt; '',
  'host' =&gt; 'localhost',
  'port' =&gt; '3306',
  'namespace' =&gt; 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' =&gt; 'mysql',
);


To activate external database

\Drupal\Core\Database\Database::setActiveConnection('external');


//code to fetch data and display

To activate drupal database

\Drupal\Core\Database\Database::setActiveConnection();


If you are looking some example refer How to connect to another database in your Drupal 8 site ?