BeatBox
BeatBox

Reputation: 5

MySQL jdbc driver and Eclipse problems

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

Upvotes: 0

Views: 3658

Answers (3)

Rakesh Patel
Rakesh Patel

Reputation: 383

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.

Upvotes: 0

Chandra Sekhar
Chandra Sekhar

Reputation: 19500

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");

Upvotes: 2

Perception
Perception

Reputation: 80633

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).

Upvotes: 1

Related Questions