You don't really seem to grasp the technologies you're using, or how they work.
In the comments you state:
My website's client machine is also a localhost for the codes that run inside it uploaded via ftp like the logic in PHP: you write localhost and upload to the server, then it connects
That PHP is running on the webserver. That also happens to be the same host as your mysql server. So, yes localhost
is correct in PHP. Your web browser on your computer at home connects to the webserver via TCP, the PHP is executed on the webserver, connects to the mysql server on the same host and all is well.
Java applets run in your browser on your machine. The applet is downloaded from the website to your machine at home, and it runs on that machine. The way your code is written in your question, it then attempts to connect to a mysql server on your machine at home. localhost
is your computer.
In order for that applet to connect to the mysql server at your hosting company, it would need to attempt to connect to that host. In addition, the ports would need to be open to/on that server allowing a remote connection to the mysql server, mysql needs to be listening for connection on an external (public) IP address, and you would need to grant privileges for that user to be able to connect from a remote host.
It is probably worth mentioning that in general this is a very bad idea. Rarely is exposing your database to the world a good idea; one small mistake in granting access creates a very large problem. There are numerous other security issues. Do people do it? Sure. Still doesn't make it a good idea.
Typically an applet that is designed for public consumption is going to use an intermediary service (such as a web API or even RMI) to give you a fighting chance to lock things down.