简体   繁体   English

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

[英]sparql query code not working in jena-java

i test this sparql query in jena-fuseki-1.1.1 it is working but when i use in jena java project it is not give me output.inside while loop not executing. 我在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();
        }

    }

corrected answer 正确答案

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM