2

I can't seem to get a connection made on my Java program to java, I have started the MySQL Server, made a database in phpMyAdmin. But I'm confused on how I should use the JDBC Driver that I downloaded from MySQL.

Here's my code:

    private void startConnection()
{
Connection conn = null;
String url = "jdbc:mysql://localhost/";
String dbName = "bank";
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 =(");
}
}

I had included the jar file in my JDK - jre\lib\ext folder but nothing. Any ideas?

Thanks in advance.

8
  • 3
    What is the console output of this code? Commented Nov 13, 2010 at 13:22
  • 1
    What is the error you get? Code looks good to me. To get the exact error, use e.printStackTrace(); in your catch clause. Commented Nov 13, 2010 at 13:23
  • In your exception handler, please print out the stack trace or at least e.getMessage()! Commented Nov 13, 2010 at 13:24
  • I have put the stacktrace in a paste bin: pastebin.com/gUGy2u1z Commented Nov 13, 2010 at 13:29
  • @Sandeep Bansal: Did u included that com.mysql.jdbc.Driver in your Build path Commented Nov 13, 2010 at 13:32

3 Answers 3

3

One thing stands out: you haven't specified a network port in the URL. The default port is 3306. So try:

jdbc:mysql://localhost:3306/

For the URL.

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

4 Comments

tried it with the port number, still didn't work. I have posted the stack trace in the comment above.
2 things: firstly, check that the jar file you have for the MySQL driver is in the classpath or in the jre\lib\ext folder. Secondly, have a peek inside the jar file (with 'jar tf', for example) and check if there is 'com/mysql/jdbc/Driver.class' in there.
Also, if your Java application hasn't been restarted since you added the MySQL driver jar to the class path, you'll need to restart it.
Although it isn't the full answer, as it was just the case of adding the jar file in the IDE. Yours is the answer more closer so you get it.
1

You have to specify the port. It's String url = "jdbc:mysql://localhost:3306/"; by default.

Comments

0
private void startConnection()
{
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "bank";
    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 =(");
    }
}

This will work

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.