test
test

Reputation: 18198

Connecting Java Applet to MySQL DB - Communications link failure

I am trying to connect my Java Applet to a MySQL Database. I know that it works, because I can connect to it on localhost and it retreives a list of records just fine. But when I put it on the internet, it doesn't work.

Here is my applet: http://mystikrpg.com/play

It's signed, but I keep getting the following exception:

SQLException Get Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

How can this happen and what can I do to fix this problem?

Here's the applet source code: http://sodan.pastebin.com/jWKTgBSU

Upvotes: 1

Views: 4364

Answers (2)

BalusC
BalusC

Reputation: 1109865

To start, the following exception

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 
  Communications link failure

can have the following causes:

  1. IP address or hostname in JDBC URL is wrong.
  2. Hostname in JDBC URL is not recognized by local DNS server.
  3. Port number is missing or wrong in JDBC URL.
  4. DB server is down.
  5. DB server doesn't accept TCP/IP connections.
  6. DB server has run out of connections.
  7. Something in between Java and DB is blocking connections, e.g. a firewall or proxy.

Further, do you realize that an Java Applet physically runs at the client machine? I.e. the machine where the webbrowser runs. The client basically downloads the applet from the webserver and it get executed at client (local) machine. The JDBC connection string jdbc:mysql://localhost:3306 would try to connect to a MySQL database running at the very same machine as where the applet runs. You can't reasonably expect that every client on the world will run a MySQL DB, let alone with your database/table.

If all you want is to access the MySQL server hosted at the server machine (there where the webserver runs) and you want to let the applet communicate with it, then you really have to create and run a webservice at the webserver. Since you tagged this question with [PHP] as well, I guess that you're using PHP at the server side, in that case, just create a PHP script which executes the MySQL action accordingly based on the request parameters or pathinfo and returns the MySQL results as for example a JSON or XML string. In the Applet, you can use the Java SE builtin java.net.URLConnection or the more convenienced Apache HttpComponents Client to communicate with the webserver. E.g.

URLconnection connection = new URL(getCodeBase(), "script.php?action=getdetails&productid=1").openConnection();
InputStream response = connection.getInputStream();
// ...

In Java, you could process any JSON response using Google Gson any XML response using JAXP.


That said (and unrelated to the current problem), the JDBC driver name you're using is the deprecated org.gjt.mm.mysql.Driver. I strongly recommend to use the correct driver name which was introduced over a decade ago to replace the old driver name: com.mysql.jdbc.Driver. Further, the Class#newInstance() call after Class#forName() is also not needed. It was to workaround a bug in the old driver. Also see this explanation.

Upvotes: 5

Venkat
Venkat

Reputation: 2634

Applets did not have the sufficient privilege to access a file or DB, when it's remotely been accessed. That's the way JVM employs security via Sandboxing...

This is what I know about applet's privilege...

Upvotes: 0

Related Questions