java.lang.ClassNotFoundException:com.mysql.cj.jdbc.Driver
我到处都找遍了,但这个问题的答案对我都没有帮助 我的问题是: java.lang.ClassNotFoundException:com.mysql.cj.jdbc.Driver 我把它添加为一个外部罐子。 在这不起作用之后,我将我的文件转换为Maven并为它添加了依赖项,这仍然没有帮助。 我尝试了人们在互联网最狭窄的角落提出的所有建议,但都没有用 我需要一些帮助 [英] 我知道可能需要我将它重命名为com.mysql.jdbc.Driver,但它也不能使用这个名称。我重新命名了它,因为这似乎是通往它的道路java.lang.ClassNotFoundException:com.mysql.cj.jdbc.Driver,java,mysql,Java,Mysql,我到处都找遍了,但这个问题的答案对我都没有帮助 我的问题是: java.lang.ClassNotFoundException:com.mysql.cj.jdbc.Driver 我把它添加为一个外部罐子。 在这不起作用之后,我将我的文件转换为Maven并为它添加了依赖项,这仍然没有帮助。 我尝试了人们在互联网最狭窄的角落提出的所有建议,但都没有用 我需要一些帮助 [英] 我知道可能需要我将它重命名为com.mysql.jdbc.Driver,但它也不能使用这个名称。我重新命名了它,因为这似乎是通
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.sql.*" import="com.mysql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Add a new company</title>
<link href="bootstrap.css" type="text/css" rel="stylesheet">
</head>
<body>
<h1>New Company</h1>
<form method="post" action="CreateNewCompany">
<table>
<tr>
<td>Name</td>
<td><input type="text" name="fullName"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td>Address</td>
<td>
<select class="form-control">
<option value="-1">Select an address</option>
<%
try{
String Query = "select * from addresses";
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/company","root","1234");
Statement statement = conn.createStatement();
ResultSet resSet = statement.executeQuery(Query);
while(resSet.next()){
%>
<option value="<%=resSet.getInt("addressId")%>"><%=resSet.getString("street")%></option>
<%
}
}
catch(Exception ex){
ex.printStackTrace();
out.println("Error: " + ex.getMessage());
}
%>
</select>
</td>
</tr>
<tr>
<td>List of Employees</td>
<td><input type="text" name="employees"></td>
</tr>
</table>
<input type="submit" value="Submit" name="submit"> <input
type="button" value="Back" name="return">
</form>
</body>
</html>
下面是一个加载驱动程序并获取连接对象的示例。检查以下几点 一, 不要导入com.mysql.jdbc* 用于获取连接对象
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Connection conn = null;
...
try {
conn =
DriverManager.getConnection("jdbc:mysql://localhost/exampledb?" +
"user=<<userName>>&password=<<Password>>");
...
} catch (SQLException ex) {
// handle any errors
}
2检查jar是否添加到类路径中
3另外,尝试手动构建项目。结果是我不得不下载一个旧版本的连接器,这使它能够工作。谢谢大家的时间和帮助 描述你的部署模型!它是桌面应用程序吗?基于servlet的标准web应用程序?它是否部署在OSGI容器上?您是否在运行时通过Class.forNamecom.mysql.cj.jdbc.driver加载了驱动程序?是的,我已经这样做了。最后,我找到了如何编写代码,很抱歉,删除过时的Class.forName会更简单、更具前瞻性。自2007年以来不再需要。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
Connection conn = null;
...
try {
conn =
DriverManager.getConnection("jdbc:mysql://localhost/exampledb?" +
"user=<<userName>>&password=<<Password>>");
...
} catch (SQLException ex) {
// handle any errors
}