Dennis
Dennis

Reputation: 4017

MySQL and JApplet (yes another one)

Before I begin I would like to mention that I have researched this thoroughly and have yet to find a solution which has worked for me (a good 2-3 days of research).

Currently using : WAMPServer Version 2.1 (Apache service disabled)
Eclipse-jee x64
javac 1.6.0_22
Windows 7 x64

The applet, webpage, and database are all residing on my local computer.

First and foremost my applet works without issues in the Eclipse IDE, however I am constantly recieving the following error when attempting to run it as applet.html with the following script:

<applet code="GUI.class"
    name="Some name goes here"
    archive="APTracker.jar"
    width="1000" height="700">
    Your browser is not Java enabled.
</applet>
  1. I have exported my class files using Eclipse IDE which has included the manifest into appletJar.jar.
  2. The Jar exported by Eclipse does NOT contain the mysql-connector library
  3. After assembling my class files I manually extracted the com and org files from the mysql-connector jar and input them into my appletJar.jar
  4. Following this I signed my applet jar (and confirmed it is signed) with a key which expires in 6 months.

After these steps I still receive the error message shown below.

I have tried replacing localhost with 127.0.0.1 which did not work. I have also tried placing the mysql-connector.jar in the jre, jdk, and root class files which showed no change.

    private final String DRIVER = "com.mysql.jdbc.Driver";
    private final String DATABASE_URL = "jdbc:mysql://localhost:3306/javadb";
    private final String USERNAME = "xxxxxx";
    private final String PASSWORD = "xxxxxx";

    private Connection connection = null;
    private PreparedStatement selectAllAirports = null;

    private ResultSet resultSet;
    private ResultSetMetaData metaData;

    /* Establish PreparedStatements */
    public ResultSetTableModel()
    {
        try
        {   
            //establish connection to database
            connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);

            //load driver class
            Class.forName( DRIVER );

            //create prepared statements
            selectAllAirports = connection.prepareStatement( "SELECT asciiname, latitude, longitude, elevation, timezone, country_code FROM geoname;" );
        }
        catch ( SQLException sqlException )
        {
            sqlException.printStackTrace();
            //System.exit(1);
        }
        catch ( ClassNotFoundException classNotFound )
        {
            System.out.println("ClassNotFoundException triggered.");
            classNotFound.printStackTrace();
        }
    }

This is the error message which I receive:

C:\Users\Mr.\Desktop\Applet>appletviewer applet.html
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/javadb
        at java.sql.DriverManager.getConnection(DriverManager.java:602)
        at java.sql.DriverManager.getConnection(DriverManager.java:185)
        at ResultSetTableModel.(ResultSetTableModel.java:38)
        at GUI.(GUI.java:20)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at java.lang.Class.newInstance0(Class.java:355)
        at java.lang.Class.newInstance(Class.java:308)
        at sun.applet.AppletPanel.createApplet(AppletPanel.java:785)
        at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
        at sun.applet.AppletPanel.run(AppletPanel.java:368)
        at java.lang.Thread.run(Thread.java:662)
java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
        at java.util.Vector.elementAt(Vector.java:427)
        at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColumnModel.java:277)
        at GUI.init(GUI.java:60)
        at sun.applet.AppletPanel.run(AppletPanel.java:424)
        at java.lang.Thread.run(Thread.java:662)

Upvotes: 1

Views: 619

Answers (4)

Jhonie D. Gumilao
Jhonie D. Gumilao

Reputation: 11

Things to consider are:

  1. Do you have mysql-connector-java-5.1.10.jar or any related connector in your machine? If none, ask it in google?
  2. If you have, paste the connector at C:\Program Files\Java\jdk...\jre\lib\ext directory.
  3. Edit the applet tag on archive as archive = APTracker.jar, mysql-connector-java-5.1.10.jar
  4. Paste again the mysql-connector-java-5.1.10.jar to where the files like .html, .class and the like are located.

Upvotes: 1

Joachim Sauer
Joachim Sauer

Reputation: 308101

Note that recent JDBC drivers don't need the Class.forName() call if the service loader method is supported.

The current Connector/J version does support that, but by including only the org and com directories of the JDBC driver you "broke" it: The service loader mechanism depends on files under the META-INF directory.

So with your setup you would indeed need the Class.forName() call. But in your code that code is after the attempt to get the connection, which won't do any good.

So do one of those:

  • Add the relevant service files from the JDBC driver (under META-INF/services) to your jar file (and get rid of the unnecessary Class.forName() call) or
  • Put the Class.forName() call before the DriverManager.getConnection() call.

Upvotes: 3

Matt McHenry
Matt McHenry

Reputation: 20919

Rather than trying to manually put the MySQL class files into your APTracker.jar, why not just include the MySQL jar on the applet's classpath?

I suspect there is something in the MySQL jar file's META-INF directory that you need - a ServiceLoader configuration file or some such.

Upvotes: 1

Talha Ahmed Khan
Talha Ahmed Khan

Reputation: 15453

I guess the MySQL JDBC jar is missing.

you can download that jar from: http://www.mysql.com/downloads/connector/j/

Upvotes: 0

Related Questions