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

Jason Rogers
  • 19,194
  • 27
  • 79
  • 112
  • 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? – Thomas Owens Jan 19 '11 at 16:26
  • 1
    you need add Exception stackTrace as well. Which class is not found? – fmucar Jan 19 '11 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. – BalusC Jan 19 '11 at 16:30
  • added the jar to the buildpath already. and the exception is thrown by Class.forName("com.mysql.jdbc.Driver").newInstance(); – Jason Rogers Jan 19 '11 at 16:42
  • That's for sure, ClassNotFound can be effectively thrown only by doing Class.forName [or ClassLoader.loadClass], so you dont have mysql driver in the classpath, like most of the answers suggest. – bestsss Jan 19 '11 at 17:00
  • @Jason Rogers which buildpath ? Are you using Netbeans, Eclipse, something else ? Sounds like a "buildpath" would be only for building, you'd need something similar when you run your app. – nos Jan 19 '11 at 17:33
  • 1
    Thanks for your help, the problem was that I hadn't placed the jar in the WEB-INF/lib folder. – Jason Rogers Jan 20 '11 at 01:17

3 Answers3

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

  • 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 – Jason Rogers Jan 20 '11 at 01:16
  • 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. – BalusC Jan 20 '11 at 01:20
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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
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.

Costis Aivalis
  • 13,680
  • 3
  • 46
  • 47