Java Springboot 无法提取 ResultSet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49827199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 03:08:23  来源:igfitidea点击:

Springboot could not extract ResultSet

javaspring-bootjpa

提问by Sinlesssc

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

目前我在为我的 springboot 项目获取 mysql 数据时遇到问题:

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

出现意外错误(类型=内部服务器错误,状态=500)。无法提取结果集;SQL [不适用];嵌套异常是 org.hibernate.exception.SQLGrammarException: 无法提取 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

测试服务.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

测试库.java

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



}

TestController.java

测试控制器.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

编辑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?

我能够使 /test/welcome 映射正常工作,因此我相信我对服务和控制器的实现是正确的。所以我想知道我是否在访问存储库中的数据库时犯了错误,还是应该使用 JpaRepository 而不是 CrudRepository 并使用显式查询?

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

编辑堆栈跟踪:org.springframework.dao.InvalidDataAccessResourceUsageException:无法提取结果集;SQL [不适用];嵌套异常是 org.hibernate.exception.SQLGrammarException:无法提取 ResultSet 引起:org.hibernate.exception.SQLGrammarException:无法提取 ResultSet 引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表 'CryptoCurrency.test_entity ' 不存在

回答by suman heuju

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

在您的实体类中,即 TestEntity.java,您需要指定您所指的表

@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.

并且使用 CrudRepository 可以很好地超出数据库。application.properties 文件对我来说看起来不错。

回答by Sinlesssc

I found the solution to the problem I was having apparently by renaming the table to all lowercase characters (test_table) in SQL and then using that table instead of Test_tableSpringboot was able to find that table and link map it to my entity class. I have no idea why it works this way. Maybe something to do with the Netbeans IDE I am using perhaps?

我通过test_table在 SQL中将表重命名为所有小写字符 ( ) 然后使用该表而不是Test_tableSpringboot找到了我显然遇到的问题的解决方案能够找到该表并将其链接映射到我的实体类。我不知道为什么它会这样工作。也许与我正在使用的 Netbeans IDE 有关?