我们如何修复“java.sql.SQLSyntaxErrorException:
how we can fix "java.sql.SQLSyntaxErrorException:
我想通过 api 显示订单列表,但我们在 DAO SQLSyntaxErrorException 中遇到错误。
@RequestMapping("list")
public String getAllOrders() {
//APIResponse response=new APIResponse();
List<OrderBeans> orderList = orderDao.selectAll();
return new Gson().toJson(orderList);
}
public List<OrderBeans> selectAll() {
System.out.println("DAO => " + jdbcTemplate);
List<OrderBeans> orders = null;
String query = "select * from " + TABLE_ORDER +"";
try {
orders = jdbcTemplate.query(query, new OrderRowMapper());
} catch (EmptyResultDataAccessException | IncorrectResultSetColumnCountException e) {
}
return orders;
}
3-May-2019 15:01:15.997 SEVERE [http-nio-8084-exec-109] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [/Grocery] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [select * from order]; nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order' at line 1] with root cause
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order' at line 1
我在这里推测你的错误是由于你的 SQL table 被调用 ORDER
,这当然是几乎所有版本的 SQL 的保留关键字].您应始终避免使用保留关键字命名您的 table 和列。作为解决方法,您可以按如下方式构建查询,在 table 名称周围放置反引号:
String query = "select * from `" + TABLE_ORDER + "`";
但是,在您有机会修复您的数据模型之前,以上内容只能被视为临时解决方案。
我想通过 api 显示订单列表,但我们在 DAO SQLSyntaxErrorException 中遇到错误。
@RequestMapping("list")
public String getAllOrders() {
//APIResponse response=new APIResponse();
List<OrderBeans> orderList = orderDao.selectAll();
return new Gson().toJson(orderList);
}
public List<OrderBeans> selectAll() {
System.out.println("DAO => " + jdbcTemplate);
List<OrderBeans> orders = null;
String query = "select * from " + TABLE_ORDER +"";
try {
orders = jdbcTemplate.query(query, new OrderRowMapper());
} catch (EmptyResultDataAccessException | IncorrectResultSetColumnCountException e) {
}
return orders;
}
3-May-2019 15:01:15.997 SEVERE [http-nio-8084-exec-109] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [dispatcher] in context with path [/Grocery] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [select * from order]; nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order' at line 1] with root cause
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order' at line 1
我在这里推测你的错误是由于你的 SQL table 被调用 ORDER
,这当然是几乎所有版本的 SQL 的保留关键字].您应始终避免使用保留关键字命名您的 table 和列。作为解决方法,您可以按如下方式构建查询,在 table 名称周围放置反引号:
String query = "select * from `" + TABLE_ORDER + "`";
但是,在您有机会修复您的数据模型之前,以上内容只能被视为临时解决方案。