title: Why is my program crashing when MySQL server is unavalibe
body: <p>I've been programming in C++ with VS 2010 Professional but I am stuck on this problem:</p>
  
  <p>If the program starts and the connection is good then it loads the "gameactive" loop, and displays the high score of the player.</p>
  
  <p>But when the connection has a error it freezes and crashes. I want to display a error like "error connecting to server" and continue loading the game.</p>
  
  <p>I did use this tutorial:
  <a href="http://r3dux.org/2010/11/how-to-use-mysql-connectorc-to-connect-to-a-mysql-database-in-windows/" rel="nofollow">http://r3dux.org/2010/11/how-to-use-mysql-connectorc-to-connect-to-a-mysql-database-in-windows/</a></p>
  
  <p>Here is my code:</p>
  
  <pre><code>// Standad C++ includes
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;string&gt;
  using namespace std;
  #include &lt;string.h&gt;
  
  // Include the Connector/C++ headers
  #include "cppconn/driver.h"
  #include "cppconn/exception.h"
  #include "cppconn/resultset.h"
  #include "cppconn/statement.h"
  #include &lt;cgl\cgl.h&gt;
  #include &lt;cgl\core.h&gt;
  #include &lt;core\corefile.h&gt;
  
  // Link to the Connector/C++ library
  #pragma comment(lib, "mysqlcppconn.lib")
  
  // Specify our connection target and credentials
  const string server   = "tcp://xxx.xxx.xxx.xxx:3306";
  const string username = "xxxxxxx";
  const string password = "XXxxXXxxXXxx";
  
  char myoutput[1024];
  char myoutput1[1024];
  char myoutput2[1024];
  
  s_font font;
  s_bitmap bmp_background;
  
  int connectsetup()
  {
      sql::Driver     *driver; // Create a pointer to a MySQL driver object
      sql::Connection *dbConn; // Create a pointer to a database connection object
      sql::Statement  *stmt;   // Create a pointer to a Statement object to hold our SQL commands
      sql::ResultSet  *res;    // Create a pointer to a ResultSet object to hold the results of any queries we run
  
      // Try to get a driver to use to connect to our DBMS
      try
      {
          driver = get_driver_instance();
      }
      catch (sql::SQLException e)
      {
          cout &lt;&lt; "Could not get a database driver. Error message: " &lt;&lt; e.what() &lt;&lt; endl;
      }
  
      // Try to connect to the DBMS server
      try
      {
          dbConn = driver-&gt;connect(server, username, password);
      }
      catch (sql::SQLException e)
      {
          cout &lt;&lt; "Could not connect to database. Error message: " &lt;&lt; e.what() &lt;&lt; endl;
      }
  
      stmt = dbConn-&gt;createStatement(); // Specify which connection our SQL statement should be executed on
  
      // Try to query the database
      try
      {
          stmt-&gt;execute("USE runner");              // Select which database to use. Notice that we use "execute" to perform a command.
  
          res = stmt-&gt;executeQuery("SELECT * FROM highscores"); // Perform a query and get the results. Notice that we use "executeQuery" to get results back
      }
      catch (sql::SQLException e)
      {
          cout &lt;&lt; "SQL error. Error message: " &lt;&lt; e.what() &lt;&lt; endl;
      }
  
      // While there are still results (i.e. rows/records) in our result set...
      while (res-&gt;next())
      {
          // ...get each field we want and output it to the screen
          // Note: The first field/column in our result-set is field 1 (one) and -NOT- field 0 (zero)
          // Also, if we know the name of the field then we can also get it directly by name by using:
          // res-&gt;getString("TheNameOfTheField");
          //printf(myoutput,"%s %s %s",res-&gt;getString(1),res-&gt;getString(2),res-&gt;getString(3));
          strcpy(myoutput,res-&gt;getString(1).c_str());
          strcpy(myoutput1,res-&gt;getString(2).c_str());
          strcpy(myoutput2,res-&gt;getString(3).c_str());
      }
  
      // Clean up after ourselves
      delete res;
      delete stmt;
      delete dbConn;
  
      return 0;
  }
  
  void coremain()
  {
      //Fullscreen, windowed of scaled?
      corefile_mountimage("res",MOUNT_DIR);
      CGL_InitVideo(1280, 720, CGL_VIDEO_NONE);
      CGL_SetTitle("CGL - Endless poepert");
      CGL_InitFont("font_heat.tga", &amp;font);
      CGL_LoadBitmap("track1.tga",&amp;bmp_background);
      connectsetup();
      int gameactive=1;
  
      int getal=atoi(myoutput);
  
      do {
          do {
              CGL_WaitRefresh();
              CGL_DrawBitmap(0,0,bmp_background); 
              CGL_DrawCenteredText(100,font, "%s: %d van %s score %s",myoutput,getal,myoutput1,myoutput2);
              int key,keytrig;
              CGL_GetKeys(&amp;key,&amp;keytrig);
              if (keytrig &amp; CGL_INPUT_KEY_EXIT) exit(EXIT_SUCCESS);
              CGL_SwapBuffers(); 
          } while(gameactive);
          CGL_FlushGraphics();
  
      } while(1);
      CGL_CloseVideo();
  }
  </code></pre>