2011-12-01 53 views
1

連接到SQL數據庫,我正在連接到存儲在同一個Web服務器上的數據庫的小程序,當我在Eclipse的applet查看測試代碼,它連接。問題是,當我試圖在Eclipse外部運行它(網絡服務器或本地的),我得到這個錯誤:如何從applet的

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文件的連接,該文件又連接到數據庫。無論哪種方式,這是更安全的。

回答

1

這是正在由小應用程序的安全模型引起的。這裏有一個很好的文章總結了解決此問題的方式主要有兩種 - 要麼簽署applet或使用策略文件。讓我們知道,如果你還在試圖其中之一後遇到的問題:

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

+0

我沒有特別喜歡的政策文件提及到的新手,因爲它散發出真實世界的小程序完全不切實際的方式,但因爲Java牧場說明具有通道解釋下側,我決定讓它這次滑動。 –

+0

上說CodeRanch那和簽名Applet「只能進行網絡連接回從他們分別擔任主」,但使用我的簽名Applet我無法連接到主機。 – JCtrl

1

您需要登錄您的小程序。 Here是一些很好的文檔。

+1

很好的回答,儘管你可能想在甲骨文換出太陽鏈接[這一個](http://docs.oracle.com/javase/tutorial/deployment/jar/signing.html)。 –