0

So I just started learning about databases this week and one of the things I need to be able to do is connect to my mySQL database that I created using Java. I have done some research and I have tried to find the correct way of doing this I just can't seem to figure out how. Here is my code:

 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.SQLException;
 public class Menu
 {
public void menu()
{
    Connection conn;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "gym";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "password";
    try 
    {
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url+dbName,userName,password);
        System.out.println("Connected to the database");
        conn.close();
        System.out.println("Disconnected from database");
    } 
    catch (Exception e) 
    {
        System.out.println("NO CONNECTION =(");
        System.out.println(e.getMessage());
    }
  }

}

Now the problem is, is that every time I run this code, the "No Connection =( " appears and then it says the error is: "com.mysql.jdbc.Driver". Can somebody please help me and say what I am doing wrong? Thank you. Much appreciated.

4
  • 1
    It means your library path doesn't contain the jar that contains the com.mysql.jdbc.Driver class. Commented Jul 31, 2014 at 20:58
  • So what should I change in my code? @Eran Commented Jul 31, 2014 at 21:00
  • 1
    You don't have to change anything in your code. If you are running it via Eclipse, you should add mysql-connector-java-x.x.x-bin.jar to your build path. (where x.x.x is the version of the jar) Commented Jul 31, 2014 at 21:03
  • 1
    @Eran I recommend you post your comment as an answer, as it solves this issue. Commented Jul 31, 2014 at 21:06

2 Answers 2

1

Your error means that your library path doesn't contain the jar that contains the com.mysql.jdbc.Driver class.

You don't have to change anything in your code. If you are running it via Eclipse, you should add mysql-connector-java-x.x.x-bin.jar to your build path (where x.x.x is the version of the jar).

Sign up to request clarification or add additional context in comments.

Comments

1

All JDBC connection classes need their respective drivers which are normally supplied as jar files from the database vendor, add the relevant database driver to your classpath.

The .jar file will be available from the vendors site, in this case : http://www.mysql.com/products/connector/ and then to add this to the classpath of your project in the ide of your choice. Here is a guide for eclipse : http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)

Try and run again once you have this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.