954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

MySQL drivers not loading?

Using the following:

protected void testDriver ( )  {
    String drivers = "nada";
    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch ( java.lang.ClassNotFoundException e ) {
      System.out.println("MySQL JDBC Driver not found ... ");
    } catch (IllegalAccessException ex) {
      System.out.println("Illegal Access");
    } catch (InstantiationException ex) {
      System.out.println("Instantiation problem");
    }
    System.out.println("Midway drivers are " + drivers);
    try {
      drivers = (String) java.security.AccessController.doPrivileged(
          new sun.security.action.GetPropertyAction("jdbc.drivers"));
    } catch (Exception ex) {
          drivers = "balderdash";
    }
    System.out.println(drivers);
  }


I get the output
"Midway drivers are nada"
"null"

This tells me that java.security.AccessController.doPrivileged isn't quite doing what I expected. Why isn't it picking up a driver? Is it the proper usage? How /do/ you load a mySQL driver, anyways?
Other than the printlns, this is about the same as the mySQL code I had seen other places.
Thanks,

MonkeyCode
Newbie Poster
20 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Where are you making a connection to the DB?

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 
Where are you making a connection to the DB?

Hello Wizard!
This fellow wrote a two-step connection process; the first to just fire the Class.forName with the assumption that the driver was good, and then the next to see whether the user-input database host name was any good.
That didn't quite make sense to me either, so I just rewrote it:

public int DBtest( String hst ) {

    host = hst;
    String url = "";

//    testDriver ( );
    String drivers = "nada";
    try {
      Class.forName("com.mysql.jdbc.Driver");//.newInstance()
    } catch ( java.lang.ClassNotFoundException e ) {
      System.out.println("MySQL JDBC Driver not found ... ");
    }
/*    System.out.println("Midway drivers are :" + drivers);
    try {
      drivers = (String) java.security.AccessController.doPrivileged(
          new sun.security.action.GetPropertyAction("jdbc.Driver"));
    } catch (Exception ex) {
          drivers = "balderdash";
    }
    if (drivers == null) System.out.println("It really is NULL");
    System.out.println("Ending drivers are :" + drivers);
*/
    try {
      //url = "jdbc:mysql://" + host + "/" + database + "?user=" + username + "&password=" + password;
      url = "jdbc:mysql://" + host + "/" + database;
      Connection con = DriverManager.getConnection(url, "username", "password");
      con.close();
      return 1;
    } catch ( java.sql.SQLException e ) {
      System.out.println(e);
      return 0;
    }
  }

For whatever reason, GetPropertyAction really was returning NULL, even though Class.forName() isn't throwing the "Not Found" error. Anyway, the only output now is, "java.sql.SQLException: No suitable driver". It's possible that MySQL has changed drastically since the previous guy was programming on this, but I was hoping the same drivers were still valid.

MonkeyCode
Newbie Poster
20 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

JDBC drivers are usually dependent on specific versions of the database engine in use.
Find out what version of mySQL you're using and what driver you need for it.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
JDBC drivers are usually dependent on specific versions of the database engine in use. Find out what version of mySQL you're using and what driver you need for it.


All right... I just downloaded MySQL 5, and the project's running on MySQL 4.0.14, and they both claim to be able to use driver 3.51. Unfortunately, after installing the 3.51 driver, the program is still saying, "No suitable driver."

Also tried adding

static {
    System.loadLibrary("msvcr71");
    System.loadLibrary("myodbc3");
    System.loadLibrary("myodbc3S");
  }


, which corresponds with the three dlls that came with the 3.51 version, but still no dice. Strangely, I couldn't find a "driver.class" or "driver.java" file in the downloads.

MonkeyCode
Newbie Poster
20 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Okay, a rundown:
Running in JBuilder 2005
Using the following:

