如何解决 springframework no suitable driver found for jdbc:mysql

How do I resolve springframework no suitable driver found for jdbc:mysql

我是 java/spring 的新手,我正在使用 spring 框架编写应用程序

但我收到以下错误。

Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: No suitable driver found for jdbc:mysl://localhost/java_test
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:83)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:646)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:960)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:1015)
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:1025)
    at com.oshabz.lesson5.config.Repositories.ProductRepository.addProduct(ProductRepository.java:15)
    at com.oshabz.lesson5.config.Services.ProductService.addOneProduct(ProductService.java:15)
    at com.oshabz.lesson4.App.main(App.java:17)

Maven 依赖项

maven 依赖项获取 jdbc、mysql-连接器

  <dependencies>
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.15</version>  
    </dependency>    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>5.3.15</version>   
     </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>5.3.15</version>   
     </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.3.15</version>
    </dependency>  
     <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.3.15</version>    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.16</version>    </dependency>
  </dependencies>

项目配置

我在projectConfig文件上做了context配置

我也配置了jdbc配置。

@Configuration
@ComponentScan(basePackages = {"com.oshabz.lesson5.config.Repositories", "com.oshabz.lesson5.config.Services"})
public class ProjectConfig {
    
    @Bean
    public DataSource dataSource() {
        var dataSource = new DriverManagerDataSource();
        dataSource.setUrl("jdbc:mysl://localhost/java_test");
        dataSource.setUsername("root");
        dataSource.setPassword("");
        return dataSource;
    }
     
    @Bean
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(dataSource());
    }
    
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
        
    }
}

您的连接字符串有错误 (mysl):

应该是:

dataSource.setUrl("jdbc:mysql://localhost/java_test");

将连接字符串更改为:

spring.datasource.url=jdbc:mysql://localhost:3306/db_name

在您的 pom.xml 文件中添加以下内容:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

你写错了url:

dataSource.setUrl("jdbc:mysql://localhost/java_test");

并确保在您的服务器中使用合适版本的 mysql 编写依赖项:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>version_of_mysql</version>