John Ubonty
John Ubonty

Reputation: 671

Can't connect to a mysql DB with DriverManager

Ok I am trying to connect to a mysql db with the following code. I tried looking at documentation but I am just having no luck what so ever.

Here is the code:

public class TESTSQL {

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://SLibraryDev.db.996****.hostedresource.com";
static final String USER = "SLD";
static final String PASS = "F11!";

public static void main(String[] args) {

    Connection conn = null;
    Statement stmt = null;

    try {
        // STEP 2: Register JDBC driver
        Class.forName(JDBC_DRIVER);

        // STEP 3: Open a connection
        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);
        ...
        }

This is the error I am getting :

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)

When I comment out "Class.forName(JDBC_DRIVER)" and run it I get this error:

java.sql.SQLException: No suitable driver found for jdbc:mysql://SLibraryDev.db.996****.hostedresource.com
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)

What I thought I needed to do was to download and run this : http://dev.mysql.com/downloads/connector/j/ which I did but it seemed like something was wrong with this installer...It seemed like it never finished, like it cut out in the middle of the process. Anyone have any ideas on what I need to do.

Upvotes: 0

Views: 541

Answers (3)

Fathah Rehman P
Fathah Rehman P

Reputation: 8741

Download mysql-connector from following url http://nearbuy.googlecode.com/files/mysql-connector-java-5.1.13-bin.jar

and add it to your class path.

If you are using netbeans, to add this file to your project do followoing

Right click on your project>click libraries>click add JAR/Folder> then select downloaded jar file

Upvotes: 1

java.sql.SQLException: No suitable driver found for jdbc:mysql://SLibraryDev.db.996****.hostedresource.com
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)

clearly suggests that there is no driver in your class path

simply add the mysql driver jar file in to class path

Upvotes: 0

Renjith
Renjith

Reputation: 3274

You need to download the mysql jdbc driver jar and include it in your classpath

Upvotes: 0

Related Questions