0

I have made a table persons in a schema name test_schema. I have connected the database from DATABASE > CONNECT TO DATABASE in mysql workbench. Then I tried to connect with that but that doesn't seem to work somehow. BTW I have cloned it from github and trying to run my database in here.

I have tried to see the status but that shows running but the data is not fetching from the database.

application.yml

  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/persons
    username: root
    password: users
  jpa:
    hibernate.ddl-auto: null
    generate-ddl: true
    show-sql: true```


UsersRepository.java

package com.techprimers.db.repository;

import com.techprimers.db.model.Users;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UsersRepository extends JpaRepository<Users, Integer> {
}

Users.java

package com.techprimers.db.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Users {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private Integer id;
    @Column(name = "token")
    private String token;
    @Column(name = "name")
    private String name;
    @Column(name = "post")
    private Integer post;

    public Users() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getPost() {
        return post;
    }

    public void setPost(Integer post) {
        this.post = post;
    }


}

UsersResources.java

package com.techprimers.db.resource;

import com.techprimers.db.model.Users;
import com.techprimers.db.repository.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/rest/users")
public class UsersResource {

    @Autowired
    UsersRepository usersRepository;

    @GetMapping(value = "/all")
    public List<Users> getAll() {
        return usersRepository.findAll();
    }

}

SpringBootMysqldbApplication.java

package com.techprimers.db;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories(basePackages = "com.techprimers.db.repository")
@SpringBootApplication
public class SpringBootMysqldbApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMysqldbApplication.class, args);
    }
}

The result should have been the table elements but instead the Whitelabel Error Page is showing. I don't know what I have been doing wrong since there is nothing much to do. I just modified the code according to my needs but I think I somehow messed with the database connection or I am doing the database connection wrong. Thanks in advance.

SAM SAMULE
  • 35
  • 1
  • 4
  • I hope that your database credentials are correct. try adding @Repository annotation above your UsersRepository class. otherwise you can't autowire UsersRepository. – Vimukthi Jun 05 '19 at 05:39
  • @Vimukthi_R Still not working . My database credentials are right there is no problem regarding that. The username and password are correct. – SAM SAMULE Jun 05 '19 at 05:43
  • Are you getting any error or exception ? – Vimukthi Jun 05 '19 at 05:48
  • @Vimukthi_R Nothing,it is running and executing like there is no error . Only the response is not the expected one. – SAM SAMULE Jun 05 '19 at 06:08

2 Answers2

0

Unfortunately I can't comment yet but I think your database enforces ssl by default Since MySQL 5.7.5, but it is not obligatory for clients by default. Check this url please jdbc:mysql://localhost:3306/persons?useSSL=false

As Avi mentioned, If your database is test_schema write it in the url instead of persons

And please, provide errors and exceptions right in the question if there any

Link how to handle ssl in mysql

Stackoverflow spring and mysql ssl question

Eugene Kortov
  • 445
  • 6
  • 17
0

I have come to notice that you said TABLE NAME IS "PERSONS", but in url you have mentioned that as DB name

jdbc:mysql://${DB_SERVER_NAME}:${DB_PORT}/${DB_DATABASE}:currentSchema=${DB_SCHEMA};

which is jdbc:mysql://localhost:3306/<db-name>

Check that once

Avi
  • 1,458
  • 9
  • 14