Ticket #38: QueryExample.java

File QueryExample.java, 2.5 KB (added by Dimitar Misev, 12 years ago)
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5
6package examples;
7
8import java.util.Iterator;
9import org.odmg.DBag;
10import org.odmg.Database;
11import org.odmg.Implementation;
12import org.odmg.ODMGException;
13import org.odmg.OQLQuery;
14import org.odmg.Transaction;
15import rasj.RasImplementation;
16
17public class QueryExample
18{
19 public final String query = "select add_cells(<[0:2] 1.042323, 1.12, 1.8>) from mr2 as c";
20
21 private String server;
22 private String base;
23 Implementation myApp = null;
24 Database myDb = null;
25 Transaction myTa = null;
26
27 public QueryExample(String s, String b)
28 {
29 this.server = s;
30 this.base = b;
31
32 myApp = new RasImplementation(server);
33 myDb = myApp.newDatabase();
34 }
35
36 public void log(String msg)
37 {
38 System.err.flush();
39 System.out.println("log> " + msg);
40 System.out.flush();
41 }
42
43 private void init() throws ODMGException
44 {
45 if (myApp == null)
46 {
47 myApp = new RasImplementation(server);
48 }
49 if (myDb == null)
50 {
51 myDb = myApp.newDatabase();
52 }
53 log("Opening database...");
54 myDb.open(base, Database.OPEN_READ_ONLY);
55 log("Beginning new transaction ...");
56 myTa = myApp.newTransaction();
57 myTa.begin();
58 }
59
60 private void finish() throws ODMGException
61 {
62 log("Commiting transaction ...");
63 myTa.commit();
64 log("Closing database...");
65 myDb.close();
66 log("Finished !");
67 }
68
69 private void abort() throws ODMGException
70 {
71 log("Aborting transaction...");
72 myTa.abort();
73 log("Closing database...");
74 myDb.close();
75 log("Finished !");
76 }
77
78 public void sendQuery() throws ODMGException
79 {
80 init();
81
82 log("Executing query: " + query);
83 OQLQuery myQu = myApp.newOQLQuery();
84 myQu.create(query);
85
86 DBag resultSet = (DBag) myQu.execute();
87 if (resultSet != null)
88 {
89 Iterator iter = resultSet.iterator();
90 while (iter.hasNext())
91 {
92 Double res = (Double) iter.next();
93 System.out.println("*** Result is: " + res);
94 }
95 }
96
97 finish();
98 }
99
100 public static void main(String[] args) throws ODMGException
101 {
102 QueryExample ex = new QueryExample(
103 "http://localhost:7001", "RASBASE");
104 ex.sendQuery();
105 }
106}