I do realize that this question has been repeated quite often in here but none of the solutions I found worked in my case except one which wasn't quite suitable.
I am using netbeans to do a java project.
This is the code I use to connect to the mysql database.
public DBConnection()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
dbCon = DriverManager.getConnection("jdbc:mysql://localhost/sets","root","");
System.out.println("Database Connection Successful");
}
catch(Exception exc)
{
System.out.println("Database Connection error");
System.out.println(exc);
}
}
This works and I am able to connect successfully when I debug in the IDE, I added to library the file mysql-connector-java-3.1.12-bin.jar. But after building the project, the subsequent jar file throws the exception. I did set the classpath environment variable however it wasnt working. Also since I read here that environment variable is considered a poor practice, I added the jar file into the projects runtime library. as described here . But it too seemed to have no effect. Finally I added Class-Path: mysql-connector-java-3.1.12-bin.jar into manifest.mf file. That seemed to do the trick. However I would like to know if there is another way of doing the same. For when I tried to set the classpath to the jar file in program files it doesnt seem to take it. Also I need to add another jar file as well. Is there any other method of achieving the same?