0

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.

Richard Rhyan
  • 203
  • 1
  • 6
  • 17
  • Please use code formatting on error output as well. Note how much more clear it is (I edited your question) using formatting. – Andrew Thompson Mar 18 '12 at 03:35
  • Per the recommendations below, I have.... added Class.forName("com.mysql.jdbc.Driver"), moved the "mysql-connector-java-x.x.xx-bin.jar" file into a WEB-INF/lib directory (located in the same directory as the application) and added the "mysql-connector-java-x.x.xx-bin.jar" to the -classpath. None of these have worked, nor have they in any combination I could come up with. I'm very confused on why this doesn't work. – Richard Rhyan Mar 18 '12 at 06:39
  • a `-bin.jar` typically contains natives (e.g. `.dll` or `.so` files). AFAIU the user Jars should also contain a Jar for classes only, specifically `com.mysql.jdbc.Driver`. Do you have an IDE that allows you to examine the contents of the Jars, or do you know how to list the content using the SDK tools? If so, find the JAr that contains that class, and add it to the `WEB-INF/lib`. Note also that a) it is better to edit this type of info. into the question as an update b) only I would have been notified of your comment starting.. *"Per the recommendations below,.."* – Andrew Thompson Mar 18 '12 at 08:26
  • Try to add the mysql connector file (can be found [here](https://www.mysql.com/products/connector/)) to your build path, a tutorial to do this is [here](http://stackoverflow.com/questions/3280353/how-to-import-a-jar-in-eclipse). Then the last thing you'll need to do is check if you have the correct import in your java project (com.mysql.jdbc.Connection rather than java.sql.Connection). – user7329062 Dec 25 '16 at 03:42

6 Answers6

1

You not only need the driver jar file in your IDE, but you need it to be installed on the server, too. You don't say what sort of server this is, but the jar need to be on the class path of your server process; i.e., in the WEB-INF/lib directory of your web app, or simply indicated in the -classpath argument to your server JVM.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
1

When you run the file in eclipse your jdbc driver is in your project classpath. When you deploy it to the server you must include it there also. Either in the war/jar/ear lib or in some server lib.

gebuh
  • 797
  • 14
  • 40
1

Call Class.forName("com.mysql.jdbc.Driver"); prior to DriverManager.getConnection(url);.

Because DriverManager doesn't know the MySql Driver . Class.forName("com.mysql.jdbc.Driver") registers MySql Driver implementation itself to DriverManager.

btw: it's "when running" instead of "when compiled" in your question title.

卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130
1

buddy,its easy,just add "mysql-connector-java-x.x.xx-bin.jar" into the folder of "WEB-INF/lib"

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
TheRealBo
  • 73
  • 1
  • 10
  • I have the file, but I do not have the directory. The file is in the build path, and I'm novice enough at Java and Eclipse that I thought it would include the information in the jar. – Richard Rhyan Mar 18 '12 at 03:56
0

you need to add ojdbc14.jar as well as mysql connector jar in your application's WEB-INF/lib folder.

Please let me know, if you have any more questions.

kandarp
  • 4,979
  • 11
  • 34
  • 43
0

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:

Class-Path: /path/file.jar

With blank line at the end of the file

Richard Rhyan
  • 203
  • 1
  • 6
  • 17