如何从applet连接到SQL数据库

我正在创建一个连接到存储在同一Web服务器上的数据库的applet,当我在Eclipse中的applet viewer中测试代码时,它会连接。 问题是,当我尝试在Eclipse外部(在Web服务器上或本地)运行它时,我收到此错误:

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.(StringUtils.java:70) ... 39 more 

为了从applet连接到SQL数据库,是否需要执行任何特殊操作? 以下是处理连接的代码:

 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(); } } 

我在这里更改了用户名,密码和数据库,但在我的代码中它们是正确的。

编辑

由于连接到SQL服务器不起作用(我想它是托管在不同的主机上),我最终改变了访问数据库的方式。 applet打开与主机上的PHP文件的连接,该文件又连接到数据库。 无论哪种方式,这都更安全。

这是由applet的安全模型引起的。 这是一篇很好的文章,总结了围绕这个问题的两种主要方式 – 签署applet或使用策略文件。 如果您在尝试以下方法之后仍然遇到问题,请告诉我们:

http://www.coderanch.com/how-to/java/HowCanAnAppletReadFilesOnTheLocalFileSystem

您需要签署您的小程序。 这是一些关于如何的好文档。