sparql查询代码在jena-java中不起作用

时间:2014-12-10 08:12:44

标签: java sparql jena

我在jena-fuseki-1.1.1中测试这个sparql查询它正在工作,但是当我在jena 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();
        }

    }

1 个答案:

答案 0 :(得分:0)

更正后的答案

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();
        }

    }
相关问题