简体   繁体   中英

Cannot find the driver in the classpath.

This is what I'm working with right now. I can't seem to figure out why when I run it I'm still getting this error. I don't know what to do.

Error: "Exception in thread "main" java.lang.IllegalStateException: Cannot find the driver in the classpath!"

Please help me. I don't know what to do anymore.

 import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * * @author Taylor Dean */ public class CustomerUtil { private static Connection connection; private CustomerUtil() {} public static synchronized Connection getConnection() throws SQLException { try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("loaded!"); } catch (ClassNotFoundException e) { throw new IllegalStateException("Cannot find the driver in the classpath!", e); } if (connection != null) { return connection; } else { try { // set the db url, username, and password String url = "jdbc:mysql://localhost:3306/mma"; String username = "root"; String password = "21334966154Dahk"; // get and return connection connection = DriverManager.getConnection( url, username, password); return connection; } catch (SQLException e) { throw new IllegalStateException("Cannot connect the database!", e); } } } public static void closeConnection() throws SQLException { if (connection != null) { try { connection.close(); } catch (SQLException e) { throw e; } finally { connection = null; } } } } 

You need to add the MySQL JDBC driver to the classpath.

First, download the JDBC driver from the MySQL web site -- the JDBC "connector". This is a .jar file that you will place in a known directory. For example, I got mysql-connector-java-5.1.38-bin.jar in a directory.

Then, when starting the application, add this JAR file to the classpath, as in:

java -cp my_dir/mysql-connector-java-5.1.38-bin.jar CustomerUtil

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