【发布时间】:2014-03-15 04:07:38
【问题描述】:
我是使用 IntelliJ 13 的新手,但在连接 MySQL 时遇到问题。我正在使用下面的代码,这个代码是我从教程 Manage Your Database Scheme with 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 文件,但我不知道将这些文件放在哪里。你有想法吗?
【问题讨论】:
-
关注“相关”问题。
-
尝试将 catch 块与 finally 块一起添加,然后打印堆栈跟踪。
-
“我试图通过将 mysql-connector-java-5.1.26-bin.jar 放入项目结构中来解决这个问题” - 我不了解 IntelliJ,但在 Eclipse 中这不是必须与将 jar 添加到项目 CLASSPATH 或引用的库中相同。
标签: java jdbc database-connection classnotfoundexception