The error is simple, it means you have stepped beyond or below the size that your array could possibly be, and either tried to read or write data to it. The error should give you an index as well as to which it was and approximate location - this is actually surprisingly helpful in determining where the error can be especially if you use multiple loops within your program.
In this particular instance, its because you have 0 possible entries in your a. MaxElem has not been defined as any particular value by this point, so by default its size is 0. Not sure how large you want that to be though...
Using arrays as stacks or queues is a bit of a pain since you need to resize them, move them, copy them, and do all sorts of things to them. I would suggest changing the String array into a collection such as an ArrayList<String> or a Vector<String> (the overhead is a trade off for simplicity). These lets you move things in and out without needing to resize them manually. For some extra experience, you may want to look at writing your own linked lists. They are about the easiest datastructure to write, and carry a lot of useful features (using a double linked list lets you move from the front and end of a linked list with a O(1) magnitude, removing / adding from middle of collection takes minimal effort). If you do that, consider extending the AbstractCollection class (or implementing the Iterator and Collection interfaces) to create an iterable object that can be used in things like a for each. Very handy.
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php