gpt4 book ai didi

java - 无法在 Hibernate 中运行一段时间的 SQL 查询

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

此代码用于显示按月销售的天数

public long getCountsOfQuery(String condition, Class outputclass) throws Exception {
Session session = BuilderSession.getSession();
return Long.parseLong(session.createSQLQuery(condition).uniqueResult().toString());
}

我的 JSP 文件是:

<td><%= ho.getCountsOfQuery("select COUNT(*) from store_sales where date like '"+ fd.getPartDate(FormatDate.PART_YEAR)+fd.getPartDate(FormatDate.PART_MONTH)+"01"+"' ", object.getClass())  %></td>
<td><%= ho.getCountsOfQuery("select COUNT(*) from store_sales where date like '"+ fd.getPartDate(FormatDate.PART_YEAR)+fd.getPartDate(FormatDate.PART_MONTH)+"02"+"' ", object.getClass()) %></td>
....
<td><%= ho.getCountsOfQuery("select COUNT(*) from store_sales where date like '"+ fd.getPartDate(FormatDate.PART_YEAR)+fd.getPartDate(FormatDate.PART_MONTH)+"31"+"' ", object.getClass()) %></td>

两次运行后无法在 hibernate 中响应查询。
此外,运行时不会出现错误。请帮助我。

最佳答案

这是因为您没有释放连接。

  Session session = BuilderSession.getSession();
Long result=Long.parseLong(session.createSQLQuery(condition).uniqueResult().toString());
session.close();
return result;

显然,您应该将其包装在 try-catch 中并在 finally block 中关闭,但通常它会显示您缺少的内容。

关于java - 无法在 Hibernate 中运行一段时间的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51493767/

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