Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

What is wrong with the code there are lots of error while debugging. I am writing a code for a singleton class to connect with the database mysql.

Here is my code

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();
    }
}

I am new to java. Please help.

share|improve this question
    
Is the driver jar in your classpath ? – NINCOMPOOP Jul 5 '13 at 8:49
    
ensure that you have synchronized on your getInstance() – Siddharth Jul 5 '13 at 11:10
    
Possible duplicate of ClassNotFoundException com.mysql.jdbc.Driver – pathikrit Oct 23 at 21:39

13 Answers 13

It seems the mysql connectivity library is not included in the project. Solve the problem following one of the proposed solutions:

  • MAVEN PROJECTS SOLUTION

Add the mysql-connector dependency to the pom.xml project file:

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

Here you are all the versions: https://mvnrepository.com/artifact/mysql/mysql-connector-java

  • ALL PROJECTS SOLUTION

Add the jar library manually to the project.

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

In Libraries Tab press Add External Jar and Select your jar.

You can find zip for mysql-connector here

  • Explanation:

When building the project, java throws you an exception because a file (the com.mysql.jdbc.Driver class) from the mysql connectivity library is not found. The solution is adding the library to the project, and java will find the com.mysql.jdbc.Driver

share|improve this answer
4  
What if we are not using any IDE and simply running on our terminal ? Then how to deal with this problem ? – Achyuta Aich Mar 25 '15 at 17:00
2  
I am doing this. But, It is not working. – Hafiz Shehbaz Ali Dec 11 '15 at 13:35

If you got the error in your IDE(compile-time error), you need to add your mysql-connector jar file to your libs and add this to your referenced library of project too.

If you get this error when you are running it, then probably its because you have not included mysql-connector JAR file to your webserver's lib folder.

Add mysql-connector-java-5.1.25-bin.jar to your classpath and also to your webserver's lib directory. Tomcat lib path is given as an example Tomcat 6.0\lib

share|improve this answer
1  
What if we are not using any IDE and simply running on our terminal ? Then how to deal with this problem ? – Achyuta Aich Mar 25 '15 at 17:00
    
if you are not using any IDE then you should add jar file path to your classpath – Asad Jul 3 '15 at 4:12
    
It fixes my problem, thanks – lenhhoxung Dec 1 '15 at 11:39

For Gradle-based projects you need a dependency on MySQL Java Connector:

dependencies {
    compile 'mysql:mysql-connector-java:5.1.+'
}
share|improve this answer

You will have to include driver jar for MySQL MySQL Connector Jar in your classpath.

If you are using Eclipse: How to add dependent libraries in Eclipse

If you are using command line include the path to the driver jar using the -cp parameter of java.

java -cp C:\lib\* Main
share|improve this answer
    
What if we are not using any IDE and simply running on our terminal ? Then how to deal with this problem ? – Achyuta Aich Mar 25 '15 at 17:00

JDBC API mostly consists of interfaces which work independently of any database. A database specific driver is required for each database which implements the JDBC API.

First download the MySQL connector jar from www.mysql.com, then:

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

In the libraries tab press Add External Jar and select your jar.

share|improve this answer

check for jar(mysql-connector-java-bin) in you classpath download from here

share|improve this answer

In the project into the folder Libraries-->right click --> Add Library --> Mysqlconnector 5.1

share|improve this answer

For Maven based projects you need a dependency.

<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
</dependency>
share|improve this answer
2  
Why PostgreSQL? The question is about MySQL. – naXa Mar 11 '15 at 13:32
    
Edited the answer to match the question – tomtomssi Mar 2 at 5:53
    
it works for me, thanks – Beraliv Jun 4 at 13:27

It is because the WEB-INF folder does not exist at the location in the sub directory in the error. You either compile the application to use the WEB-INF folder under public_html OR copy the WEB-INF folder in sub folder as in the error above.

share|improve this answer

The driver connector is not in your build path. Configure the build path and point it to the 'mysql-connector-java-5.1.25-bin.jar' (check the version which you are using). Alternatively you can use maven :D

share|improve this answer

I Understood your problem add this dependency in your pom.xml your problem will be solved,

https://mvnrepository.com/artifact/mysql/mysql-connector-java/5.1.38

share|improve this answer

Trivial as it may seem in my case netbeans version maven project 7.2.1 was different. There is a folder in the project called dependencies. Right click and then it brings up a popup window where you can search for packages. In the query area put

mysql-connector

It will bring up the matches (it seems it does this against some repository). Double click then install.

share|improve this answer

The exception can also occur because of the class path not being defined.

After hours of research and literally going through hundreds of pages, the problem was that the class path of the library was not defined.

Set the class path as follows in your windows machine

set classpath=path\to\your\jdbc\jar\file;.
share|improve this answer

protected by Community Nov 23 '14 at 11:45

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.