File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Problems accessing MySql from a jar file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Problems accessing MySql from a jar file" Watch "Problems accessing MySql from a jar file" New topic

Problems accessing MySql from a jar file

Roy Doorenbos

Joined: Jan 27, 2004
Posts: 2
I built a small program for the teachers at our school to help keep track of grades and it needs to access a MySQL database. Everything was fine until I packed the class files into a jar file and tried to run it using the java -jar command and I got a "java.sql.SQLException: No suitable driver" error. The jar file sits in the same directory as the class files from which they were derived.
Here is the code:
Connection con;
public Connection getConnection() {
}catch(Exception e) {System.out.println("Std_Connection.init>no drivers");}
con = DriverManager.getConnection("jdbc:mysql://localhost/Standards");
}catch(SQLException sqle) {System.out.println("Std_Connection.init>Cannot make connection. sqle= "+sqle);}
return con;
I have the mysql jar file on the classpath. I have to, otherwise I wouldn't be able to access the db at all! I made and compiled a very simple program to access the drivers and the same thing happened. It did Ok when run from a directory, but failed to find the drivers when jar-ed.

Roy from Iowa
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15302

Yep, that's what happens.
The easiest way to resolve this is to unjar the MySQL Driver (I hope you are using the Connector/J Driver) and then jar the file structure back into your application JAR file.

GenRocket - Experts at Building Test Data
I agree. Here's the link:
subject: Problems accessing MySql from a jar file
It's not a secret anymore!