i am coding a maven project with spring,
the default mysql-connector is version 6.0.5, and whenever i run the app on server the stack trace tells me that "Could not load JDBC driver class [com.mysql.jdbc.Driver]".
So i add as external library mysql-connector downloaded from mvnrepository.com version 5.1.40, add the dependency code to pom.xml and then it works!
How can i fix it using v 6.0.5?
thanks
If I understand you correctly you downloaded manually mysql connector, and added it as library.
Add dependency in pom.xml and reimport maven dependencies.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>
You should checkout if your application.properties are set correctly, e.g:
spring.datasource.url=jdbc:mysql://localhost/jpa_example
spring.datasource.username=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
Related
My Spring Boot Project trying connect to MYSQL database with driver mysql-connector-java.
I have import newest mysql driver and spring-boot-starter-data-jpa
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
I have configured database connection in application.properties file
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=somethingfunny
spring.datasource.password=somethingfunny
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
MYSQL version is 8.0.26
Spring boot version 2.6.2
When run project with Intellij I get error
Caused by: org.springframework.beans.BeanInstantiationException:
Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory
method 'dataSource' threw exception; nested exception is
java.lang.IllegalStateException: Cannot load driver class:
com.mysql.cj.jdbc.Driver at
org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
~[spring-beans-5.3.14.jar:5.3.14] at
org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
~[spring-beans-5.3.14.jar:5.3.14] ... 35 common frames omitted Caused
by: java.lang.IllegalStateException: Cannot load driver class:
com.mysql.cj.jdbc.Driver at
org.springframework.util.Assert.state(Assert.java:97)
~[spring-core-5.3.14.jar:5.3.14] at
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:241)
~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:193)
~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at
org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:48)
~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at
org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:90)
~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) ~[na:na] at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
~[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:566) ~[na:na]
at
org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
~[spring-beans-5.3.14.jar:5.3.14] ... 36 common frames omitted
I have refered the post about Cannot load driver class: com.mysql.jdbc.Driver (NOT com.mysql.cj.jdbc.Driver), I cannot apply for my project because my project get error when using com.mysql.cj.jdbc.Driver but not com.mysql.jdbc.Driver.
I also refered this post Cannot load driver class: com.mysql.cj.jdbc.Driver. But i can not find correct answer(the answer is marked corrected) for this error.
How to fix this error ?
1) Use this dependency in pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
2) Use this property in your application.properties file
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
I have fix this issue. In Intellij right click to project, choose maven, choose reload project. Now Intellij will add mysql driver to the project
Going off of one of the above answers https://stackoverflow.com/a/70514438/14111809, but with a Gradle instead of Maven (in IntelliJ IDE), this fixed the issue for me:
Have you checked which version of MySQL connector do you have?
Since you haven't specified the version in your pom.xml, there is a chance that it pulled version 5 and now it is complaining about that .cj which is required for version 8.
That might be the reason why is it working without .cj ( com.mysql.jdbc.Driver ), because it pulled the version 5.
Manually add the version in your pom.xml and keep .cj as it is.
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
After that make sure that you pull your dependencies.
But make sure that you check in your project if you do have ONLY version 8 after "mvn clean install".
As of the picture that you shared "mysql version", that means nothing, the exception is regarding mysql-connector jar, it has nothing to do with the workbench, you can still have workbench version 5, and use mysql-connector jar version 8, it will make no difference.
In any version of mysql workbench ( 5 or 8 ):
mysql-connector 8 jar = requires .cj
mysql-connector 5 jar = does not require .cj
The only thing that you need to do is define the version of mysql-connector-java in your pom.xml
Fix the line that sets jdbc driver class as below: I don't think it matters but driver-class-name is more common. And check if your build file is up to date. Might as well try re-building using pom.xml
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
I'm about to rewrite a Struts 2 webapp with SpringBoot & Spring Web.
How can I get started? I'm using start.spring.io with three dependencies: Spring Web, MyBatis Framework and MySQL Driver
I've opened the project in IntelliJ, built the project (no problem) but can't run the webapp as it fails to connect to my MySQL instance. Sure I realise that I'll need to provide DB URL, username, password, port, etc. but there's no configuration files included in the zip. Where do I add these so I can properly set the properties?
How can I get started with an up-to-date version of Spring Boot that includes the basic dependencies I need?
It's so frustrating to be hamstrung at the first hurdle when I know I'll be able to run the rest of the way once I get over this first step!
Thank you.
You can find a file named "application.yml" or "application.properties" in directory below.
src/main/resources/
So, you have to go into that, and put the configs there.
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name =com.mysql.jdbc.Driver
#spring.jpa.show-sql: true
Also, check to make sure you have the required dependencies added. You'll probably need these dependencies Spring Web, Spring Data JPA, and MySQL Driver.
For example, in case you're using Maven. You can go check the dependencies section of POM.XML.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.17.Final</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
<scope>runtime</scope>
</dependency>
I highly recommend you to check this link.
My JavaFx application is running perfectly well from source but When I'm compiling to a single jar file I get error :
Error: JavaFX runtime components are missing, and are required to run this application.
I'm using Maven as my repository manager and My install with Maven is sucessfull.
Note: In my Intellij build artifact I can see that Intellij include JavaFx and all its libraries
a) For my maven project the trick was to use an extra starter class that does not inherit from javafx.application.Application:
public class GUIStarter {
public static void main(final String[] args) {
GUI.main(args);
}
}
The JavaFx dependencies are included in my pom.xml as follows:
<!-- JavaFx -->
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-base</artifactId>
<version>12</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>12</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics </artifactId>
<version>12</version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>12</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>12</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>12</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-swing</artifactId>
<version>12</version>
</dependency>
In order to include fxml files in your build, you might need to define them as resource:
!-- define JavaFx files as resources to include them in the jar -->
<resource>
<directory>${basedir}/src/main/java/foo/baa/view</directory>
<filtering>false</filtering>
<targetPath>foo/baa/view</targetPath>
</resource>
b) As an alternative, an extra maven plugin "javafx-maven-plugin" could be used to build the javafx appication, see example project at https://openjfx.io/openjfx-docs/#maven (This did not work for me. I have several pom files and want to reference the javafx dependencies in a sub project. My main class could not be found by the javafx-maven-plugin.)
In Java 11 the JavaFX components have been removed into their own SDK, so when you run it won't be able to find them in the runtime.
The JavaFX page has the instructions to get it going: https://openjfx.io/openjfx-docs/#install-javafx
In short, you have to compile / run by adding the javafx modules in, either as options passed on the command line, or using a modular project.
I found that the modular project with Maven and IntelliJ worked best for me. https://openjfx.io/openjfx-docs/#IDE-Intellij
In the modular method you have a module-info.java file that describes all the modules that your project "requires", and allows you to "open" them to other modules. If you have a bunch of Maven dependencies you have to add them in the requires list too though. (IntelliJ can make this easy - find the error about no requires in the code and alt-enter)
Once everything was working with modules etc, I had to make a Fat Jar using I think the Maven shade plugin, to put everything together. Then it would work running the jar from the command line.
However, after getting my Java 11 code ##$%## working after 2 days of pain, I went back to Java 8 (using correto SDK for latest version) as Java 11 doesn't have packaging and IntelliJ can't do it for you.
How I get the CXF version used on the follwowing maven dependency
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cxf</artifactId>
<version>2.16.5</version>
</dependency>
I got it using maven dependences by click on the camel-cxf dependency in my project to open the camel-cxf pom then switched to 'Effective POM'. There you can search for cxf-core dependency and get the version.
I'd like to install the JDBC connector using maven.
I have the following: mvn install:install-file -DgroupId=mysql -DartifactId=mysql-connnector-java -Dversion=5.1.6 -Dpackaging=jar -Dfile= -DgenerationPom=true
I think all I'm need is what I put on the other side of the =Dfile= ?
I haven't used maven in a while either, so I'm not sure what the file switch is used for?
Thanks for all the insight!
The "install-file" or "deploy-file" goals are used for installing or deploying artifacts to your local or internal repository that are not available from Maven Central or other external repositories that you may have configured.
If you've got access to Maven Central, simply adding the following to your project's pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
...should do the trick.
To answer your question though, the -Dfile= argument is for specifying the artifact that would actually be installed in the local repository.
lotz answer is right and that should be sufficient
But, If you want to use the latest version of the connector, you can check https://mvnrepository.com/artifact/mysql/mysql-connector-java
Add the following to your project's pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>