简体   繁体   中英

Issues adding external JDBC jar to applet

I am trying to make an applet that accesses a database and reads the table information. I don't care about any security risks whatsoever, just want this to work.

Whenever I run the applet in the appletviewer, it accesses the database just fine and does what I want it to do. However, when I try to add the .class to an HTML website, I get a class not found exception. I've looked around and nothing seems to be working. I have my .class, my JDBC .jar, and my .html all in a folder on a my desktop. Here is my HTML code:

<html>
<head>
<title>applet</title>
</head>

<body>
<applet code="testing.class" width="350" height="200" archive="mysql-connector-java-5.1.31-bin.jar"></applet>
</body>

</html>

My applet code (even though I'm sure it's correct):

import java.applet.Applet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class testing extends Applet{
    public void init() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String URL = "https://ca.news.yahoo.com/-a-kick-in-the-teeth---pc-supporters-respond-to-hudak-s-resignation-195516571.html";
            Connection con = DriverManager.getConnection("jdbc:mysql://BLOCKED/BLOCKED", "BLOCKED", "BLOCKED");
            PreparedStatement statement = con.prepareStatement("INSERT INTO voting (URL, Votes) VALUES ('" + URL + "', 2) ");
            statement.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

New error:

java.lang.ExceptionInInitializerError
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at testing.init(testing.java:11)
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 sun.plugin2.applet.AWTAppletSecurityManager.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:69)
... 7 more

OK, this exception is reasonable.

Your problem is in the beginning. You try to run applet that connects directly to JDBC. You noted that security issues are out of scope. Probably it is correct from your point of view but not from the point of view of your browser.

Applets are very limited in security perspective. They cannot use client's disk, cannot call JNI, cannot establish connection to server other than one that they started from.

In your case you run your page from file system, ie use URL like c:/myproject/mypage.html . This means that your "host" is something like "myproject". Then you try to perform JDBC connection to BLOCKED (i guess it is localhost . But localhost is not myproject . To solve this security problem try to deploy your applet under HTTP server (even if it is on your computer). Then try to access the page over HTTP.

As far as I remember you should also sign your applet. Probably you can configure your browser to run untrusted applets. In this case you can run it without signing.

I still do not know what was the problem in Chrome.

BUT: why are you doing all this? Applets is an obsolete technology. No-one uses it for last 15 years. If you just need some kind of java UI, implement java application. If you need some kind of easy distribution run it using java web start. Or alternatively implement ordinary web application, so client talks to server over HTTP and server performs all connections it needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM