Jason Rogers
Jason Rogers

Reputation: 19344

problem using JDBC to connect to mysql

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

Upvotes: 1

Views: 1939

Answers (3)

Costis Aivalis
Costis Aivalis

Reputation: 13728

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.

Upvotes: 1

BalusC
BalusC

Reputation: 1109532

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.

Upvotes: 2

Amila Domingo
Amila Domingo

Reputation: 86

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

Upvotes: 3

Related Questions