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, 2022 in Java by Rahul
• 9,680 points
8,161 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.

To know more about SQL, It's recommended to join Microsoft SQL Training today.

answered Feb 8, 2022 by Soham
• 9,710 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, 2022 in Java by narikkadan
• 63,600 points
2,441 views
+3 votes
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,480 points
249,480 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,640 points
2,688 views
0 votes
0 answers

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

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

May 18, 2022 in Java by Kichu
• 19,040 points
1,429 views
0 votes
1 answer

Java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse

Just follow these two steps: Step 1 Add This ...READ MORE

answered Apr 29, 2022 in Other DevOps Questions by narikkadan
• 63,600 points
2,706 views
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
2,864 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,020 points
2,478 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,140 points
3,278 views
0 votes
3 answers

Change date format in a Java string

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

answered Feb 9, 2022 in Java by Soham
• 9,710 points
3,359 views
0 votes
2 answers

What is immutable string in java?

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

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