javaspringxquerybasexxqj

Exception when getting result of xquery


I want to execute simple xquery in java. But i get exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.xml.xquery.XQException:
  XQJFOS017 - Can not call getItemType() when Forward Only Sequences is not positioned on an item.
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

My dependecies

<groupId>org.basex</groupId>
<artifactId>basex</artifactId>
<version>7.3.1</version>

<groupId>net.xqj</groupId>
<artifactId>basex-xqj</artifactId>
<version>1.4.0</version>

My Bean

@Bean(name = "baseXDataSource")
public XQDataSource xqDataSource() throws XQException {
    XQDataSource ds = new BaseXXQDataSource();
    ds.setProperty("serverName", "localhost");
    ds.setProperty("port", "1984");
    ds.setProperty("user", "admin");
    ds.setProperty("password", "admin");
    ds.setProperty("databaseName", "2016");
    return ds;
}

My code

XQConnection conn = baseXDataSource.getConnection("admin", "admin");
        XQPreparedExpression expr = conn.prepareExpression("123");
        XQResultSequence result = expr.executeQuery();
        return result.getItemType().toString();

Any xquery, that i tryed to execute, gives same result. What is wrong? Thanks!


Solution

  • In XQJ an XQResultSequence is like a combination of a collection and an iterator. A method like getItemType() applies to the item at the "current position" in the collection. The current position is initially "before the first item". To find the item type of the first item in the collection, you first need to position the collection at the first item by calling the next() method.