gpt4 book ai didi

mysql - 获取 JSON 的 URL 中的多个变量

转载 作者:行者123 更新时间:2023-11-29 23:48:03 25 4
gpt4 key购买 nike

我试图检索 URL 中的多个变量,因此我可以向数据库发送查询以选择特定对象,但它似乎选择了第一个对象,而其他对象为空。

@RequestMapping(value = "getGroup/{Name}/{StartDate}/{EndDate}/{Status_GroupID}", method = RequestMethod.GET)
public @ResponseBody List<GroupsDetails> getGroupList(
@PathVariable String Name, String StartDate, String EndDate,
Integer Status_GroupID) {

return musicStoreService.getGroupList(Name, StartDate, EndDate, Status_GroupID);
}

我收到的错误:

    Hibernate: select groupsdeta0_.Status_GroupID as Status1_1_, groupsdeta0_.GroupsID as GroupsID1_,        groupsdeta0_.EndDate as EndDate1_, groupsdeta0_.Name as Name1_, groupsdeta0_.StartDate as StartDate1_ from Groups groupsdeta0_ where groupsdeta0_.Name=Gamma and (groupsdeta0_.StartDate is null) and (groupsdeta0_.EndDate is null) and (groupsdeta0_.Status_GroupID is null)
Sep 12, 2014 2:41:40 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [SpringDispatcher] in context with path [/ATS] threw exception [Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'Gamma' in 'where clause'

该表名为 Groups,包含行:Name、StartDate、EndDate、Status_GroupID 和 GroupsID

更新:

我尝试通过提供行详细信息来检索主键,但它不起作用。

@Override
public List<GroupsDetails> getGroupList(String Name, String StartDate, String EndDate, Integer Status_GroupID) {
SessionFactory sessionFactory=HibernateSessionManager.getSessionFactory();
Session session=sessionFactory.getCurrentSession();
Transaction transaction=session.beginTransaction();
try{
transaction.begin();
@SuppressWarnings("unchecked")
List<GroupsDetails> groupList=session.createSQLQuery("FROM Groups WHERE Name=" + Name + " AND StartDate=" + StartDate + " AND EndDate=" + EndDate + " AND Status_GroupID=" + Status_GroupID).list();
return groupList;
}finally{
session.close();
}

}

但是它抛出了语法错误的异常,即使我看不到语法错误。错误是:

Hibernate: FROM Groups WHERE Name=Gamma AND StartDate=01-06-2014 AND EndDate=01-09-2014 AND Status_GroupID=1
Sep 12, 2014 3:35:51 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [SpringDispatcher] in context with path [/ATS] threw exception [Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'FROM Groups WHERE Name=Gamma AND StartDate=01-06-2014 AND EndDate=01-09-2014 AND' at line 1

最佳答案

根据错误详细信息:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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
'FROM Groups WHERE Name=Gamma AND StartDate=01-06-2014 AND EndDate=01-09-2014 AND' at line 1

它清楚地表明您缺少引号,例如:Name=Gamma 应该是 Name='Gamma'

或者,您可以使用 Parameters 创建查询例如:

query = sess.createSQLQuery("SELECT * FROM Groups 
WHERE NAME=:name AND StartDate=:startDate AND EndDate=:endDate
AND Status_GroupID=:status_GroupID").addEntity(Groups.class);

query.setString("name", name);
query.setDate("startDate", StartDate);
query.setString("endDate", EndDate);
query.setString("status_GroupID", Status_GroupID);


List<GroupsDetails> groupList = query.list();

关于mysql - 获取 JSON 的 URL 中的多个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25809919/

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