0

I'm having issues inserting a new node into exist-db using Java.

If I run this Xquery from eXide it will run fine and will insert the new department:

update insert < DEP_ROW >< DEPT_NO >1< /DEPT_NO >< DNOMBRE >A< /DNOMBRE >< LOC >A< /LOC >< /DEP_ROW > into /departamentos

However, if I try to do it from Java it says that there's a syntax error. The code is:

    String queryString = "update insert <DEP_ROW><DEPT_NO>1</DEPT_NO><DNOMBRE>A</DNOMBRE><LOC>A</LOC></DEP_ROW> into /departamentos";

    consulta = conn.prepareExpression(queryString);
    consulta.executeQuery();

And the error is:

Exception in thread "main" javax.xml.xquery.XQException:
XQJQS001 - Invalid XQuery syntax, syntax does not pass static 
validation.
Root Cause:
XQueryParseException: Encountered "insert" at line 1, column 8.
Was expecting one of:
<EOF> 
"%%%" ...
"=" ...
"," ...
"or" ...
"and" ...
"to" ...
"*" ...
"div" ...
"idiv" ...
"mod" ...
"union" ...
"|" ...
"intersect" ...
"except" ...
"instance" ...
"treat" ...
"castable" ...
"cast" ...
"!=" ...
"<=" ...
">" ...
">=" ...
"eq" ...
"ne" ...
"lt" ...
"le" ...
"gt" ...
"ge" ...
"is" ...
"<<" ...
">>" ...
"[" ...
"-" ...
"+" ...
"<" ...
"/" ...
"//" ...
"(" ...

at Visualizar.insertadep(Visualizar.java:58)
at Visualizar.main(Visualizar.java:23)
at Visualizar.insertadep(Visualizar.java:58)
at Visualizar.main(Visualizar.java:23)

I really appreciate your help.

Thanks,

  • I don't know eXist-db but I'd guess it should either be `update ...` or `insert ...` but not `update insert ...`. – Thomas Nov 15 '18 at 16:00
  • It is update insert because it does work within ExistDB command line. However it fails when doing it in Java. Even tough, I've already tried using only update or only insert and it doesn't work. – Yeray Benito García Nov 15 '18 at 16:04

1 Answers1

2

I was having the exact same problem as Yeray and came across this post. I have now found a solution, so I'm back to share my code, which I hope can help Yeray, or someone else.

public class InsertDepartment {

public static final String DRIVER = "org.exist.xmldb.DatabaseImpl";
public final static String URI = "xmldb:exist://localhost:8080/exist/xmlrpc";
public final static String COLLECTION = "/db/first_steps";
public final static String USERNAME = "admin";
public final static String PASSWORD = "";

public static void main(String[] args) throws Exception {
    String value = "";

    Class cl = Class.forName(DRIVER);
    Database database = (Database) cl.newInstance();
    DatabaseManager.registerDatabase(database);
    Collection col = DatabaseManager.getCollection(URI + COLLECTION, USERNAME, PASSWORD);

    int depNumber = 42;
    String depName = "Department 42";
    String depAddress = "42 Galaxy Avenue, Betelgueuse 23458 OH";

    String sQuery = "update insert <department><dep_number>" + depNumber
            + "</dep_number><dep_name>" + depName + "</dep_name><dep_address>"
            + depAddress + "</dep_address></department> into /departments";

    EXistXQueryService service = (EXistXQueryService) col.getService("XQueryService", "1.0");
    service.setProperty("indent", "yes");
    service.query(sQuery);
}

Note that for this code to work, I ended up with the following list of libraries in my project. Some may not be required for this code to work.

exist-xqj-1.0.1.jar
j8fu-1.21.jar
log4j-1.2-api-2.11.0.jar
log4j-api-2.11.0.jar
log4j-core-2.11.0.jar
log4j-jul-2.11.0.jar
log4j-slf4j-impl-2.11.0.jar
org.apache.commons.pool.jar
org-apache-commons-logging.jar
ws-commons-util-1.0.2.jar
xmldb-api-1.7.0.jar
xmlrpc-client-3.1.3.jar
xmlrpc-common-3.1.3.jar
xqj2-0.0.1.jar
xqjapi.jar
exist.jar
exist-optional.jar

Also, I had to add the file log4j2.xml to the src folder of my project. You can find a sample of this file in this post.

Fili
  • 31
  • 5