I have this problem with my swing application: "java.lang.classnotfoundexception com.mysql.jdbc.driver with swing". When I created a connection I added mysql connector lib file and I made a test. It was successful. Now I am connected and I can see every table in my database. The problem is when I want to create a simple select query.

public class DatabaseManager {
    public static Connection getMySqlConnection() throws Exception {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost/Sample";
        String username = "root";
        String password = "123";
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        return conn;
      }
}
link|improve this question

60% accept rate
3  
How are you running your application when it fails? For example, is this through Eclipse, or is it already compiled? The exception suggests that your MySQL lib is not in the classpath. – Dennis 20 hours ago
I run my app normally and when I click on the button which call the select query I see the error. – user1107922 20 hours ago
2  
I would double-check to make sure your MySQL library is actually in the classpath. – Dennis 20 hours ago
@user1107922 : Please have a look at this example, hopefully this might be able to give you some idea how to put a .jar file to your classpath, to atleast make this program work. But first try to run your project with this command java -classpath pathToJARFile/myJARFile.jar;.; packageName.ClassNameWithMainMethod Now press Enter. No need to specify .class after Class Name. – nIcE cOw 15 hours ago
feedback

1 Answer

You don't have the driver class in your classpath when you run. Add the MySQL JAR to your runtime classpath.

There's lots wrong with your code. There's no reason to hard code this for only MySQL. It's bad form to have all that info in plain text that way. You could move it out into a properties file. You'll need more methods than this (e.g. closing connections, etc.)

link|improve this answer
How should I add this jar to my runtime classpath? I can't understand what is my classpath in swing application. This is first app which I code. – user1107922 20 hours ago
It's the same as a non-Swing application: what you tell the JVM when you run. Look at the -classpath option on java.exe. – duffymo 20 hours ago
I tried this: "java -classpath mysql-connector-java-5.1.21-bin.jar Client.classes.client.DatabaseManager.class" but I have this error:Caused by: java.lang.ClassNotFoundException: ...Could not find the main class: Client.classes.client.DatabaseManager.class. Program will exit. – user1107922 19 hours ago
Wrong, as you can see. You've got the JAR correct; now you need to tell it how to find your class with the main method. Your package name is certainly confusing. You need to add the directory that's the parent of your package structure to the classpath next. – duffymo 19 hours ago
Sorry for the stupid answer but I can't make it working. I put the jar in the directory Project. I have this structure: Project/Client/classes/client and there I have two class files - HomePage.class with the main method and DatabaseManager.class with the source which I wrote. – user1107922 19 hours ago
show 2 more comments
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.