javasesameopenrdf

How do I execute a SPARQL update on a Sesame RDF repository using a Java API?


Please, I have a a code and would want to update the repo with the new triples but the execute method gave some errors:

Below is my code:

public void temporalsendRIG(RepositoryConnection con) {
        ArrayList<String> userInputArrayList = new ArrayList<String>();
        populateArrayList(userInputArrayList);
        try {
            con = makeConnection(RemoteServer_url, Repository_id);
            String updateQuery = buildUpdateQuery(userInputArrayList);
            System.out.println(updateQuery);
            Update update = con.prepareUpdate(QueryLanguage.SPARQL,
                        updateQuery);
                        update.execute(); 


        } catch (MalformedQueryException e) {
            e.printStackTrace();
        }catch (RepositoryException e) {
            e.printStackTrace();
        }catch(UpdateExecutionException e){
            e.printStackTrace();
        }

    }

and below is the updateQuery string I am parsing:

public String buildUpdateQuery(ArrayList<String> arr){
    String updateQuery = 
        "PREFIX owl:<http://www.w3.org/2002/07/owl#> \n" + 
        "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
        "DELETE \n" +
        "{ \n" +
             "?s ?p ?o.\n" +

            "}\n" +
            "INSERT \n" +
            "{\n" +
            "?s \n" +
                    "<http://localhost:9090/reservationService/onto/Reservation/bookerFirstName>" + "\"" + arr.get(0) + "\" ; \n"  +
                    "<http://localhost:9090/reservationService/onto/Reservation/cityOfInterest>" + "\"" + arr.get(1) + "\" ; \n" +
                    "<http://localhost:9090/reservationService/onto/Reservation/distanceToCityOfInterest>"  + "\"" + arr.get(2) + "\" ;\n" +
                    "<http://localhost:9090/reservationService/onto/Reservation/maximumShift>" + "\"" + arr.get(3) + "\" ; \n"  +
                    "<http://localhost:9090/reservationService/onto/Reservation/requiredAmountOfDays>" + "\"" + arr.get(4) + "\" ; \n" +
                    "<http://localhost:9090/reservationService/onto/Reservation/requiredBedrooms>" + "\"" + arr.get(5) + "\" ; \n" +
                    "<http://localhost:9090/reservationService/onto/Reservation/requiredDistanceToLake>"  + "\"" + arr.get(6) + "\" ; \n" +
                    "<http://localhost:9090/reservationService/onto/Reservation/requiredOccupacy>"  + "\"" + arr.get(7) + "\" ; \n" +
                    "<http://localhost:9090/reservationService/onto/Reservation/startingBookingDay>" + "\"" + arr.get(8) + "\" . \n" +

            "} \n" +
            "WHERE \n" +
            "{ \n" +
                " ?s rdf:type <http://sswapmeet.sswap.info/sswap/Subject>. \n" +
                " ?s ?p ?o. \n" +
                " FILTER NOT EXISTS  { ?s rdf:type  ?o  } \n" +
                 "FILTER NOT EXISTS  { ?s  <http://sswapmeet.sswap.info/sswap/mapsTo>  ?o  } \n" +
            "} \n"
             ;


            return updateQuery;
}

Finally, here is the error I am getting when I run the code :

May 15, 2014 3:17:13 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused
May 15, 2014 3:17:13 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
May 15, 2014 3:17:13 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused
May 15, 2014 3:17:13 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
May 15, 2014 3:17:13 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: I/O exception (java.net.ConnectException) caught when processing request: Connection refused
May 15, 2014 3:17:13 PM org.apache.commons.httpclient.HttpMethodDirector executeWithRetry
INFO: Retrying request
org.openrdf.repository.http.HTTPUpdateExecutionException: Connection refused
    at org.openrdf.repository.http.HTTPUpdate.execute(HTTPUpdate.java:70)
    at org.henry.queryUpdate.temporalsendRIG(queryUpdate.java:63)
    at org.henry.queryUpdate.main(queryUpdate.java:138)
Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:280)
    at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:80)
    at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:122)
    at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
    at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.open(MultiThreadedHttpConnectionManager.java:1361)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
    at org.openrdf.http.client.HTTPClient.sendUpdate(HTTPClient.java:387)
    at org.openrdf.repository.http.HTTPUpdate.execute(HTTPUpdate.java:57)
    ... 2 more

Solution

  • Problem solved! I had a typo in the repository_url. The code is perfectly fine as shown in the question. The only problem was I was calling localhost:8080.. when my locahost port was 9090. It was due to some copying error. I had copied the code fragment from the main project to test and forgot to change the port number correctly. Thanks