Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

843859Nov 26 2008 — edited Nov 20 2014
Dear All,

I'm trying to connect to a mysql dbqse (ver 6.0) via a little Java progy using JDK6 and all upto date JRE etc:

I keep getting an error for the class not found exception - as in above title of post.

My environment variables are: (copy and paste from the env variables in XP Pro)

path=C:\Sun\SDK\bin;F:\Devel\Wachabe\GetMySQLUsers;C:\Sun\ConnectorJ\mysql-connector-java-5.1.7\mysql-connector-java-5.1.7-bin.jar

classpath=.;C:\Program Files\Java\jre1.5.0_06\lib\ext\QTJava.zip;C:\Sun\ConnectorJ\mysql-connector-java-5.1.7\mysql-connector-java-5.1.7-bin.jar;F:\Devel\Wachabe\GetMySQLUsers

I have tried all the obvious things from this and other forums.

Ensured that the path to the program is in both fields for path and classpath. but still no luck.

The program will compile fine, I have confirmed that the connector is available in netbeans in the <tools | libraries> section and that it is located in the correct place etc etc.

I have 3 classes.

---------contains the ,ain class main----------
package getmysqlusers;

import java.util.Scanner;
import java.sql.*;
import javax.sql.*;

public class GetMySQLUsers {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws SQLException 
     {
         //a scanner to read the data in from the cli
         //Scanner readCLI = new Scanner(System.in);

          //get mySQL user name
         // System.out.println("please enter user name"); //will be used to get the details from a GUI eventually
          String readName = new String("MyoGroup");//readCLI.nextLine(); //read the CLI into the variable.

          //get the mySQL pwd
         //System.out.println("please enter your password");
          String readPWD = new String("anonymous");//readCLI.nextLine();//get the next line of CLI data and pass to the new variable.

         ConnectSQL SQL = new ConnectSQL(readName, readPWD);
         
         System.out.println("username is " + SQL.getUser());
         System.out.println("pwd is " + SQL.getPWD());
    }

}
---------controls the details of the user etc-------------
public class ConnectSQL 
{

package getmysqlusers;
import java.sql.*;
import java.sql.SQLException;
import javax.sql.*;
//insert instance variables here.
     
//string variables for holding the user name and pwd, collected from command line initially but later from a GUI login.
private String user;
private String pwd;
private connectToMysql login;//to open the connection to the database

//Call to the constructor


public ConnectSQL(String aUser, String thePWD) throws SQLException//note the try catch is handled in the connectToMysql Class.
{
     user = aUser;
     pwd = thePWD;
     login= new connectToMysql(getUser(), getPWD());
     login.toString();//used for testing.
}



//Methods for this class,  eg setter and getter methods.

public String getUser()//used for getting the user's name
     {
          return user;
     }

public String getPWD()
     {
          return pwd;
     }

}
-----------handles the actual connection--- or rather doesn't!!-----------
package getmysqlusers;

//import getmysqlusers.ConnectSQL.*;//as thi

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.sql.*;


public class connectToMysql 
{
//insert instance variables here.
private Connection con =null; //to hold the connection to the database.

//the connection is made up of 3 components as follows
private String URL; //the URL to the database.
private String schema;//the table schema to use. 
private String uName;//the name of the user.
private String MotDePass;//the users password.



//Call to the constructor <public nameOfClass(variables to pass to class)


public connectToMysql(String name, String pass) throws SQLException
{// grab the name and details from the GetMySQLUsers class.
     setURL("jdbc:mysql://localhost/mysql"); // test using a fixed string.
     setSchema("mysql");
     setUName(name);
     setPass(pass);
     //makeConnection();
     
     
//     String ODBCcon;
//     DBCcon = new String("DRIVER={MySQL ODBC 3.51 Driver}" +
//                   "SERVER=localhost" +
//                   "DATABASE=test" +
//                   "USER=venu" +
//                   "PASSWORD=venu"
//                   "OPTION=3");

//requiruires a try an catch
try{
    
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection(URL + uName + MotDePass);
}

catch(java.lang.ClassNotFoundException err)
     {    System.out.println("error in connecting.. again! " + err);}

catch(SQLException err)
{
System.out.println("error in connecting to DBase system " + err);
}



}
//Methods for this class,  eg setter and getter methods.
public void setSchema(String schema)
{
schema = new String(schema);
}
//get the DBname to be connected to.
public String getSchema()
{
return schema;
}

//set the URL for the database, if it needs to be changed
public String setURL(String url)
{
URL = url;//new String("jdbc:mysql:/localhost:3306/test1");
return URL;
}


//methods to set a username.
public String setUName(String name)
{
     uName = new String(name);
     return uName;
}
//methods to get a username.
public String getUName()
{
return uName;
}

//methods to set a password ~ it is never required to retrieve a password!
public String setPass(String mdp)
{
MotDePass = new String(mdp);
return MotDePass;
}

//the method to return details of the DB in use and the user who is logged in, overides the toString method of Object
public String toString()
{
String report = new String(getUName() +" is connected to the database " + getSchema());
return report;
}

//this method will make a connection to the database using the required driver.
//public void makeConnection()
//{//requiruires a try an catch
//try{      Class.forName("com.mysql.jdbc.Driver");      }
//catch(java.lang.ClassNotFoundException err)
//     {    System.out.println("error in connecting.. again! " + err);}
//}

}
I think I have included everything.

I can confirm that using the username and password is fine in the monitor.

I have also tested this on my Ubuntu desktop and have the same problem, although there I also have an issue with the classpath not being retained? but I can solve that once I understand why I can see the class in the firstplace.

my thoughts are:

should I have the whole of the connectorJ;java file in the same folder as the rest of my package?
should I in fact have it as a class in my package? - this seems a bit ridiculous as I don't need to do this for the other java libraries that I am using!

help is greately appreciated.

for information.

I am developing a system for medical research, and I need to be able to connect to a database that the team is developing. Firstly however I would like to show that I can succesfully connect to MySQL localy and then from anywhere on the network (I intend to create an "applet" of some sort) - but that is a little further down the line, first I need to get this to work so as I can add in some functionality to auto update some tables (better still if I could do it on the fly... but the team insist on using another "closed source" front end for the time being untill I can get this to function satisfactorily (which seems more than fair enough).

thanks again in advance.

David
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 26 2010
Added on Nov 26 2008
6 comments
1,176 views