Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I connect to my database with spring and test it?

I am trying to create a connection for my spring project, soemthing very simple.

I want to create the configuration fields on my application.properties. I tried with the spring.datasource and I dont get any error, but still, I dont get any info, just null pointers... I think the connections have not been made properly.

Here is part of my pom, with this dependencies:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.2.1.RELEASE</version>
    </dependency>

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

And my properties:

server.port=80
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/database
jdbc.username=user
jdbc.password=password

I dont get any error, but when I query the database, I always get a null pointer, which makes me think is not working properly, any idea how to configure this?

Thanks

EDIT:

My configuration class

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

    }

}

The controller

@Autowired
private MyService myService;


@RequestMapping("/")
public String index() {
    User myUser = myService.findUserById(1L);
    System.out.println(myUser);
    return myUser.getFirstName();
}

My Service Implementation

@Service
public class MyServiceImpl implements MyService {

    @Autowired
    UserRepository userRepository;


    public User findUserById(Long id){
        return userRepository.findOne(id);
    };
}

My Entity

@Entity
public class User {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;


    public Long getId() {
        return id;
    }

    public void setId(Long 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;
    }

}

And my repository

public interface UserRepository extends CrudRepository<User, Long> {

    //User findOne(Long id); this is a default method
}

THE ERROR:

I always get null pointer, even if data exist... and if I change the database password for a wrong one, I dont get any error.. like if it would be ok.

like image 809
jpganz18 Avatar asked Sep 05 '15 23:09

jpganz18


2 Answers

1) Please remove below dependency from pom, because of it might let SpringBoot use h2 instead of mysql, which cause you don't see any error even with wrong mysql password.

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>

2) In application.properties, please use:

spring.datasource.url=jdbc:mysql://localhost:3306/database
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

These property names can be found from SpringBoot document: http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html:

like image 53
JM Yang Avatar answered Nov 18 '22 17:11

JM Yang


The correct data source url is:

jdbc:mysql://HostName:Port(3306 is the default)/DBName
like image 40
SwEngin Avatar answered Nov 18 '22 19:11

SwEngin