1
votes

I'm trying to set up a connection between my applet and my mysql server using jdbc

I added the jar mysql-connector-java-5.1.14-bin.jar to the project

then I used this code

public void databaseTesting(){
        Connection con;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test", "root","");
            System.err.println("connected !");

            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM `test`.`test`;");
            while (rs.next()) {
                int A = rs.getInt("columnA");
                int B = rs.getInt("columnB");
                System.err.println("A "+A+" B "+B);
            }
        } catch (SQLException e) {
            System.err.println("failed to connect");
        } catch (InstantiationException e) {
            System.err.println("InstantiationException");
        } catch (IllegalAccessException e) {
            System.err.println("IllegalAccessException");
        } catch (ClassNotFoundException e) {
            System.err.println("ClassNotFoundException");
        }
    }

and for some reason I keep getting ClassNotFoundException.


edit

the jar is added to the build path and appears in the Referenced Libraries.

the exception is thrown by

Class.forName("com.mysql.jdbc.Driver").newInstance();

Anybody got an idea why ?

Thanks in advance jason

3
What do you mean by "added the jar mysql-connector-java-5.1.14-bin.jar to the project"? Did you add the JAR to the classpath or did you just drop it in the project? - Thomas Owens
you need add Exception stackTrace as well. Which class is not found? - fmucar
@faith: given the code in the try block, that can be only one possible missing class. Others (i.e. the ones present during compiletime) would rather have produced a NoClassDefFoundError during runtime if it were really missing. - BalusC
added the jar to the buildpath already. and the exception is thrown by Class.forName("com.mysql.jdbc.Driver").newInstance(); - Jason Rogers
Thanks for your help, the problem was that I hadn't placed the jar in the WEB-INF/lib folder. - Jason Rogers

3 Answers

3
votes

Do you run this application through eclipse? Is it a Dynamic web project, if so then try adding the jar file to the WEB-INF\lib folder

2
votes

Then the JAR is not in the runtime classpath.

Since you explicitly mentioned "project", I'll assume that you're using an IDE. You have to add the JAR file to the so-called Build Path (which represents both the compiletime and runtime classpath). In Eclipse for example, rightclick the JAR file you dropped in the project folder, choose Build Path > Add to Build Path and that should be it.

alt text

See also:


Update: If it keeps complaining, then it is still not in the runtime classpath. Either you did it wrong or the runtime environment didn't use this JAR. Did you run it as a Java Application or as a Java Applet? (even though it's a bad practice to do JDBC inside an applet). If you're actually running this as an applet, it has got to be in the runtime classpath of the applet as well. You can specify it in the archive attribute/parameter of the applet.

1
votes

Your code is OK! It will work when:

  1. Your mysql-connector-java-5*-bin.jar is in place.
  2. The login & password are correct.
  3. Your database exists
  4. Your table exists
  5. Your ColumnA and ColumnB are integers

    If you are using an IDE just add the jar to the Libraries.