[英]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.