MySQL ExceptionInInitializerError Java JDBC on Sta

2019-08-17 03:38发布

问题:

I am making a simple applet that uses an SQL Database.
When I use this applet locally on Netbeans it runs fine without a problem, yet when I deploy it to the Web I am getting a weird error.
I am using Java 1.6 locally, and the server I deploy on is running Java 1.6
I am using J2BC 5.1.22.
This is the error I get:

java.lang.ExceptionInInitializerError
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:315)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at FinalProject.DBConnect.<init>(DBConnect.java:29)
at FinalProject.TriviaApplet.init(TriviaApplet.java:61)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.init(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.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)

The code I use to initialize the driver is:

        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection(connectionURL, "db", "pw");

Does any one have any insight as to why this is occuring?

To add, my Applet gets all the information from another class. ^ The above is from DBConnect Class. An instance of this class is created, and this class does all the DB work. It was a requirement for this to be done as an applet. I have signed both my jar, and the lib jar containing the JDBC driver.

回答1:

it is due to the web security problem. how your JDBC driver is deployed to the client? do you give them enought permission? in fact, using JDBC in applet to visit the database in value-less.

  1. it requires all the client must have JRE installed and JDBC driver in CLASSPATH, you can try to package the JDBC driver with your applet in a same JAR.

  2. it need the policy modification by grant the socket connection in every local JRE.

permission java.net.SocketPermission "10.6.1.16:1521", "connect, resolve";

that make this solution nearly useless..