- Notifications
You must be signed in to change notification settings - Fork 849
/
Copy pathtestSODA.java
149 lines (123 loc) · 5.65 KB
/
testSODA.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
importjava.sql.Connection;
importjava.sql.DriverManager;
importoracle.sql.*;
importoracle.soda.rdbms.OracleRDBMSClient;
importoracle.soda.OracleDatabase;
importoracle.soda.OracleCursor;
importoracle.soda.OracleCollection;
importoracle.soda.OracleDocument;
importoracle.soda.OracleException;
importjava.util.Properties;
importoracle.jdbc.OracleConnection;
importoracle.jdbc.pool.OracleDataSource;
publicclasstestSODA {
publicstaticvoidmain(String[] args) {
// SODA works on top of a regular JDBC connection.
Connectionconn = null;
try {
/*
* Where is your code running: in the database or outside?
*/
if (System.getProperty("oracle.jserver.version") != null)
{
/*
* You are in the database, already connected, use the default connection
*
*/
OracleDataSourceods = newOracleDataSource();
ods.setURL("jdbc:default:connection");
conn = ods.getConnection();
}
else {
/*
* You are not in the database, you need to connect thru the client driver
*/
// Set up the connection string: replace hostName, port, and serviceName
// with the info for your Oracle RDBMS instance
Stringurl = "jdbc:oracle:thin:@//hostName:port/serviceName";
Propertiesprops = newProperties();
// Replace with your schemaName and password
props.setProperty("user", "schemaName");
props.setProperty("password", "password");
conn = (OracleConnection) DriverManager.getConnection(url, props);
}
// Get an OracleRDBMSClient - starting point of SODA for Java application
OracleRDBMSClientcl = newOracleRDBMSClient();
// Get a database
OracleDatabasedb = cl.getDatabase(conn);
//Check whether the named collection already exists or not
OracleCollectioncol = db.openCollection("MyFirstJSONCollection");
if (col != null) col.admin().drop();
// Create a collection with the name "MyFirstJSONCollection".
// Note: Collection names are case-sensitive.
// A table with the name "MyFirstJSONCollection" will be
// created in the RDBMS to store the collection
col = db.admin().createCollection("MyFirstJSONCollection");
// Create a few JSON documents, representing
// users and the number of friends they have
OracleDocumentdoc1 =
db.createDocumentFromString(
"{ \"name\" : \"Alex\", \"friends\" : \"50\" }");
OracleDocumentdoc2 =
db.createDocumentFromString(
"{ \"name\" : \"Mia\", \"friends\" : \"300\" }");
OracleDocumentdoc3 =
db.createDocumentFromString(
"{ \"name\" : \"Gloria\", \"friends\" : \"399\" }");
// Insert the documents into a collection, one-by-one.
// The result documents contain auto-generated
// keys, among other documents components (version, etc).
// Note: SODA provides the more efficient bulk insert as well
OracleDocumentresultDoc1 = col.insertAndGet(doc1);
OracleDocumentresultDoc2 = col.insertAndGet(doc2);
OracleDocumentresultDoc3 = col.insertAndGet(doc3);
// Retrieve the first document using its auto-generated
// unique ID (aka key)
System.out.println ("* Retrieving the first document by its key *\n");
OracleDocumentfetchedDoc = col.find().key(resultDoc1.getKey()).getOne();
System.out.println (fetchedDoc.getContentAsString());
// Retrieve all documents representing users that have
// 300 or more friends. Use the following query-by-example:
// {friends : {$gte : 300}}.
System.out.println ("\n* Retrieving documents representing users with" +
" at least 300 friends *\n");
OracleDocumentf = db.createDocumentFromString(
"{ \"friends\" : { \"$gte\" : 300 }}");
OracleCursorc = null;
try {
// Get a cursor over all documents in the collection
// that match our query-by-example
c = col.find().filter(f).getCursor();
while (c.hasNext()) {
// Get the next document
fetchedDoc = c.next();
System.out.println (fetchedDoc.getContentAsString());
}
}
finally {
// Important: you must close the cursor to release resources!
if (c != null) {
c.close();
}
}
// Drop the collection, deleting the table backing
// it and collection metadata
if (args.length > 0 && args[0].equals("drop")) {
col.admin().drop();
System.out.println ("\n* Collection dropped *");
}
}
catch (Exceptione) {
e.printStackTrace();
}
finally {
if (conn != null) {
try {
conn.close();
}
catch (Exceptione) {
}
}
}
}
}