简体   繁体   中英

“ClassNotFoundException: com.mysql.jdbc.Driver”

I'm new using IntelliJ 13 and I have a problem on connection to MySQL. I'm using the code as below and this code I learn from tutorials 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();
            }
        }
    }
}

I have tried many solution based on this issue on https://stackoverflow.com/ but nothing can solved my problem. It give error message like this

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)

I've followed this as well to solve my problem, but nothing works to me even the problem is same. http://nixmash.com/java/classnotfoundexception-com-mysql-jdbc-driver-fix-in-intellij-idea/ . Actually, I want to create database using MySQL on IntelliJ 13 but the connection to MySQL is failed. I have tried to solve this problem with putting mysql-connector-java-5.1.26-bin.jar in project structure, but it's not work for me. I don't know how to solve this problem? I hope some experts on this issue will help me some ways to solve this problem. I cannot use the comment, I don't know why. So I reply the answer here. Ashish: I already download the jar files, but I don't know where to put that files. Do you have idea?

You need to download jar containing com.mysql.jdbc.Driver . So you can Download jar from here and then set your classpath .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM