3

I have a few problems starting up the spring guide on how to access mysql data (see this link: https://spring.io/guides/gs/accessing-data-mysql/). I adjusted the classes slightly, so I have this code:

  1. pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>org.springframework</groupId>
     <artifactId>gs-mysql-data</artifactId>
     <version>0.1.0</version>
     <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
    </parent>
    <dependencies>
    <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
    <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <!-- Use MySQL Connector-J -->
    <dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
    </dependency>
    <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    </dependencies>
    <properties>
     <java.version>1.8</java.version>
    </properties>
    <build>
    <plugins>
     <plugin>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-maven-plugin</artifactId>
     </plugin>
    </plugins>
    </build>
    

I am using this mainController: MainController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.resource.iPbackend.UserRepository;
import com.resource.iPbackend.User;
@Controller
@RequestMapping(path = "/main")
public class MainController {
 @Autowired
 private UserRepository userRepository;
 @RequestMapping(path = "/reg", method = RequestMethod.POST)
 public @ResponseBody String regNewUser (@RequestParam String firstName, @RequestParam String lastName, @RequestParam String email, @RequestParam String password, @RequestParam String username) {
 User n = new User();
 n.setFirstName(firstName);
 n.setLastName(lastName);
 n.setEmail(email);
 n.setPassword(password);
 n.setUsername(username);
 userRepository.save(n);
 return "User is stored in database: " + n;
 }
 @GetMapping(path = "/all")
 public @ResponseBody Iterable<User> getAllUsers() {
 return userRepository.findAll();
 }
}

Together with this repository: UserRepository.java

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.resource.iPbackend.User;
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
}

And this Entity: User.java

import org.springframework.data.annotation.Id;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@Entity
public class User {
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private Integer id;
 private String firstName;
 private String lastName;
 private String email;
 private String password;
 private String username;
 public Integer getId() {
 return id;
 }
 public void setId(Integer id) {
 this.id = id;
 }
 public String getFirstName() {
 return firstName;
 }
 public void setFirstName(String firstName) {
 this.firstName = firstName;
 }
 public String getLastName() {
 return lastName;
 }
 public void setLastName(String lastName) {
 this.lastName = lastName;
 }
 public String getEmail() {
 return email;
 }
 public void setEmail(String email) {
 this.email = email;
 }
 public String getPassword() {
 return password;
 }
 public void setPassword(String password) {
 this.password = password;
 }
 public String getUsername() {
 return username;
 }
 public void setUsername(String username) {
 this.username = username;
 }
}

And finally I have this Application.class:

@EnableAutoConfiguration
@SpringBootApplication
public class IPbackendApplication {
public static void main(String[] args) {
 SpringApplication.run(IPbackendApplication.class, args);
}
}

I am getting this error:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalArgumentException: URL must start with 'jdbc' at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]

Thanks for your help!

davidxxx
132k23 gold badges231 silver badges228 bronze badges
asked Aug 16, 2017 at 20:54
1
  • The relevant code is missing: Where is your DB configuration for as used by spring? Commented Aug 16, 2017 at 20:58

3 Answers 3

9

Look at this message in the exception trace :

nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalArgumentException: URL must start with 'jdbc' at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)

You very probably don't provide an application.properties file that values the spring.datasource.url property.
Or else you don't value it correctly.

It should look like :

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

You can refer to the Spring Boot documentation.

answered Aug 16, 2017 at 21:03
Sign up to request clarification or add additional context in comments.

Comments

3

I received pretty similar error when try to test my dao layer.

@RunWith(SpringRunner.class)
@DataJpaTest
public class ItemRepositoryTest {
 @Autowired
 private ItemRepository itemRepository;
 @Test
 public void findAll() {
 List<Item> allItems = itemRepository.findAll();
 Assertions.assertThat(allItems).hasSize(9);
 }
}

The issue was resolved by adding:

@AutoConfigureTestDatabase(replace=AutoConfigureTestDatabase.Replace.NONE)

to the test class:

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace=AutoConfigureTestDatabase.Replace.NONE)
public class ItemRepositoryTest {...}
answered Jul 23, 2018 at 15:01

Comments

0

The last properties that you share have some errors to fix spring.datasource.data-username=myuser and spring.datasource.data-password=ThePassword

This is and example of how to properly configure the spring datasource

spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=myuser
spring.datasource.password=ThePassword

It is another example from Spring documentation

spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
answered Aug 17, 2017 at 1:21

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.