1

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

7
  • 1
    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? Commented Jan 19, 2011 at 16:26
  • 1
    you need add Exception stackTrace as well. Which class is not found? Commented Jan 19, 2011 at 16:28
  • @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. Commented Jan 19, 2011 at 16:30
  • added the jar to the buildpath already. and the exception is thrown by Class.forName("com.mysql.jdbc.Driver").newInstance(); Commented Jan 19, 2011 at 16:42
  • 1
    Thanks for your help, the problem was that I hadn't placed the jar in the WEB-INF/lib folder. Commented Jan 20, 2011 at 1:17

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

I give you the green mark because it was effectively to problem I had, I didn't find any mention of WEB-INF/lib in the tutorials I read about the
You also didn't mention that you was running this piece of code in a servletcontainer as runtime environment. For future questions, it's pretty important to mention the actual runtime environment used. The /WEB-INF/lib as container for 3rd party JAR's which are to be part of the runtime environment is however enough mentioned in the average JSP/Servlet tutorials.
2

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.