我从数据库获取数据时出错。看起来一切都很好,但我收到此错误?为什么?
完全错误
(org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: 0 near line 1, column 47 [from com.project.Data limit 0,10] (Encoded))
DataAction.java
/**
* To list Data
*
* @return String
*/
public String listThrowException() {
try{
//Getting Total records count from database...
pagination.setPreperties(facade.getDataCount());
//Getting data list from database
listData = facade.listData(pagination);
//Setting number of records in the particular page
pagination.setPage_records(listData.size());
logger.info(Logger.EVENT_SUCCESS,
"successfully viewed data list!");
}catch (Exception e) {
logger.error(
Logger.EVENT_FAILURE,
"could not view data list, error: *"
+ e.getMessage() + "*");
}
return "success";
}
DataService.java
@Override
public List<Data> listData(Pagination pagination){
List<Data> list=new ArrayList<Data>();
Query query;
try {
String excQuery = "from Data limit ";
excQuery = pagination.getSQLQuery(excQuery);
query = em.createQuery(excQuery);
list = query.getResultList();
} catch (Exception e) {
logger.error(Logger.EVENT_FAILURE, e.getMessage());
return null;
}
return list;
}
Pagination.java
public class Pagination {
private long page_size = 0;
private int page_number = 0;
private long total_records = 0;
private int page_records = 0;
private long start = 0;
private long end = 0;
private int total_pages = 0;
public void setPreperties(long l){
total_records = l;
total_pages = (int) Math.ceil(((double)l / (double)page_size));
start = ((page_size * page_number) - page_size);
end = page_size*page_number;
if(page_size==0){
start = 0;
end = l;
total_pages = 1;
}
}
public Pagination(int page_size, int page_number) {
this.page_number = page_number;
this.page_size = page_size;
}
public String getSQLQuery(String query) {
query += getStart() + "," + getEnd();
return query;
}
///get and set methods
}
我的问题是&#34;限制&#34;在Hql中它不受支持。我使用setFirstResults()setMaxResults()并且它工作。我的问题是在你的页面中实现paginaton的一个很好的例子。我参考本教程 http://prathap-puppala.blogspot.com/2011/06/struts-2-pagination-example.html我只更改DateService.java ** 祝你好运!
DateService.java
@Override
public List<Data> listData(Pagination pagination){
List<Data> list=new ArrayList<Data>();
Query query;
try {
String excQuery = "from Data ";
query = em.createQuery(excQuery).
setFirstResult(pagination.getStart()).setMaxResults(pagination.getEnd());
query = em.createQuery(excQuery);
list = query.getResultList();
} catch (Exception e) {
logger.error(Logger.EVENT_FAILURE, e.getMessage());
return null;
}
return list;
}