Rumah > pangkalan data > tutorial mysql > 【Sesame】查询与修改数据

【Sesame】查询与修改数据

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Lepaskan: 2016-06-07 15:28:37
asal
1403 orang telah melayarinya

Sesame数据库利用Java API查询数据与修改数据。 1. 查询数据 获取查询数据(通过SPARQL) public static void testQuery() {try { RepositoryConnection con = repo.getConnection(); try { String queryString = PREFIX rk:http://rk.com/test/ + SELECT ?s

Sesame数据库利用Java API查询数据与修改数据。

1. 查询数据

获取查询数据(通过SPARQL)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

public static void testQuery() {

    try {

           RepositoryConnection con = repo.getConnection();

           try {

              String queryString = "PREFIX rk:<http://rk.com/test/> " +

                    "SELECT ?s ?o " +

                    "WHERE { " +

                    "?s rk:type rk:CreativeWork ." +

                    "?s ?p ?o ." +

                    "} ";

              TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);

              long startTime = System.currentTimeMillis();

              TupleQueryResult result = tupleQuery.evaluate();

              long secondTime = System.currentTimeMillis();

               

              List<String> bindingNames = result.getBindingNames(); //get the name of binded variables

              while (result.hasNext()) {

                 BindingSet bindingSet = result.next();

                 Value firstValue = bindingSet.getValue("s"); //bindingSet.getValue(bindingNames.get(0));

                 Value secondValue = bindingSet.getValue("o"); //bindingSet.getValue(bindingNames.get(1));

                 System.out.println(firstValue);

                 System.out.println(secondValue);

                 // do something interesting with the values here...

              }

              long endTime = System.currentTimeMillis();

              System.out.println("evaluation time = "+(secondTime-startTime));

              System.out.println("fetch time = "+(endTime-secondTime));

           }

           finally {

              con.close();

           }

        }

        catch (OpenRDFException e) {

            // handle exception

            e.printStackTrace();

        }

}

Salin selepas log masuk

2. 修改数据

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

public static void updateQuery() {

    String updateQuery = "PREFIX rk:<http://rk.com/test/> " +

            "DELETE { " +

            "?creativeWork rk:type ?type ." +

            "} " +

            "INSERT { " +

            "?creativeWork rk:type ?typeUri ." +

            "} " +

            "WHERE { " +

            "?creativeWork rk:type ?type ." +

            "BIND( URI(?type) AS ?typeUri ) " +

            "} ";

    RepositoryConnection conn;

    try {

        conn = repo.getConnection();

        Update update = conn.prepareUpdate(QueryLanguage.SPARQL, updateQuery);

        update.execute();

        conn.commit();

        conn.close();

    } catch (RepositoryException e) {

        e.printStackTrace();

    } catch (MalformedQueryException e) {

        e.printStackTrace();

    } catch (UpdateExecutionException e) {

        e.printStackTrace();

    }

     

}

Salin selepas log masuk

具体SPARQL语言可以参考书籍《Learning SPARQL》。
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan