spring.freemarker.cache:false
spring.datasource.url=jdbc:mysql://localhost/mydb
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
im getting this error even with the driver-class-name difined
java.lang.IllegalStateException: Cannot load driver class:
com.mysql.jdbc.Driver at
org.springframework.util.Assert.state(Assert.java:392)
~[spring-core-4.2.1.RELEASE.jar:4.2.1.RELEASE] at
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:153)
~[spring-boot-autoconfigure-1.3.0.M5.jar:1.3.0.M5] at
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:119)
~[spring-boot-autoconfigure-1.3.0.M5.jar:1.3.0.M5] at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
~[na:1.8.0_60] at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
~[na:1.8.0_60] at ......
and it goes on
If you're using Maven, add this to your pom.xml:
(Recommended) For MySQL 5.6, 5.7, 8.x and Java >= 8 use;
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
(Legacy) For MySQL <= 5.5 or Java <= 7 or JDBC < 4.2 use;
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
More details on Connector/J versions
It might be that you are using the deprecated driver class name. I solved it by updating the property in application.properties file.
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
Just confirm the following things.
If you are under proxy make sure use VPN to connect to internal servers, iif you are accessing DEV/STG servers
Make sure you add the following dependency inside correct location, like this
<dependencies> <--- inside this section
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
and Not in
`<build> <--- Not this section
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
`
3. And you don't need to use the following line
`spring.datasource.driver-class-name=com.mysql.jdbc.Driver.
The spring.datasource.url=jdbc:mysql://xxxxx` automatically know which driver to fetch.
If you are using IDEA and maven, you might forget reloading all maven projects, which causes the dependencies don't have the MySQL connector jar. And that's why when u run the application, there will be the error remind "com.mysql.cj.jdbc.Driver".
The crux of the problem is that you're missing MySQL driver dependency.
One of the ways, as outlined by other answers, is to specify it in your build tool's configuration. However, if you do not want to do it and are using IntelliJ IDEA (though I'm pretty sure Eclipse has something similar), you can also add the dependency via it. Steps:
Download desired jar from maven repository
Open your project in Intellij IDEA
File -> Project Structure -> Libraries
Click New Project Library (green plus sign on the left), or press Alt + Insert keys
Select Java
Select your jar
Press OK
Should look something like this:
Now you should be able to use the dependency you have just added.
Tested with IntelliJ IDEA 2017.3.4.
I will share more causes of this error:
I already seen this error many times using Intellij IDEA and running some integration test. The test fail with the message: Caused by: java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver. After rebuild the project (Build > Rebuild Project) the error disappears.
Another cause of this error is a corrupted JAR. A college was receiving this error (also trying to run integration tests) and, after delete some related jars on .m2 directory, the error was gone.
build.gradle
runtimeOnly 'mysql:mysql-connector-java'
application.yml
spring:
application:
name: apevent
version: 1.0
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/applicaiton
Probably someone still needs an answer (as me before) I solved it by adding the following dependency
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
I encountered the same problem now and solved it after providing the required dependency,
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
adding the following dependency solved my issue
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
I know this might be a little late but maybe anyone from the future might find this answer helpful.
This problem arises from potentially three reasons
Incorrect maven or Gradle configuration
Incorrect spring configuration in the application.properties file
Wrong class name on the application.properties file.
To troubleshoot if you are using maven. do not include the version name. This mostly is a recipe for problems, especially in huge applications. use maven autocompletes features to save you a little time.
configure the data source correctly. There are a lot of answers on it to this question. make sure it's correct. sometimes there is a cj that is missing or included where it's not supposed to be. Trial and error might help you discover the problem
if neither of the above options works. Remove spring.datasource.driver-class-name=com.mysql.jdbc.driver completely. I don't know if it magic but this last one worked for me.
GO NERDS!!!
My problem is solved just by refresh the project, perhap the dependency did not load by the time you run the application
I had the same issue. It happened because I forgot to declare the MySQL connector dependency in my pom file. I added the dependency, then boom! The error went away.
For People who uses Gradle 8.0 and beyond. I had to use
implementation 'mysql:mysql-connector-java:8.0.26'
instead of
implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.26' ,
my application.yml looks like:
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/<Your DB name>?useSSL=false
remember to reload your gradle file
Related
I have mysql-connector-java added as dependency, but I cannot use the PacketTooBigException inside my code. It says 'not able to resolve' .
I am writing some jdbc template based update logic, where I wish to capture instance of PacketTooBigException and retry with a smaller batch.
Ok, I found what was causing the error: I had runtime scope for the maven dependency. Removing it solved my problem.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope> <-- this caused the problem
</dependency>
The error I am getting is PY4JAVAERROR :
an error occurred while calling o313.load: Java.lang.classnotfoundexception: com.mysql.jdbc.Driver
has anyone encountered this before?
Place mysql-connector jar in your classpath.
That occurs when you don't have a MySQL connector in your classpath. If you are using a maven project you can try adding the dependency to your pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
If you are not using a dependency management tool like Maven or Gradle you can download the jar online and then add it to your classpath. Here is the link.
https://dev.mysql.com/downloads/connector/j/
Remember to do a bit of research for the correct versions before you download.
Either of the two options will definitely work.
It seems the mysql connectivity library is not included in the project. Solve the problem following one of the proposed solutions:
MAVEN PROJECTS SOLUTION
Add the mysql-connector dependency to the pom.xml project file:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version> // your desired version
</dependency>
Here you are all the versions: https://mvnrepository.com/artifact/mysql/mysql-connector-java
ALL PROJECTS SOLUTION
Add the jar library manually to the project.
Right Click the project -- > build path -- > configure build path
In Libraries Tab press Add External Jar and Select your jar.
You can find zip for mysql-connector here
Explanation:
When building the project, java throws you an exception because a file (the com.mysql.jdbc.Driver class) from the mysql connectivity library is not found. The solution is adding the library to the project, and java will find the com.mysql.jdbc.Driver
I am trying to create a maven version of the gradle-based treehouse 'unit-testing-a-spring-application' project used in this course. See course code here.
After not being able to build initially, I ended up adding dependency versions specified in the original gradle build file. Maven will build fine, but running the app gives this:
The following method did not exist:
org.springframework.data.repository.config.RepositoryConfigurationSource.getAttribute(Ljava/lang/String;)
Ljava/lang/String;
The method's class, org.springframework.data.repository.config.RepositoryConfigurationSource, is
available from the following locations:
jar:file:/C:/Users/home/.m2/repository/org/springframework/data/spring-data-
commons/2.1.6.RELEASE/spring-data-commons-2.1.6.RELEASE.jar!/org/springframework/data/repository/config/RepositoryConfigurationSource.class
It was loaded from the following location:
file:/C:/Users/home/.m2/repository/org/springframework/data/spring-data-commons/2.1.6.RELEASE/spring-
data-commons-2.1.6.RELEASE.jar
Action
Correct the classpath of your application so that it contains a single, compatible version of
org.springframework.data.repository.config.RepositoryConfigurationSource.
Several similar problems are solved here on S.O. by changing the version of some related dependency. I have tried various versions of the following to no avail (I stick normally with spring-boot-starter-parent v2.1.4 cause it works all the time with my projects):
spring-data-commons
spring-boot-starter-parent
I see the needed class below listed in my project Dependencies directory, but what dependency versions will work in this case? How can I solve this otherwise?
org/springframework/data/repository/config/RepositoryConfigurationSource.class
I looked at your code and found some mistakes. Since your project is a spring-boot one, you mustn't add direct spring dependencies in your pom file. Instead try to use spring-boot based dependencies. For instance, instead of having such this dependency:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.9.2.RELEASE</version>
</dependency>
You must have the following one in your pom file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
So, while learning Springboot, one of the things that came up was working with DBs and the Springboot Data JPA. After adding the simple dependency (with and without the version):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.5.8.RELEASE</version>
</dependency>
and trying to export/build a jar file, with either STS's export or the "mvn clean install"/"mvn clean package" I get the following:
https://pastebin.com/5qDSGejT
This also happens if the project was just created with STS or Spring Initializr and I really have no idea where to start figuring out what's wrong here.
Cannot determine embedded database driver class for database type NONE
The database driver is either missing or corrupt.
Or you simply have not setup a connection to a database.
I would suggest you to double check if your database connection is correct and go into your .m2 folder and delete your maven repositories.
You can easily setup a database connection using application.properties which is located in src/main/resources. Spring boot will take care of the rest.
spring.datasource.url=jdbc:mysql://localhost:3306/DBNAME?useSSL=false
spring.datasource.username = user
spring.datasource.password = pass
It was something with the database at the point of posting the question, but this is not the whole picture. I was getting a different stack trace yesterday. To reach it again, I added a dependency for a DB. Part of the second error was:
Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
It seems the problem lies in Java 9 (which I installed just yesterday) and the new "thing" with modules and Jaxb. So in the end, the solution was in another thread here, about jaxb not being included:
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.7.0</version>
</dependency>
This does fix the problem, but this is such a mess of information, it really is just a band-aid solution.
Now I am working with Microsoft SQLserver jdbc connectivity when I try to define class.forname
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
error occurs,
everything else is fine, but I dint have any idea about this error.
pom.xml:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4-2.0</version>
<scope>system</scope>
<systemPath>D:\my current work(please backup this folder)\backup\12-15-2016\milma_jishnu/src/main/lib/sqljdbc4-2.0.jar</systemPath>
</dependency>
Quoting Maven documentation:
System Dependencies
Important note: This is marked deprecated.
Dependencies with the scope system are always available and are not looked up in repository. They are usually used to tell Maven about dependencies which are provided by the JDK or the VM. Thus, system dependencies are especially useful for resolving dependencies on artifacts which are now provided by the JDK, but where available as separate downloads earlier. Typical example are the JDBC standard extensions or the Java Authentication and Authorization Service (JAAS).
So, <scope>system</scope> tells Maven: This is already present, you don't need to do anything.
Ergo, it is not added to the classpath, and you wonder why it's not there?
Seems you misunderstood the purpose of <scope>system</scope>. Don't use it, because the MS SQL JDBC driver is not part of the JDK.