我已經將類路徑設置爲mysql-connector-java-5.0.8-bin.jar併成功編譯了我的類。 但是當我運行它,我得到:即使在添加mysql-con.jar文件後,「com.mysql.jdbc.Driver」的ClassNotFound異常
拋出java.lang.ClassNotFoundException:com.mysql.jdbc.Driver
的代碼是:
import java.sql.*;
public class JdbcExample
{
public static void main(String arg[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://loacalhost:3306/sample","root","root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from sample");
while(rs.next())
{
System.out.println(rs.getString(1));
}
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
你嘗試按照這些鏈接, http://stackoverflow.com/questions/1585811/classnotfoundexception-com-mysql-jdbc-driver和http://stackoverflow.com/questions/17484764/java-lang-classnotfoundexception-com-mysql-jdbc-driver-in- eclip se – Dev4World
yup ...在所有這些鏈接中,人們使用了一些IDE ......我沒有這樣做。他們都把這個罐子放在錯誤的文件夾裏。這裏情況不同。我沒有使用IDE。另外請注意,當我使用javac -cp「我的類路徑」filename.java時,類成功編譯...檢查此鏈接的答案由斯蒂芬更好地瞭解問題http://stackoverflow.com/questions/43918756/classnotfound-exception -for-com-mysql -jdbc-driver-even-after-adding-mysql-con?stw = 2 –
我嘗試過stephen的解決方案,即使在運行時也提及jar classpath ...但它沒有工作... –