Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Springboot could not extract ResultSet

Currently I am getting a problem with fetching mysql data for my springboot project:

There was an unexpected error (type=Internal Server Error, status=500). could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet

TestEntity.java

@Entity
public class TestEntity implements Serializable {

    @Id
    private int id;
    private String p1;
    private String p2;
    private String p3;

    public TestEntity() {
    }

    public TestEntity(int id, String p1, String p2, String p3){
        this.id = id;
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    public int getId() {
        return id;
    }

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

    public String getP1() {
        return p1;
    }

    public void setP1(String p1) {
        this.p1 = p1;
    }

    public String getP2() {
        return p2;
    }

    public void setP2(String p2) {
        this.p2 = p2;
    }

    public String getP3() {
        return p3;
    }

    public void setP3(String p3) {
        this.p3 = p3;
    }



}

TestService.java

@Service
public class TestService {

    @Autowired
    private TestRepository testRepository;

    public ArrayList<TestEntity> getAllTestEntities(){
       ArrayList<TestEntity> list = new ArrayList();
       testRepository.findAll().forEach(list::add);
       return list;
    }

    public Optional getTestEntity(int id){
       return testRepository.findById(id);
    }
    public void addTestEntity(TestEntity t){
        testRepository.save(t);
    }
    public void removeTestEntity(int index){

        testRepository.deleteById(index);

    }

}

TestRepository.java

@Repository("mysql")
public interface TestRepository extends CrudRepository<TestEntity,Integer> {



}

TestController.java

@RestController
public class TestController {

    @Autowired
    private TestService testService;

    @RequestMapping("/test/AllUnits")
    public ArrayList<TestEntity> getAllTestUnits(){
        return testService.getAllTestEntities();
    }

    @RequestMapping("/test/{id}")
    public Optional getAllTestUnit(@PathVariable int id){
        return testService.getTestEntity(id);
    }

    @RequestMapping(method=RequestMethod.POST,value = "/test" )
    public void addTestUnit(@RequestBody TestEntity t){
        testService.addTestEntity(t);
    }

    @RequestMapping(method=RequestMethod.DELETE,value = "/test/{id}" )
    public void deleteTestUnit(@RequestBody Integer id){
        testService.removeTestEntity(id);
    }

    @RequestMapping("/test/welcome")
    public String welcome(){

        return "welcome to springboot";

    }


}

Edit: application.properties

cloud.aws.region.auto=true
cloud.aws.region.static=us-east-2

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://alyxdev.czcdgqfkwsnr.us-east-2.rds.amazonaws.com:3306/CryptoCurrency
spring.datasource.username=*******
spring.datasource.password=*******

I am able to get the /test/welcome mapping working so I believe my implementation of the service and controller is correct. So I am wondering if I made a mistake for accessing my database in my repository or should I use a JpaRepository instead of a CrudRepository and use an explicit query?

Edit Stack Trace: org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'CryptoCurrency.test_entity' doesn't exist

like image 200
Sinlesssc Avatar asked Apr 14 '18 02:04

Sinlesssc


1 Answers

In you Entity class i.e. TestEntity.java, you need to specify which table that your referring to

@Entity
@Table(name="tbl_something")
public class TestEntity implements Serializable {

And use of CrudRepository would be fine for excessing the database. The application.properties file looks good to me.

like image 192
suman heuju Avatar answered Oct 04 '22 08:10

suman heuju