Reputation: 3
I am unable to connect to mysql database with the below code
application.properties
spring.jpa.hibernate.ddl-auto = create
spring.datasource.url = com.mysql.jdbc.Driver
spring.datasource.driverClassName = jdbc:mysql://localhost:3306/MyDatabase
spring.datasource.username = root
spring.datasource.password = password
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
Getting the below error:
Caused by: java.lang.IllegalStateException: Cannot load driver class: jdbc:mysql://localhost:3306/MyDatabase
at org.springframework.util.Assert.state(Assert.java:97) ~[spring-core-5.3.16.jar:5.3.16]
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:241) ~[spring-boot-autoconfigure-2.6.4.jar:2.6.4]
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:193) ~[spring-boot-autoconfigure-2.6.4.jar:2.6.4]
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:48) ~[spring-boot-autoconfigure-2.6.4.jar:2.6.4]
at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:90) ~[spring-boot-autoconfigure-2.6.4.jar:2.6.4]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.16.jar:5.3.16]
... 41 common frames omitted
Someone please let me know what's the mistake in the code.
Upvotes: 0
Views: 4157
Reputation: 11
years later i'm back to java ,i got this error. I think it is an IDE error. I solved this by opening application.properties file in an external editor VSCode then saved it. Everything went fine. Mac users have these kind of errors more often.
Upvotes: 0
Reputation: 2504
I think you provided incorrect datasource url
and datasource driver class name
. Try the below one, hopefully it will work
spring.jpa.hibernate.ddl-auto = create
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/MyDatabase
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
spring.datasource.username = root
spring.datasource.password = password
Upvotes: 1