如何连接到从applet的SQL数据库 [英] How to connect to SQL database from applet

查看:141
本文介绍了如何连接到从applet的SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在连接到存储在同一个Web服务器上的数据库的小程序,当我在Eclipse中的小程序查看器测试code,它连接。问题是,当我尝试的Eclipse之外运行它(网络服务器或本地的),我得到这个错误:

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:

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

有什么特别需要为了从一个小程序连接到SQL数据库做?
这里是code处理的连接:

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:

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正确。

I changed the username, password and database here, but in my code they are correct.

修改

由于连接到SQL服务器没有工作(我想它托管在其他主机上),我已经结束了改变我访问数据库的方式。小程序打开主机,这反过来又连接到数据库上的PHP文件的连接。无论哪种方式,这是更安全的。

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.

推荐答案

这是由用于小应用程序的安全模型造成的。这里有一个很好的文章总结了解决此问题的方式主要有两种 - 要么签署applet或使用策略文件。让我们知道,如果你还在试图其中之一后遇到的问题:

This is being caused by the security model for applets. Here's a good article summarizing the two main ways around this issue - either sign the applet or use a policy file. Let us know if you still experience problems after trying one of these:

http://www.$c$cranch.com/how-to/java/HowCanAnAppletReadFilesOnTheLocalFileSystem

这篇关于如何连接到从applet的SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