Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect MySQL to Spring application

I want to use MySQL database rather than using runtime database like hsqldb. I have cloned this repository and it is using hsqldb as its database.

As I want to learn how to use relational database with rest based spring application. So I have made following changes to pom.xml: changed pom.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.springsource.restbucks</groupId>
    <artifactId>restbucks</artifactId>
    <packaging>war</packaging>
    <version>1.0.0.BUILD-SNAPSHOT</version>
    <name>Spring RESTBucks</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.5.RELEASE</version>
    </parent>

    <properties>
        <spring-data-releasetrain.version>Evans-RC1</spring-data-releasetrain.version>
        <tomcat.version>8.0.9</tomcat.version>
    </properties>

    <dependencies>

        <!-- Spring Data REST -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-entitymanager</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!-- JDK 8 DateTime support for Hibernate -->

        <dependency>
            <groupId>org.jadira.usertype</groupId>
            <artifactId>usertype.extended</artifactId>
            <version>3.2.0.GA</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>

        <!-- Database >

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

        <!-- Misc -->

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx-events</artifactId>
            <version>1.0.0.BUILD-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.14.4</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-libs-snapshot</id>
            <url>http://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-libs-snapshot</id>
            <url>http://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

and in application.properties (spring-restbucks/src/main/resources/application.properties), I have made following changes:

# JPA
spring.jpa.show-sql=true

server.port=8080

spring.datasource.url=jdbc:mysql://localhost/restBucks

spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.datasource.username=root

spring.datasource.password=

but I am facing about 15 errors like:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'orderInitializer' defined in file [/home/jimish/projects/spring_projects/spring/spring-restbucks/target/classes/org/springsource/restbucks/order/OrderInitializer.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springsource.restbucks.order.OrderInitializer]: Constructor threw exception; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

So if anyone can suggest path for how to connect mysql to spring application that would be great. Thanks.

like image 535
jimish Avatar asked Sep 19 '14 09:09

jimish


4 Answers

Include only the following configuration in your application.properties in classpath and it will work like a charm:

 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

This "spring.jpa.database-platform" specify which database you are going to use. It may be postgress, MySQL etc. According to that you need to specify it.

like image 98
gauravbhatt13 Avatar answered Sep 19 '22 15:09

gauravbhatt13


Expounding on @M. Deinum's comment... You need to specify the JPA configuration information as the Spring RestBucks App is using Spring Data JPA.

Adding standard JPA properties and specifying the database-platform (Common Application Properties) should get your JPA connection working.

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

# Enable spring data repos 
spring.data.jpa.repositories.enabled=true

# Replace with your connection string
spring.datasource.url=jdbc:mysql://localhost:3306/restbucks

# Replace with your credentials
spring.datasource.username=sa
spring.datasource.password=

spring.datasource.driverClassName=com.mysql.jdbc.Driver
like image 36
Edward J Beckett Avatar answered Sep 19 '22 15:09

Edward J Beckett


Add bellow properties in your application.properties file:

# Use one of create/update/create-update    
spring.jpa.hibernate.ddl-auto=update  
spring.datasource.url=jdbc:mysql://localhost:3306/db?createDatabaseIfNotExist=true   
spring.datasource.username=root  //your db user name  
spring.datasource.password=root  //your db user password 
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect  
#If your MySql version 5 then use MySQL5Dialect


# Enable spring data repos
spring.data.jpa.repositories.enabled=true
spring.jpa.show-sql=true
spring.jpa.database=mysql
like image 39
Tofazzal Hossain Avatar answered Sep 16 '22 15:09

Tofazzal Hossain


Add MySQL Connector/J to classpath:

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

This is an example of file application.properties inside Spring-based application:

datasource.mine.poolSize=30

spring.datasource.url=jdbc:mysql://127.0.0.1/vy?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
like image 36
Do Nhu Vy Avatar answered Sep 17 '22 15:09

Do Nhu Vy