与 java applet 相关的 Class.forName("com.mysql.jdbc.Driver").newInstance() 中的 ClassNotFoundException
我创建了一个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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
马尼什,
在开发 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).