ClassNotFoundException com.mysql.jdbc.Driver myeclipse indigo Resolved

Open myeclipse indigo
Create New Java Project.
Add a Class file and write the code shown below for connecting to mysql
Download mysql-connector-java-5.1.21.jar file and add it to myeclipse Run Configurations
Keep the jar file in CLASSPATH environment variable
Include in Java build path also by adding External Jar.

After doing all the above steps I see the below error message. Checked lot of forums and searched google for more than 2 days, every step that I did was correct and nothing looks wrong. 
But still I can't connect to mysql.

Error Messages : 
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at$ Source)
at Method)
at 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 database.Connect.ConnectDB(

Error Message : 
No Suitable Driver for jdbc:mysql://localhost:3306/test

Program written in myeclipse indigo IDE
  1. package database;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. public class Connect {
  5. public Connection ConnectDB()
  6. {
  7. Connection conn = null;
  8. try
  9. {
  10. String userName = "root";
  11. String password = "planet";
  12. String url = "jdbc:mysql://localhost:3306/test";
  13. Class.forName ("com.mysql.jdbc.Driver").newInstance();
  14. conn = DriverManager.getConnection (url, userName, password);
  15. System.out.println ("Database connection established");
  16. }
  17. catch (Exception e)
  18. {
  19. e.printStackTrace();
  20. }
  21. return (conn);
  22. }
  23. }

Resolution :

After checking lot of articles on the internet, I couldn't figure out what is going wrong in my eclipse.
The mistake I did was, I downloaded file from mysql downloads section and setting the PATH to this file and added the same zip file to the external JAR.

To solve my problem I extracted the ZIP file which internally contains mysql-connector-java-5.1.21.jar file. Once we have the real jar file we should use this file every where. This solved my problem.

Hope this helps anyone out there..  If anyone finds this useful please leave a comment

No comments: