Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling MySQL JDBC Driver - No Suitable Driver

Status
Not open for further replies.

sherifMe

IS-IT--Management
Jun 26, 2005
4
GT
Hello,

I have setup a a small text file where user can specify the following, so that nothing is hard coded into the application:

Driver = com.mysql.jdbc.Driver
Driver String = jdbc:mysql://
Host = localhost
Port = 3306
user = root
pwd = root
I have some code to make the "Class.forName(driverClass).newInstance()"
Statement work.

I have driver (J Connector) added to my classpath or so I think. I do not wich o add it to my Jar or my Project as the user may wish to use a different Database eventually (like Oracle)

When I run the application through JBuilder it runs fine.

But when I run a JAR, i get "No Suitable Driver" error message.
I am using the following command
java -jar myjar.jar -cpmysql.jar

What am I doing wrong?

Thanks for you help!

Sherif
 
As long as the JConnector is available on your classpath, it should work. Don't add the JConnector classes to your Jar. To switch over to Oracle, you'd simply leave the JConnector Jar off the classpath, and add the path to the Oracle JDBC connector instead ( as well as editing your text file).

If you pasted the java command line verbatim, then the problem is probably the lack of a space after the -cp flag. I'm assuming that myjar.jar includes a Manifest which identifies the main class and that both jars are in the current directory).

It should be:-
Code:
java -jar myjar.jar -cp mysql.jar

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Thanks for the help.
It is greatly appreciated....
However it still did not work.
I am not sure why it is not seeing the driver jar!
 
Can I ask you to post the code you're using to create your JDBC connections. It may be that you're relying on some JBuilder magic which is not provided when running from command-line. Please post the exact exception stack trace your getting, too.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Hi Tim,

Thanks again for the response.

Here is the Code:

Part that reads the code and main class which establishes the connections:

private static StringBuffer getReadErpiccolo() {
StringBuffer contents = new StringBuffer();
BufferedReader input = null;
String erpiccolo = "erpiccolo.txt";
try {
input = new BufferedReader(new FileReader(erpiccolo));
String line = null;
int i = 0;
while ( (line = input.readLine()) != null) {
contents.append(line);
contents.append(System.getProperty("line.separator"));
if (i == 0){
driverJar = line;
}
else if (i == 1) {
driverClass = line;
}
else if (i == 2) {
dbServerInitURL = line;
}
else if (i == 3) {
dbServerHost = line;
}
else if (i == 4) {
dbServerPort = line;
}
else if (i == 5) {
serverlogin = line;
}
else {
serverpass = line;
}

i++;
}

}
catch (Exception ex) {
ex.printStackTrace();
}

try {
input.close();
}
catch (IOException ex1) {
}

return contents;
}

public static void main(String args[]) throws Exception {
getReadErpiccolo();

completeURL = dbServerInitURL + dbServerHost + ":" +
dbServerPort + "/";
SQLUtility.driverClass = driverClass;
SQLUtility.dbServerInitURL = dbServerInitURL;
SQLUtility.dbServerHost = dbServerHost;
SQLUtility.completeURL = completeURL;
SQLUtility.dbServerPort = dbServerPort;
SQLUtility.serverlogin = serverlogin;
SQLUtility.serverpass = serverpass;
SQLUtility.mainDB = mainDB;
SQLUtility.seriesDB = seriesDB;
SQLUtility.systemDB = systemDB;
SQLUtility.inventoryDB = inventoryDB;
SQLUtility.crmDB = crmDB;
ikkunalogin.systemDB=systemDB;
SQLUtility.setConnectionProperties(driverClass,
dbServerInitURL,
dbServerHost,
completeURL,
dbServerPort,
serverlogin,
serverpass,
mainDB,
seriesDB,
systemDB,
inventoryDB,
crmDB);
SQLUtility.getServerConnection();
// the above line calls another method which does the
// Class.ForName(...).
myConnection = myInventoryDBConn =
SQLUtility.getDefaultInventoryDBConnection();
SQLUtility.getDefaultMainDBConnection();
mySeriesDBConn =
SQLUtility.getDefaultSeriesDBConnection();
mySystemDBConn =
SQLUtility.getDefaultSystemDBConnection();
myTaskDBConn = SQLUtility.getDefaultTaskDBConnection();
//
// more stuff here
//
}

public static Connection getServerConnection() {
ClassFound();
try {
Connection con =
DriverManager.getConnection(completeURL,
serverlogin, serverpass);
Connection connection = con;
return connection;
}
catch (SQLException se) {
System.out.println(String.valueOf(String.valueOf( (new
StringBuffer(
"SQLException while creating generalConnection to"+
"server: [")).append(dbServerHost).append("]"))));
System.out.println(" |--JDBCURL:"+
".concat(String.valueOf(String.valueOf(
completeURL))));
se.printStackTrace();
Connection connection1 = null;
return connection1;
}
}

private static boolean ClassFound() {
if (ClassOk) {
return true;
}
boolean isOK = true;
try {
Class.forName(driverClass).newInstance();
//The above line is where the error originates.
ClassOk = true;
}
catch (ClassNotFoundException cnfe) {
JOptionPane.showMessageDialog(null,
"Driver class not found.\n Please check your"+
"classpath.\n ------------------------------\n"+
"Driverclass:"+driverClass+",ClassNotFoundException",
0);
isOK = false;
}
catch (Exception e) {
JOptionPane.showMessageDialog(null,
"Driver class is not loaded", 0);
isOK = false;
}
return isOK;
}
___________________________________________________________________
Exact Error Message:

SQLException while creating generalConnection to server: [localhost]
|--JDBCURL: jdbc:mysql://localhost:3306/
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getConnection(DriverManager.java:532)
at java.sql.DriverManager.getConnection(DriverManager.java:171)
at erpiccolo.util.SQLUtility.getServerConnection(SQLUtility.java:585)
at erpiccolo.Frame2.main(Frame2.java:655)
__________________________________________________________________
Please note...:

I included the mysql jar in the project properties and asked the jar to "include all"

It worked fine....

So the error is in the classpath somehow!?
Must be...!! No?

Regards,
Sherif

 
Err. My apologies. Just rememembered that if you use the -jar options to run your program, then the classpath (set by -cp) is ignored. You'll have to put all your jars on the classpath using the -cp option and specify the fully qualified classname of your main class instead.

On a note of coding practices, you've written things like
Code:
  Connection connection1 = null;
  return connection1;
... and
Code:
  Connection connection = con;
  return connection;

These should be
Code:
  return null;
... and
Code:
  return con;
respectively. Why create a whole new reference variable just to return a value?

Also, when loading a Driver class, you don't need to create an instance. So
Code:
Class.forName(driverClass).newInstance();

becomes ...
Code:
Class.forName(driverClass);

Hope this helps.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Thanks Tim,

I was without Internet over the last week.
I thank you for the tips...

I am actually not a programmer and I am learning as I go.
There are a lot of "cra." in my code I have to clean eventually.

It is impossible to find Java programmers here. So i have to do everything myself. (Here being Nicaragua, where I moved recently)

 
Hi Sherif.

Don't worry about 'not being a programmer'. I AM a programmer, and I still 'learn as I go'. I hope my reply above has got you moving forwards.

Since you are learning, and due to the lack of face-to-face Java peers, I suspect you will probably have other Java questions to post over time. Can I recommend you post 'pure' Java questions like this one in the Java forum (forum269) in future. There are some sharp chaps in there who know lots of stuff. This forum is really for problems with using JBuilder itself, not Java.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top