因此,在 Tomcat 6.0.20 上运行的 Java Web Start 应用程序中,我一直遇到 MySQL Connector/J 驱动程序无法正确加载的问题。我已将 MySQL 连接器 JAR 文件复制到 Tomcat 的 lib 目录以及 webapps//WEB-INF 中的 lib 目录中。我还在 JNLP 文件中添加了对 JAR 文件的引用。经过一番研究,我发现我还需要在 context.xml 文件 ($CATALINA_HOME/conf) 中添加一个节点,这是我根据 Tomcat 6 语法完成的。以下是 XML 文件的内容:
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Resource name="jdbc/MySQLDB" auth="Container" driverClassName="com.mysql.jdbc.Driver"
maxActive="10" maxIdle="1" maxWait="500" type="javax.sql.DataSource"
url="jdbc:mysql://myServerName:3306/myDbName" username="myUserName"
password="myPassword>" />
</Context>
这是我用来访问数据库的代码:
Connection con = null;
try {
sb.append("\nAbout to register JDBC driver\n");
Driver driver=new com.mysql.jdbc.Driver();
DriverManager.registerDriver(driver);
sb.append("About to create new instance of mysql jdbc driver\n");
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException e) {
sb.append(e.getMessage());
e.printStackTrace();
} catch (IllegalAccessException e) {
sb.append(e.getMessage());
e.printStackTrace();
} catch (ClassNotFoundException e) {
sb.append(e.getMessage());
e.printStackTrace();
} catch (SQLException e) {
sb.append(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
sb.append(e.getMessage());
e.printStackTrace();
} // end try-catch
try {
sb.append("About to create connection using DriverManager\n");
con = DriverManager.getConnection("jdbc:mysql://myServerName:3306/myDbName","myUserName", "myPassword");
// Make sure that connection instance isn't null before creating statement and executing query.
if (con != null) {
sb.append("About to create SQL statement using con.createStatement\n");
Statement st = con.createStatement();
sb.append("About to create ResultSet by executing query");
ResultSet rs = st.executeQuery("SELECT * FROM catissue_user");
sb.append("About to loop through ResultSet\n");
while(rs.next()) {
sb.append(rs.getString(2));
} // end of while
sb.append("\nAbout to close ResultSet, Statement and connection\n");
rs.close();
st.close();
con.close();
} else {
sb.append("There was a problem creating the connection:\n");
} // end if
} catch(SQLException exception) {
sb.append(exception.getMessage());
exception.printStackTrace();
} catch(Exception exception) {
sb.append(exception.getMessage());
exception.printStackTrace();
} // end try-catch
当我从 Eclipse 或使用 ant 的 SSH 执行代码时,我得到的结果很好,所以我知道这不是连接 URL 的问题。但是,当我使用 Web Start(jnlp 文件)运行它时,当我单击按钮时,JFrame 甚至都不会打开。不过,我知道事件侦听器没有问题,因为代码可以正常执行其他两种方式。我发现在注册驱动程序时会发生异常,但如果我不包括这些行,我会收到一条错误消息“找不到合适的驱动程序”。关于我可以做些什么来解决这个问题的任何建议?