English 中文(简体)
MySQL Connector/J Problem
原标题:

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("
About to register JDBC driver
");
        Driver driver=new com.mysql.jdbc.Driver();
        DriverManager.registerDriver(driver);

        sb.append("About to create new instance of mysql jdbc driver
");
        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
");

   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
");
     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
");

     while(rs.next()) {
       sb.append(rs.getString(2));
     } // end of while

     sb.append("
About to close ResultSet, Statement and connection
");
     rs.close();
     st.close();
     con.close();
   } else {
     sb.append("There was a problem creating the connection:
");
   } // 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?

问题回答

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.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签