java.sql.SQLException: 找不到适合 jdbc:mysql://localhost:3306/database 的驱动程序
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/database
我一直在尝试为学校项目设置自己的休息 api。我决定使用 MySQL 作为数据库,我想将它与我的网络服务连接起来,但显然我总是收到此错误消息:
java.sql.SQLException: 找不到适合 jdbc:mysql://localhost:3306/userdatenbank
的驱动程序
当我查看错误消息时,它还告诉我无法执行此代码:
public UserService() {
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdatenbank", "root", "adminadmin123");
} catch (SQLException e) {
e.printStackTrace();
}
}
我不知道为什么会这样... MySQL Java 连接器 JAR 文件位于 "Referenced Libraries"。顺便说一句,我使用 Tomcat 9、MySQL 8、JDK 13 和 Maven (Jersey Servlet)。
在建立连接之前,您必须加载您的驱动程序:
Class.forName("com.mysql.jdbc.Driver");
并且对应的JAR必须在你的类路径中(在你的服务器的lib中或者打包在你的WAR文件中)。
无论如何我建议你使用 connection's pool 而不是 DriverManager
The main benefits to connection pooling are:
- Reduced connection creation time.
Although this is not usually an issue with the quick connection setup that MySQL offers compared to other databases, creating new JDBC connections still incurs networking and JDBC driver overhead that will be avoided if connections are recycled.
- Simplified programming model.
When using connection pooling, each individual thread can act as though it has created its own JDBC connection, allowing you to use straightforward JDBC programming techniques.
- Controlled resource usage.
If you create a new connection every time a thread needs one rather than using connection pooling, your application's resource usage can be wasteful, and it could lead to unpredictable behaviors for your application when it is under a heavy load.
由于您使用的是 Maven,只需在 pom.xml 文件中添加依赖项即可。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
只需转到 Maven 存储库并搜索适合您的版本。
或者,如果您已经下载了 jar 文件,则将其添加到您的项目类路径中。
编辑 -> Class.forName() is not needed since JDBC 4.0.
我一直在尝试为学校项目设置自己的休息 api。我决定使用 MySQL 作为数据库,我想将它与我的网络服务连接起来,但显然我总是收到此错误消息: java.sql.SQLException: 找不到适合 jdbc:mysql://localhost:3306/userdatenbank
的驱动程序当我查看错误消息时,它还告诉我无法执行此代码:
public UserService() {
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdatenbank", "root", "adminadmin123");
} catch (SQLException e) {
e.printStackTrace();
}
}
我不知道为什么会这样... MySQL Java 连接器 JAR 文件位于 "Referenced Libraries"。顺便说一句,我使用 Tomcat 9、MySQL 8、JDK 13 和 Maven (Jersey Servlet)。
在建立连接之前,您必须加载您的驱动程序:
Class.forName("com.mysql.jdbc.Driver");
并且对应的JAR必须在你的类路径中(在你的服务器的lib中或者打包在你的WAR文件中)。
无论如何我建议你使用 connection's pool 而不是 DriverManager
The main benefits to connection pooling are:
- Reduced connection creation time.
Although this is not usually an issue with the quick connection setup that MySQL offers compared to other databases, creating new JDBC connections still incurs networking and JDBC driver overhead that will be avoided if connections are recycled.
- Simplified programming model.
When using connection pooling, each individual thread can act as though it has created its own JDBC connection, allowing you to use straightforward JDBC programming techniques.
- Controlled resource usage.
If you create a new connection every time a thread needs one rather than using connection pooling, your application's resource usage can be wasteful, and it could lead to unpredictable behaviors for your application when it is under a heavy load.
由于您使用的是 Maven,只需在 pom.xml 文件中添加依赖项即可。
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
只需转到 Maven 存储库并搜索适合您的版本。 或者,如果您已经下载了 jar 文件,则将其添加到您的项目类路径中。
编辑 -> Class.forName() is not needed since JDBC 4.0.