javamysqlappletmysql-connectorconnector-j

Connecting to a sql database from an applet on the server, do you use localhost or the actual ip


I am trying to connect to a database on my website from a java applet. When I try from eclipse, it says that there was a communications link failure and that the driver has not received any packets from the server. Is that because only applets on the actual server can connect to a database?

So I exported it as a jar and I personally signed it and uploaded it to my website. Then using as embed tag, I put it in my webpage.

<div id="blah">
<embed id="Math"
   type="application/x-java-applet"
   width="810" height="600" 
   archive="mathG.jar,mysql-connector-java-5.1.23-bin.jar"
   code="com.mathg.math.MathG"
   codebase="test"
   pluginspage="http://java.com/download/"/>

Is that the proper way to put the applet on the site? The applet shows up and it works on the webpage, but when I press a button to connect to the database, it says that it couldn't connect.

I tried using both localhost and the actual ip in my java code to try to connect, but neither works.


Solution

  • Putting the IP address of the database server is preferred; localhost should only be used for quick & dirty local tests. It's unlikely that you'll deploy an app or database on localhost.

    This is a very bad idea. No applet should have direct access to a database from the internet. All that data is exposed.

    A better idea is to put a servlet in-between the applet and database. Deploy the servlet by exposing it to the internet; put the database behind a firewall and only open its port to the servlet. Let the servlet handle authentication, validation, binding, and interacting with the database. Users will be much better off with this design.