Reputation: 439
I was trying to connect to MySQL using JDBC API. I have downloaded the MySQL driver which is the "mysql-connector-java-5.1.28-bin jar" file. My OS is Windows 7 and I have set the Classpath of Java to following path:
"E:\Myclass"
I have copied the above jar file to this folder. Then I have written the following code to test if I can load the driver.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class LoadDriver {
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
System.out.println("Unable to load Driver Class");
return;
}
}
}
Note: Thanks for all your answers. I have solved the problem. Since I am using Eclipse, I have add the JAR file to the classpath of the Eclipse.
Upvotes: 0
Views: 2910
Reputation: 4923
Use the below cmd to run it
java -cp E:\Myclass\mysql-connector-java-5.1.28-bin.jar; LoadDriver
As mentioned the mysql jar exist @ E:\Myclass\mysql-connector-java-5.1.28-bin.jar
, just set in the classpath and run it
Upvotes: 0
Reputation: 23627
You have to include the JAR in your classpath:
java -jar yourdriver.jar LoadDriver
JARs are filesystems. They should be added to your classpath the same way you add directories. Only classes will be loaded from the classpath you specified.
Upvotes: 2