与 java applet 相关的 Class.forName("com.mysql.jdbc.Driver").newInstance() 中的 ClassNotFoundException

发布于 2024-12-25 20:19:45 字数 1645 浏览 1 评论 0 原文

我创建了一个Applet,它连接到 mysql 数据库并在数据库中插入行。它正在本地主机上运行文件。但是当我在 php web 服务器上发布相同的内容时,找不到 mysql 连接 jar。我正在以 HTML 文件的形式从该服务器运行 Applet,并且收到 ClassNotFoundException 异常。我已经将 mysql-connector-java-5.1.12-bin.jar 放在 html 和其他类文件存在的同一位置。我还在 root 上创建了 web-inf/lib 并将 jar 放在那里,但没有运气。下面是代码。

Connection conn=null;
try {
    String url = "jdbc:mysql://<ipaddress of domain>:3306/db_name";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "username"; 
    String password = "password";
    try {
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url,userName,password);
        Statement statement = conn.createStatement();
        statement.executeUpdate("insert query");
        conn.close();
    } catch (SQLException e) {
        showAlert("Error=" + e.getMessage() + ",ErrorCode="+e.getErrorCode() + "," + e.getSQLState());
    }
} catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    showAlert("InstantiationException=" + e.getMessage());
} catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    showAlert("IllegalAccessException=" + e.getMessage());
} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    showAlert("ClassNotFoundException=" + e.getMessage());
}

以下是 HTML 文件代码

<HTML>
<BODY>
<APPLET CODE=app_test.class WIDTH=200 HEIGHT=100 archive='mysql-connector-java-5.1.12-bin.jar'>
</APPLET>
</BODY>
</HTML>

I created an Applet which connect to mysql database and insert rows in db. It is running file on localhost. But when I published the same on php web server, the mysql connect jar is not found. I am running the Applet from that server in the form of HTML file and I am getting exception of ClassNotFoundException. I have already put mysql-connector-java-5.1.12-bin.jar in the same place where html and other class files exist. Also I created web-inf/lib on root and put the jar there but no luck. Below is the code.

Connection conn=null;
try {
    String url = "jdbc:mysql://<ipaddress of domain>:3306/db_name";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "username"; 
    String password = "password";
    try {
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url,userName,password);
        Statement statement = conn.createStatement();
        statement.executeUpdate("insert query");
        conn.close();
    } catch (SQLException e) {
        showAlert("Error=" + e.getMessage() + ",ErrorCode="+e.getErrorCode() + "," + e.getSQLState());
    }
} catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    showAlert("InstantiationException=" + e.getMessage());
} catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    showAlert("IllegalAccessException=" + e.getMessage());
} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    showAlert("ClassNotFoundException=" + e.getMessage());
}

Below is the HTML file code

<HTML>
<BODY>
<APPLET CODE=app_test.class WIDTH=200 HEIGHT=100 archive='mysql-connector-java-5.1.12-bin.jar'>
</APPLET>
</BODY>
</HTML>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉服软 2025-01-01 20:19:45

马尼什,
在开发 Web 应用程序时必须应用一些称为 MVC 的概念。

Applet 永远不应该访问数据库,从而破坏 MVC 范例。

您的小程序至少应该与 servlet 交互,而 servlet 又与您的控制器(数据库代码)通信。

Maneesh,
There are concepts called MVC that must be applied when developping web applications.

Applets should never access a database thus breaking the MVC paradigm.

Your applet should for the least, interact with a servlet which in turns speak to your controller (Database code).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文