Can someone please please explain to me why the following code works when I run the app in the Eclipse IDE, but it generates an exception when compiled and run on a server?
I just don’t get it….
public class Database {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public Database() throws SQLException {
String host = "localhost:3306";
String schema = "stickmanDatabase";
String user = "richard";
String password = "xxxxxxxx";
String url = "jdbc:mysql://" + host + "/" + schema + "?user=" + user + "&password=" + password;
try {
conn = DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace();
throw e;
}
}
}
Here’s the stacktrace I’m getting:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/stickmanDatabase?user=richard&password=xxxxxxxx
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at stickmanPyramidScheme.server.Database.<init>(Database.java:23)
at stickmanPyramidScheme.server.LoginVerification.verifyLogin(LoginVerification.java:10)
at stickmanPyramidScheme.server.StickmanServer.parseAndExecute(StickmanServer.java:55)
at stickmanPyramidScheme.server.StickmanServer.main(StickmanServer.java:32)
My understanding is that the problem is that I need the file “mysql-connector-java-5.1.18-bin.jar in the build path, which I do. And if I remove it the code no longer works when running within Eclipse.
Any help would be greatly appreciated. Thanks.
…
Per Andrew’s request… I am using Eclipse, on an Ubuntu Linux server.
I can see that “mysql-connector-java-5.1.18-bin.jar” contains “com.mysql.jdbc” which contains the “Driver.class”
The jar is added to the (application directory)/WEB-INF/lib directory. And still getting the same error.
I figured out the problem…
I was under the impression that Eclipse was to take care of getting all the files together, but I needed to add the classpath in the Manifest. Putting it in the manifest worked, but adding a -classpath option nor adding it to my PATHs did not.
Anyone with a similar problem should note that you need to add this to the MANIFEST.MF in the jar that Eclipse creates:
With blank line at the end of the file