gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:56:38 26 4
gpt4 key购买 nike

我正在制作一个连接到存储在同一 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 服务器不起作用(我想它托管在不同的主机上),我最终改变了访问数据库的方式。该小程序打开与主机上 PHP 文件的连接,该文件又连接到数据库。无论哪种方式,这都更安全。

最佳答案

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

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

关于java - 如何从小程序连接到SQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8336335/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com