Search code examples
javaswingappletjappletsecurityexception

Connect to SQL with JApplet


I am attempting to connect to an SQL database using a JApplet. However, I get a SecurityException:

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: java.security.AccessControlException: access denied ("java.net.SocketPermission" "162.243.229.150:3306" "connect,resolve")

STACKTRACE:

java.net.SocketException: java.security.AccessControlException: access denied ("java.net.SocketPermission" "162.243.229.150:3306" "connect,resolve")
    at com.mysql.jdbc.StandardSocketFactory.unwrapExceptionToProperClassAndThrowIt(StandardSocketFactory.java:407)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:268)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at me.nrubin29.cubesorter.MySQL.setup(MySQL.java:24)
    at me.nrubin29.cubesorter.MySQL.access$100(MySQL.java:8)
    at me.nrubin29.cubesorter.MySQL$1.run(MySQL.java:35)
    at java.lang.Thread.run(Thread.java:744)


** END NESTED EXCEPTION **



Last packet sent to the server was 1 ms ago.
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2847)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:270)
    at me.nrubin29.cubesorter.MySQL.setup(MySQL.java:24)
    at me.nrubin29.cubesorter.MySQL.access$100(MySQL.java:8)
    at me.nrubin29.cubesorter.MySQL$1.run(MySQL.java:35)
    at java.lang.Thread.run(Thread.java:744)
Exception in thread "Thread-38" java.lang.NullPointerException
    at me.nrubin29.cubesorter.MySQL$1.run(MySQL.java:38)
    at java.lang.Thread.run(Thread.java:744)

I have tried using AccessController#doPrivileged, but that didn't help. How do I grant permission to use a socket with my applet?


Solution

  • Unsigned Applets have a number of restrictions applied to them. One of the restrictions is that they can't open network connections to hosts other than the one hosting the applet.

    There are a few ways around this:

    1. Sign the applet. The obvious problem with this is that you need an application signing certificate.
    2. Host the DB server on the same machine as the applet.
    3. Write a WebStart application instead (although it probably has the same problem).