title: How to connect to SQL database from applet
body: <p>I am making an applet that connects to a database that is stored on the same web server, and when I test the code in the applet viewer in Eclipse, it connects. Problem is, when I try to run it outside Eclipse (on web server or locally), I get this error:</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-2" java.lang.ExceptionInInitializerError
  at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:286)
  at java.sql.DriverManager.getConnection(Unknown Source)
  at java.sql.DriverManager.getConnection(Unknown Source)
  at com.j_ctrl.MovePanel.connectDB(MovePanel.java:569)
  at com.j_ctrl.MovePanel.showHighScore(MovePanel.java:558)
  at com.j_ctrl.MovePanel.enterPress(MovePanel.java:544)
  at com.j_ctrl.MovePanel$1.keyPressed(MovePanel.java:163)
  at java.awt.Component.processKeyEvent(Unknown Source)
  at java.awt.Component.processEvent(Unknown Source)
  at java.awt.Container.processEvent(Unknown Source)
  at java.awt.Component.dispatchEventImpl(Unknown Source)
  at java.awt.Container.dispatchEventImpl(Unknown Source)
  at java.awt.Component.dispatchEvent(Unknown Source)
  at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
  at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
  at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
  at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
  at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
  at java.awt.Component.dispatchEventImpl(Unknown Source)
  at java.awt.Container.dispatchEventImpl(Unknown Source)
  at java.awt.Component.dispatchEvent(Unknown Source)
  at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
  at java.awt.EventQueue.access$000(Unknown Source)
  at java.awt.EventQueue$1.run(Unknown Source)
  at java.awt.EventQueue$1.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
  at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
  at java.awt.EventQueue$2.run(Unknown Source)
  at java.awt.EventQueue$2.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
  at java.awt.EventQueue.dispatchEvent(Unknown Source)
  at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
  at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
  at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  at java.awt.EventDispatchThread.run(Unknown Source)
  
  Caused by: java.security.AccessControlException: access denied       (java.util.PropertyPermission file.encoding read)
  at java.security.AccessControlContext.checkPermission(Unknown Source)
  at java.security.AccessController.checkPermission(Unknown Source)
  at java.lang.SecurityManager.checkPermission(Unknown Source)
  at java.lang.SecurityManager.checkPropertyAccess(Unknown Source)
  at java.lang.System.getProperty(Unknown Source)
  at com.mysql.jdbc.StringUtils.&lt;clinit&gt;(StringUtils.java:70)
  ... 39 more
  </code></pre>
  
  <p>Is there anything special that needs to be done in order to connect to an SQL database from an applet?
  Here is the code that handles the connection:</p>
  
  <pre><code>private void connectDB(){
      try{
          String driverName = "com.mysql.jdbc.Driver";
          Class.forName(driverName);
          String url = "jdbc:mysql://localhost/database";
          String user = "user";
          String pass = "pass";
          connection = DriverManager.getConnection(url, user, pass);
          System.out.println("Connected");
      }catch(Exception ex){
          ex.printStackTrace();
      }
  }
  </code></pre>
  
  <p>I changed the username, password and database here, but in my code they are correct.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Since connecting to the SQL server didn't work (I imagine it was hosted on a different host), I have ended up changing the way I access the database. The applet opens a connection to a  PHP file on the host, which in turn connects to the database. Either way this is more secure. </p>