test
test

Reputation: 18198

Java JDBC Connection Lost after actionPerformed in Applet

If I run this code: http://www.danny92.pastebin.com/m1f84b972

You will see that my Database connection connects then disconnects after actionPerformed.... why? :(

Upvotes: 0

Views: 710

Answers (3)

nos
nos

Reputation: 229128

Line 39 has this code:

  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jgame", props);

Thus you only assign the Connection to a local variable, not the con member variable in your applet.

Replace it with

  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jgame", props);

Upvotes: 0

Buhake Sindi
Buhake Sindi

Reputation: 89169

Applets have many resctrictions including network restrictions. Don't forget that applets run from the client side and not server side, therefore as a policy, applet was restricted to access company internal networks (the private networks)...

In short, your code is trying to access your database server (as it never connects because of the network restrictions placed on applets). It's trying to call a private network from a client side. Javascript follows the same restriction as it must never access a private network from client side.

More info here (http://www.wutka.com/hackingjava/ch3.htm)

Upvotes: 0

duffymo
duffymo

Reputation: 308813

I wouldn't recommend that an applet connect directly to a database. This exposes the database directly on the network - not a good practice.

A better idea might be to put a servlet in between the applet and the database. This will have several beneficial effects:

  1. Servlet can manage security
  2. Servlet engine can use a connection pool
  3. Servlet can handle several simultaneous connections at once for better scaling

Upvotes: 4

Related Questions