0

I am having problems loading the MySQL JDBC driver. I have tried everything mentioned here, but am still running into problems.

The error I get is this:

    Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at Main.main(Main.java:12)

My code is as follows:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Main {

   public static void main(String args[]) throws Exception {

      //accessing the driver
      Class.forName("com.mysql.jdbc.driver");

      // creating variable to pass the connection information into
      Connection dbcon = DriverManager.getConnection(
            "jdbc:mysql//localhost:3306/test", "root", "root");

      PreparedStatement statement = dbcon
            .prepareStatement("select * from names");

      ResultSet result = statement.executeQuery();

      while (result.next()) {
         System.out.println(result.getString(2));

      }
   }
}

screenshot

As you can see above I have placed the driver in the lib folder and I have also put it into the Apache tomcat lib folder, because that was suggested as well.

Thank you for any help you can give me. Andrew

Community
  • 1
  • 1
BeatBox
  • 5
  • 1
  • 2

3 Answers3

2

The class name inside the MySql connectorJ JAR file is Driver. so you need to change

Class.forName("com.mysql.jdbc.driver");

to

Class.forName("com.mysql.jdbc.Driver");
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
1

It tells you the problem on the first line of your stacktrace:

Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.driver

The driver name is com.mysql.jdbc.Driver (capital D).

Perception
  • 79,279
  • 19
  • 185
  • 195
  • thankyou i feel like a fool now, i knew the probelm was in the main but no idea it needed to be capital, iam sorry for such a simple question – BeatBox Mar 14 '12 at 11:59
  • Oh, this happens to everyone trust me. Good luck with the rest of the project. – Perception Mar 14 '12 at 12:00
0

problem in loading the mysql connection library.please check the proper mysql library is load successfully.because when library load successfully then the library package display all the external loaded library.

Rakesh Patel
  • 393
  • 2
  • 10