1

I took this example from Oracle's website and I am not able to implement it. It keeps on showing an error. I have already added the mysql 5.1 driver jar to my projects lib folder and also to my build path. How do I fix my code ?

link - http://docs.oracle.com/javase/tutorial/jdbc/basics/connecting.html

error -

java.sql.SQLException: No suitable driver found for jdbc:JavaEE://localHost:3306/
    at java.sql.DriverManager.getConnection(DriverManager.java:602)
    at java.sql.DriverManager.getConnection(DriverManager.java:154)
    at com.beans.us.dao.Data.getConnection(Data.java:25)
    at com.beans.us.dao.Data.main(Data.java:43)
Cannot connect to database

code -

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class Data {

    public static Connection getConnection() {

        Connection conn = null;
        Properties connectionProps = new Properties();
        String userName = "root";
        String password = "root";
        String dbms = "JavaEE";
        String serverName = "localHost";
        String portNumber = "3306";

        connectionProps.put("user", userName);
        connectionProps.put("password", password);

        try {

            conn = DriverManager.getConnection("jdbc:" + dbms + "://"
                    + serverName + ":" + portNumber + "/", connectionProps);
        } catch (SQLException sQLException) {
            sQLException.printStackTrace();
        }

        if (conn != null) {
            System.out.println("Connected to database");
        } else {
            System.out.println("Cannot connect to database");
        }

        return conn;
    }

    public static void main(String[] args) {

        getConnection();

    }

}
david blaine
  • 5,683
  • 12
  • 46
  • 55
  • what kind of JavaEE database? I haven't seen, the default db with java friends is derby –  Jul 02 '13 at 18:28

3 Answers3

6

Your connection string is:

"jdbc:JavaEE://..."

but with MySQL it should be

"jdbc:mysql://..."

Also, your forgot to load the driver:

Class.forName("com.mysql.jdbc.Driver");

It needs to be done before calling

conn = DriverManager.getConnection(...);
jlordo
  • 37,490
  • 6
  • 58
  • 83
  • Class.forName("com.mysql.jdbc.Driver"); is not needed here. Your answer wins. I thought that the dbms meant my db. Dumb error. – david blaine Jul 02 '13 at 18:42
1

It seems you have forgotten to load the driver .

Class.forName("com.mysql.jdbc.Driver");

The Class.forName() causes the ClassLoader to load the class into memory. JDBC driver classes contain a static initializer block that registers the driver with DriverManager for later reference.

Didn't notice this earlier , your connection string should be "jdbc:mysql://localhost:3306/dbname"

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • 3
    version 4 jdbc drivers no longer need that. – jtahlborn Jul 02 '13 at 18:17
  • @jtahlborn - yeah. I had that line in my code and it still failed. So I removed the line before posting the code on SO. Downvoted the answer. I recommend that the answer be downvoted to 0 points and no further. – david blaine Jul 02 '13 at 18:39
0

Your jdbc string is not properly formatted, take a look at the answers of this question:

What is the MySQL JDBC driver connection string?

Community
  • 1
  • 1
David Hofmann
  • 5,683
  • 12
  • 50
  • 78