小编典典

如何从小程序连接到SQL数据库

sql

我正在制作一个连接到存储在同一Web服务器上的数据库的小程序,当我在Eclipse中的小程序查看器中测试代码时,它就连接了。问题是,当我尝试在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.<clinit>(StringUtils.java:70)
... 39 more

为了从小程序连接到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
Server无效(我想它托管在其他主机上),因此我最终改变了访问数据库的方式。该小程序将打开与主机上PHP文件的连接,该主机又将其连接到数据库。无论哪种方式,这都更安全。


阅读 291

收藏
2021-05-16

共1个答案

小编典典

这是由applet的安全模型引起的。这是一篇很好的文章,概述了解决此问题的两种主要方法-
签署applet或使用策略文件。让我们知道您尝试以下方法之一后是否仍然遇到问题:

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

2021-05-16