To answer your question, is this the code you’re using to connect to the online database? Its definitely not located on localhost. Also ensure that the port you’re supplying is correct. MySQL usually works over 3306.
Now, for the tough love (don’t be offended if you already know this). Never do this. Distributing an application [to the public] that connects directly to a database, at all, is a huge security risk, let alone that you’re giving the root credentials out as well.
To do this with some protection, build a simple application residing on the server that acts as the in-between for the game application and the data that you’re storing in the database. This can be in Java, PHP, Ruby, whatever suits you. The way it would work is your game sends a command to the server, the server figures out what to do, and the server puts the information in the database. This way, your MySQL credentials are stored in the server software where they can’t be accessed by the game.
I hope you take this into some consideration