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

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

问题描述

我正在制作一个连接到存储在同一 Web 服务器上的数据库的小程序,当我在 Eclipse 的小程序查看器中测试代码时,它连接.问题是,当我尝试在 Eclipse 之外(在 Web 服务器上或本地)运行它时,我收到此错误:

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 数据库,需要做些什么特别的事情吗?这是处理连接的代码:

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

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

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.

推荐答案

这是由小程序的安全模型引起的.这是一篇很好的文章,总结了解决此问题的两种主要方法 - 对小程序进行签名或使用策略文件.如果您在尝试其中之一后仍然遇到问题,请告诉我们:

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.coderanch.com/how-to/java/HowCanAnAppletReadFilesOnTheLocalFileSystem

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

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