gpt4 book ai didi

java - Spring Boot 无法提取 ResultSet

转载 作者:行者123 更新时间:2023-11-30 10:16:57 25 4
gpt4 key购买 nike

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

出现意外错误(类型=内部服务器错误,状态=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

@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";

}


}

编辑: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=*******

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

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

最佳答案

在您的实体类中,即 TestEntity.java,您需要指定您引用的是哪个表

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

并且使用 CrudRepository 可以很好地处理超出数据库的情况。application.properties 文件看起来不错。

关于java - Spring Boot 无法提取 ResultSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49827199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com