简体   繁体   中英

Query runs fine in database(derby) ,but when executed in java, an exception is thrown

My first question guys !

So my query works fine in the database, however when i call a methid that runs the query, an exception is thrown. below is the code.

 @Override
public String lastID() {
    String x = "SELECT TICKETNO FROM TICKET ORDER BY TICKETNO DESC";
    return (String) em.createQuery(x).getResultList().get(0);
}

this is part of the exception

Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.JPQLExceptionException Description: Syntax error parsing [SELECT TICKETNO FROM TICKET ORDER BY TICKETNO DESC].[28, 28]An identification variable must be provided for a range variable declaration.

As far as I can understand, your String variable "x" contains an SQL statement (since you ran it in the database). But when you create a Query with createQuery method, you have to use JPQL expression as parameter: Oracle Docs

So either you should change the content of the x variable to contain JPQL expression, or you should use the createNativeQuery method:

return (String) em.createQuery(x).getResultList().get(0);

I hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM