简体   繁体   English

在类路径中找不到驱动程序。

[英]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!" 错误:“线程“主”中的异常java.lang.IllegalStateException:在类路径中找不到驱动程序!

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. 您需要将MySQL JDBC驱动程序添加到类路径。

First, download the JDBC driver from the MySQL web site -- the JDBC "connector". 首先,从MySQL网站(JDBC“连接器”)下载JDBC驱动程序。 This is a .jar file that you will place in a known directory. 这是一个.jar文件,您将放置在已知目录中。 For example, I got mysql-connector-java-5.1.38-bin.jar in a directory. 例如,我在目录中找到了mysql-connector-java-5.1.38-bin.jar

Then, when starting the application, add this JAR file to the classpath, as in: 然后,在启动应用程序时,将此JAR文件添加到类路径中,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java classpath找不到MySQL驱动程序 - Java classpath cannot find MySQL driver Spring无法在Tomcat类路径中找到属性文件。 - Spring cant find properties file in Tomcat classpath. 在类路径上找不到javax.persistence.Persistence。 假设非JPA 2环境。 默认情况下,所有属性都是可遍历的。 春季靴子A - Cannot find javax.persistence.Persistence on classpath. Assuming non JPA 2 environment. All properties will per default be traversable. Spring Boot A Maven构建清单不包含类路径。 - Maven build manifest does not contain classpath. 设置类路径。 javac无法识别 - setting up classpath. javac is not recognized 在类路径中找不到类 - Cannot find class in classpath Elasticsearch “StatusLogger Log4j2 找不到日志记录实现。 请将 log4j-core 添加到类路径中。” - Elasticsearch “StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath.” 环境变量CLASSPATH &lt;&gt; Clojure的CLASSPATH。 为什么? - Environment variable CLASSPATH <> Clojure's CLASSPATH. Why? Spring 引导 - PostgreSQL 驱动程序无法位于类路径中 - Spring Boot - PostgreSQL driver cannot be located in classpath 在类路径中找不到类:(类名) - Cannot find class in classpath: (ClassName)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM