[英]“ClassNotFoundException: com.mysql.jdbc.Driver”
我是使用IntelliJ 13的新手,但與MySQL的連接存在問題。 我正在使用以下代碼,並且這些代碼是我從使用IntelliJ IDEA 12管理您的數據庫方案的教程中學到的。
public class App {
public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
public static final String JDBC_URL = "jdbc:mysql://localhost/sample";
public static final String JDBC_USER = "root";
public static final String JDBC_PASSWORD = "";
public static void main (String[] args) throws SQLException, ClassNotFoundException {
Statement statement = null;
try{
Class.forName(JDBC_DRIVER);
Connection connection = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select id, firstname, lastname, email " +
"from customer");
System.out.println("First name\tLast name\tEmail");
int count = 0;
while (resultSet.next()){
String firstname = resultSet.getString("firstname");
String lastname = resultSet.getString("lastname");
String email = resultSet.getString("email");
System.out.printf("%s\t%s\t%s%n", firstname, lastname, email);
count++;
}
System.out.println("--");
System.out.println(MessageFormat.format("Rows: {0}", count));
} finally {
if(statement != null){
statement.close();
}
}
}
}
我已經在https://stackoverflow.com/上嘗試過基於此問題的許多解決方案,但是沒有任何方法可以解決我的問題。 它給出這樣的錯誤信息
Connection to MySQL – sample@localhost failed: Exception in thread “main” java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.intellij.persistence.database.console.RemoteJdbcServer.main(RemoteJdbcServer.java:15)
我也遵循此方法來解決我的問題,但是即使問題相同,也無濟於事。 http://nixmash.com/java/classnotfoundexception-com-mysql-jdbc-driver-fix-in-intellij-idea/ 。 實際上,我想在IntelliJ 13上使用MySQL創建數據庫,但與MySQL的連接失敗。 我試圖通過將mysql-connector-java-5.1.26-bin.jar放在項目結構中來解決此問題,但是它對我不起作用。 我不知道如何解決這個問題? 我希望有關此問題的一些專家可以幫助我解決該問題的一些方法。 我無法使用評論,不知道為什么。 所以我在這里回答答案。 Ashish:我已經下載了jar文件,但是我不知道將文件放在哪里。 你有主意嗎
您需要下載包含com.mysql.jdbc.Driver
jar。 因此,您可以從此處下載jar ,然后設置您的classpath
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.