java lang classnotfoundexception com mysql jdbc driver

0 votes

I am facing an error with my code while debugging it. I am trying to write the code for connecting a singleton class with the database mysql. I am new to Java and need some assistance on what the error is. Under this line is the code which I used:-

package com.glomindz.mercuri.util; 
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException; 


public class MySingleTon { 
String url = "jdbc:mysql://localhost:3306/"; 
String dbName = "test"; 
String driver = "com.mysql.jdbc.Driver"; 
String userName = "root"; 
String password = ""; 

private static MySingleTon myObj;
private Connection Con ;
private MySingleTon() { 

    System.out.println("Hello"); 
    Con= createConnection(); 

} 

@SuppressWarnings("rawtypes") 
public Connection createConnection() { 
          Connection connection = null; 
          try { 

// Load the JDBC driver 
Class driver_class = Class.forName(driver); 
Driver driver = (Driver) driver_class.newInstance(); 
DriverManager.registerDriver(driver); 
connection = DriverManager.getConnection(url + dbName);
} catch (ClassNotFoundException e) { e.printStackTrace(); 
} catch (SQLException e) { e.printStackTrace(); 
} catch (IllegalAccessException e) { e.printStackTrace();
} catch (InstantiationException e) { e.printStackTrace(); 
} 
return connection; 
} 
/** 
* Create a static method to get instance. 
*/ public static MySingleTon getInstance() { 
          if (myObj == null) { 
              myObj = new MySingleTon(); 
}
return myObj;
}
public static void main(String a[]) {

      MySingleTon st = MySingleTon.getInstance(); 
  } 
}
Feb 8 in Java by Rahul
• 8,980 points
467 views

1 answer to this question.

0 votes

I assume that your mysql connectivity library is not included in the project you are in. In order to rectify the error you are facing, try to use either the Maven Projects Solution or the All Projects Solution. In the former, add the mysql-connector dependency to the given project file pom.xml which is displayed below:-

<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency>

The latter, add the jar library to the given project manually and follow the steps:-

Right Click the project -- > build path -- > configure build path

In the Libraries Tab press Add External Jar and Select your jar. As the file from the MYSQL connectivity library is not found, Java will give an exception while you build your project. In other words, to conclude is that once you add the library to the project, then Java will be able to locate the driver com.mysql.jdbc.Driver.

answered Feb 8 by Soham
• 8,730 points

Related Questions In Java

0 votes
0 answers

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

I am getting this exception when I ...READ MORE

May 7 in Java by narikkadan
• 7,860 points
27 views
+1 vote
14 answers

Communications link failure: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException

Your exception seems to indicate that your ...READ MORE

answered Dec 11, 2020 in Java by Roshni
• 10,500 points
164,881 views
0 votes
1 answer

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: How to handle this exception?

You have a com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure java.net.ConnectException: ...READ MORE

answered Oct 8, 2018 in Java by Parth
• 4,630 points
1,841 views
0 votes
0 answers

Getting java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory exception

I am getting this exception when I ...READ MORE

13 hours ago in Java by Kichu
• 15,810 points
5 views
0 votes
0 answers
0 votes
1 answer

How to retrieve column names from java.sql.ResultSet?

You may refer the below code: ResultSet ...READ MORE

answered Jul 4, 2018 in Java by sophia
• 1,400 points
1,673 views
0 votes
1 answer

Establish JDBC Connection in R through single sign-on

You can certainly connect to databases with ...READ MORE

answered Sep 12, 2018 in AWS by Priyaj
• 58,060 points
1,291 views
0 votes
1 answer

Find Oracle JDBC driver in Maven repository

Download the jar and place it in ...READ MORE

answered Sep 26, 2018 in Java by Daisy
• 8,120 points
2,044 views
0 votes
3 answers

Change date format in a Java string

The reason for the inaccuracy is because ...READ MORE

answered Feb 9 in Java by Soham
• 8,730 points
1,091 views
0 votes
2 answers

What is immutable string in java?

To answer your question, the String is ...READ MORE

answered Feb 9 in Java by Soham
• 8,730 points
215 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP