1

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

0

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Dan: did you spot that the part java.net.URLConnection in my answer is blue and clickable? Anyway, here's the link again, hope that helps more: stackoverflow.com/questions/2793150/…
0

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...

3 Comments

Maybe it's this line: con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jgame", props);
Of course, yes bro.. It tries to establish connection with the localhost, which will be denied by the JVM sandbox.
If it were a security issue, then you would rather have gotten an AccessControlException before the code can even attempt to connect the DB. In this particular case, the attempt to connect has already taken place, but the DB simply cannot be located.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.