所以我一直有問題,MySQL Connector/J驅動程序沒有正確加載在Tomcat 6.0.20上運行的Java Web Start應用程序。我已將MySQL連接器JAR文件複製到Tomcat的lib目錄以及webapps/WEB-INF中的lib目錄中。我還在JNLP文件中添加了對JAR文件的引用。經過一番研究後,我發現我還需要將一個節點添加到context.xml文件($ CATALINA_HOME/conf)中,我根據Tomcat 6語法完成了該節點。下面是XML文件的內容:MySQL連接器/ J問題
<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或者從SSH執行代碼,我得到的結果很好,所以我知道這不是連接URL的問題。但是,當我使用Web Start(jnlp文件)運行它時,當我單擊按鈕時,JFrame甚至不會打開。然而,我知道這對於事件監聽器來說不是問題,因爲代碼可以很好地執行另外兩種方式。我發現當驅動程序正在註冊時會發生異常,但如果我不包含這些行,我會收到錯誤消息,提示「找不到合適的驅動程序」。有什麼建議可以解決這個問題?