try {
      java.sql.Driver mySQLDriver = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance();
      DriverManager.registerDriver(mySQLDriver);
    }catch(Exception e){
      e.printStackTrace();
    }
    java.util.Enumeration driverlist = DriverManager.getDrivers();
    if(!driverlist.hasMoreElements()){
      System.out.println("If you see this than something is really screwed on this system...");
    }

    try {
      Object Instants = Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch ( java.lang.ClassNotFoundException e ) {
      System.out.println("MySQL JDBC Driver not found ... ");
    } catch (IllegalAccessException ex) {
      System.out.println("Illegal Access");
    } catch (InstantiationException ex) {
      System.out.println("Instantiation problem");
    }
    java.util.Enumeration driverlist2 = DriverManager.getDrivers();
    Object ob = null;
    if(driverlist2.hasMoreElements()) System.out.println("list2 isn't empty");
    else System.out.println("list2 is empty");
    for (; driverlist2.hasMoreElements(); ){
      ob = driverlist2.nextElement();
      System.out.println(ob);
      System.out.println("Filler");
    }
    try{
      Connection c = DriverManager.getConnection("jdbc:mysql://ycb","root","R00T");
      c.close();
    } catch ( java.sql.SQLException e) {
      System.out.println("The exception is " + e);
    }

I get the following output:list2 isn't empty So it found and loaded a driver...
com.mysql.jdbc.Driver@650646 Here it is...
Filler Here's a line I did to make sure I wasn't printing a blank list...
The exception is java.sql.SQLException: No suitable driver And I'm still getting this result.
If it's in the DriverManager, why wouldn't it connect? Is it just a bad driver (I wouldn't think so, it came with the same version as a working program) or is the driver not in the right place, or is there a process I'm missing somewhere?

MonkeyCode
Newbie Poster
20 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

You'll get that error if it doesn't connect to the DB, has nothing to do with not finding the Driver.
This is all I have to connect to my DB:

Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://"+host;
connection = DriverManager.getConnection(url, user, password);

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 

Okay, trying yours but adding error catching and helpful printlns:

public int DBtest2(String hst){
  int worked = 1;
  try{Class.forName("com.mysql.jdbc.Driver").newInstance();}
  catch(Exception e){System.out.println("Class Error; exception " + e); worked = 0;}
  String url = "jdbc:mysql://"+hst;
  try{Connection conn = DriverManager.getConnection(url, username, password); conn.close();}
  catch(Exception e){System.out.println("Connection error; exception " + e); worked = 0;}
  return worked;
}

I get:Connection error; exception java.sql.SQLException: No suitable driver
Hmm... seeing if I can find what this error message means, since it's obvious I have a driver in there, and it's supposedly the one that belongs with the version MySQL I'm using.
Looks like most other people are having problems with how their url is formatted. Checking that out...

MonkeyCode
Newbie Poster
20 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

More fidgeting; after changing the connection's catch statement to:

catch (SQLException ex) {
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
            worked = 0;
        }

I got:SQLException: No suitable driver
SQLState: 08001
VendorError: 0

MonkeyCode
Newbie Poster
20 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

what does your full host address look like? If you dont specify a database, it must end with a "/".

jdbc:mysql://localhost/
or
jdbc:mysql://localhost/myDB

If that syntax isn't exact, you can get that "no driver" error.

Phaelax
Practically a Posting Shark
858 posts since Mar 2004
Reputation Points: 92
Solved Threads: 51
 
what does your full host address look like? If you dont specify a database, it must end with a "/". jdbc:mysql://localhost/ or jdbc:mysql://localhost/myDB If that syntax isn't exact, you can get that "no driver" error.

Well, bother. I was hopeful, since i was taking a user-inputted database and the user generally didn't add a "/" to the end of the host, but even after explicitly connecting to localhost:

try{Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/", "mylogin", "mypassword");

It's still sending back the same errors. Since it is loading /something/, the only thing left is the possibility that it's not an appropriate driver. Hmf.

MonkeyCode
Newbie Poster
20 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 
It's still sending back the same errors. Since it is loading /something/, the only thing left is the possibility that it's not an appropriate driver. Hmf.

Weirdness abounds. It seems to be getting a driver, a mysql one even, I just have no idea how or where it's coming from since the mysql-connector.jar isn't listed in the classpath. Except it should be... I checked the JBuilder.config file, it lists "addjars ../jdk1.4/lib". I copied the mysql jar into jdk1.4/lib (where the other jars listed on the classpath were) and closed JBuilder, but when I started JBuilder again and ran the program mysql*.jar still wasn't listed in the classpath.
Any suggestions?

MonkeyCode
Newbie Poster
20 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

Okay... I've made another project in JBuilder. All it does is connect to the database and add a line. And it works.

Oddly, now both projects have "*mysql-connecter-*" in the classpath, but not the lib one.

And more oddly, the full project still isn't functioning despite using the same syntax as the microproject.

MonkeyCode
Newbie Poster
20 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

it is likely that you tried to create two connections. eg

class DB{
    public DB(){
      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bangr", "user", "pass");
      ...
    }
  }
class classUsingDB1() {
    DB db = new DB();
    ...
  }
  
  class classUsingDB2() {
    DB db = new DB();
    ...
  }


This will give exception. If you comment out the 2nd conenction it
will work. So that means you should only have one conenction to one
DB schema.

To resolve this the code must be changed like this...

class DB{
    private static DB db = new DB();

    public static DB getHandle() {
      return db;
    }

    private DB(){
      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bangr", "user", "pass");
      ...
   }
 }
class classUsingDB1() {
    DB db = DB.getHandle();
    ...
  }
  
  class classUsingDB2() {
    DB db = DB.getHandle();
    ...
  }
tloonie
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Well done you just replied to 5 years old thread, where last entry was made 4 years ago with some feeble code, ignoring naming conventions. Was it worth it? Dunno...

Thread closed, no more time travellers accepted.

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You