Reputation: 610
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;
}
}
Upvotes: 1
Views: 854
Reputation: 308968
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.)
Upvotes: 2