views:

245

answers:

1

So I've been having issues with the MySQL Connector/J driver not correctly loading in a Java Web Start application that is running on Tomcat 6.0.20. I've copied the MySQL connector JAR file into the lib directory of Tomcat as well as the lib directory within webapps//WEB-INF. I also added a reference to the JAR file inside of the JNLP file. After researching a little I discovered that I also needed to add a node to the context.xml file ($CATALINA_HOME/conf), which I did according to the Tomcat 6 syntax. Below is the contents of the XML file:

<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>

And here is the code that I'm using to access the database:

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

When I execute the code from Eclipse or from SSH using ant, I get the results back just fine, so I know that it's not an issue with the connection URL. However when I run it using Web Start (jnlp file), the JFrame won't even open when I click the button. I know it's not a problem with the event listener, though, since the code works fine executing it the other two ways. I've figured out that the exception happens when the driver is being registered but if I don't include those lines, I get an error saying "No suitable driver found." Any suggestions as to what I can do to fix this problem?

+1  A: 

Your Java Web Start program runs on the client, but your JDBC driver is loaded by your Tomcat server - those are two different environments running in two different JVMs.

You say that you have a reference to your JDBC driver in your JNLP file - this is the part you need to look at. The jar file that contains your driver needs to be downloadable by the Web Start client through HTTP. I'm guessing that this is where the chain is broken.

That said, I wonder about the safety of letting your clients download a JDBC driver and connect to your database. Bytecode isn't terribly hard to reverse engineer, and jar files are just a special case of zip compression.

Christian Vest Hansen