java.sql.SQLException : No suitable driver found for jdbc:mysql://localhost:3306/database

标签 java mysql database maven tomcat9

我一直在尝试为学校项目设置自己的休息 API。我决定使用 MySQL 作为数据库,并且想将它与我的 Web 服务连接,但显然我总是收到此错误消息: 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 文件位于“引用库”中。顺便说一句,我使用 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:

  1. 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.

  1. 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.

  1. 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.

关于java.sql.SQLException : No suitable driver found for jdbc:mysql://localhost:3306/database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60084064/

相关文章:

mysql - 在查询结果中显示数字字符串

java - 从两侧更新双向 ManyToMany

java - 附加到 CipherOutputStream - AES/CTR/NoPadding (Java)

mysql - Centos yum安装事务检查错误

Mysql - 递归平衡计算

database - 让我的文本框查看 LINQ 查询的结果

mysql - 如何在 SQL 中插入带有货币符号的货币值(value)?它显示超出范围的值

mysql - MariaDB/MySQL 电影数据显示为每日时间表 - 4 个时间段

java - Android MediaPlayer 已定稿但未发布

java - 为什么 Http Get 不返回 If-None-Match?