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,

Recommended Answers

All 14 Replies

Where are you making a connection to the DB?

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.

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.

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.

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?

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);

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...

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

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.

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.

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?

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.

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();
    ...
  }

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.