Sparql 查询代码在 Jena-Java 中不起作用



我在耶拿-fuseki-1.1.1中测试这个sparql查询,它正在工作,但是当我在耶拿Java项目中使用时,它没有给我output.inside,而循环没有执行。

private static void sparqlTst() {
        FileManager.get().addLocatorClassLoader(Jena_Enigma.class.getClassLoader());
        Model model = FileManager.get().loadModel("http://www.heshjayasinghe.webatu.com/Enigma.RDF");
         model.write(System.out, "RDF/JSON");
        String queryString = "PREFIX sep: <http://www.heshjayasinghe.webatu.com/Enigma.RDF#>"
                + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
                + "PREFIX strg: <http://www.w3.org/2001/XMLSchema#string#>"
                + "SELECT  ?first "
                + "WHERE {"
                + "?User  sep:email "heshjse@gmail.com"."
                + "?User sep:password "123"."
                + "?User sep:fname ?first. "
                + "}";
        Query query = QueryFactory.create(queryString);
      //  System.out.println(query);
        QueryExecution qexec = QueryExecutionFactory.create(query, model);
        try {
            ResultSet results = qexec.execSelect();
            System.out.println(results);
            while (results.hasNext()) {
                System.out.println("ok");
                QuerySolution soln = results.nextSolution();
                System.out.println(soln);
                Literal name = soln.getLiteral("x");
                System.out.println(name);
                System.err.printf("X is '%s'n", soln.getLiteral("first"));
            }
        } finally {
            qexec.close();
        }
    }

更正的答案

private static void sparqlTst() {
        FileManager.get().addLocatorClassLoader(Jena_Enigma.class.getClassLoader());
        Model model = FileManager.get().loadModel("http://www.heshjayasinghe.webatu.com/Enigma.RDF");
         model.write(System.out, "RDF/JSON");
        String queryString = "PREFIX sep: <http://www.heshjayasinghe.webatu.com/Enigma.RDF#>"
                + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
                + "PREFIX strg: <http://www.w3.org/2001/XMLSchema#string#>"
                + "SELECT  ?x "
                + "WHERE {"
                + "?User  sep:email "heshjse@gmail.com"."
                + "?User sep:password "123"."
                + "?User sep:fname ?x. "
                + "}";
        Query query = QueryFactory.create(queryString);
      //  System.out.println(query);
        QueryExecution qexec = QueryExecutionFactory.create(query, model);
        try {
            ResultSet results = qexec.execSelect();
            System.out.println(results);
            while (results.hasNext()) {
                QuerySolution soln = results.nextSolution();
                Literal name = soln.getLiteral("x");
                System.out.println(name);

            }
        } finally {
            qexec.close();
        }
    }

最新更新