I wrote a program to connect to a database using jdbc with type 4 connectivity. The program compiles just fine but gives an exception of java.lang.ClassNotFoundException : com.mysql.jdbc.Driver. I have extracted all the folders in the folder in which my java file is and even placed all the jar files and database in the same folder. The database contains two records with ID 1 and 2. What could be the problem?
import java.sql.*;
class TestJDBC {
public static void main(String aa[]) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Student_Details", "root","");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select ID, studentname from Details where ID=1");
while(rs.next()) {
System.out.print(rs.getString("ID"));
System.out.print("\t");
System.out.println(rs.getString("StudentName"));
}
}
catch(Exception e) {
System.out.println(e);
}
}
}
If your mysql-connector-java-5.0.8-bin.jar
file is located with a path like \\lib\\
within your Project like below
Then with a cmd prompt (assuming windows) change directory to this folder, then you would need to compile your class with
javac -cp ".;lib/mysql-connector-java-5.0.8-bin.jar" YourClass.java
use :
if not windows (only need quotes if spaces in file/folder names)
then run with
java -cp ".;lib/mysql-connector-java-5.0.8-bin.jar" YourClass
Also make sure you dont have duplicate jars in use
you need to have the jar containing com.mysql.jdbc.Driver
in the class path
eg
java -cp mysql.jar;. TestJDBC
where mysql.jar is the jar containing the class file
com/mysql/jdbc/Driver.class
on a windows machine
placed mysql-connector-java-5.0.8-bin.jar
and TestJDBC.class
in same folder.
from the folder executed java -cp mysql-connector-java-5.0.8-bin.jar;. TestJDBC
java -cp mysql-connector-java-5.0.8-bin.jar;. TestJDBC
that worked as expected.
unix usually requires
java -cp mysql-connector-java-5.0.8-bin.jar:. TestJDBC
As answered by BevynQ, setting class path from command line sets the path(As best of my knowledge) for that particular current instance of cmd, if you exit/close and reopen command prompt, again you need to set the path. I guess two solutions for this to use jar files in your programs:
Solution 1) Copy jar files in Program Files/Java/JRE/lib folder, so that it will be available for run time environment, But this is not recommended to add jar files in JDK/JRE.
Solution 2) Set the classpath variable to place the full path of your jar file location, So that it will be available for run time environment: Copy full path of your jar file location, go to environment variable, search for user/system classpath variable(User variable for current login user, system variable for all users accounts on current windows) if it is there, edit it and put your path in starting, otherwise create a new classpath variable and place your full path
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.