ALTER USER SCOTT ACCOUNT UNLOCK;
ALTER USER SCOTT IDENTIFIED BY TIGER; -- NON PRODUCTION DATABASE
dbms_java.grant_permission('SCOTT','SYS:java.net.SocketPermission','127.0.0.1:1521','resolve');
As we need to import and export data from a table, let's create it :
CREATE TABLE SCOTT.BINDINGS
(
IRCEVENT VARCHAR2(24 BYTE),
METHOD VARCHAR2(24 BYTE),
REGEX VARCHAR2(24 BYTE)
)
JSONObject strjson = new JSONObject(sJSON);
We get the root, IT MUST BE the name of the table
String root = strjson.names().getString(0);
To read all rows of data, we use a JSON array
JSONArray jsonArray = (JSONArray )strjson.get(root);
We create a new JSON object to receive each "row"
JSONObject json;
We use a mutable string to store each column name
StringBuffer columns = new StringBuffer();
We use a mutable string to set associated the parameter of each column
StringBuffer qstmark = new StringBuffer();
Our main loop. We read every JSON element {key:value} from the JSON array
for(int j=0;j<jsonArray.length();j++) {
We delete content of the previous set of columns and parameters
columns.delete(0, columns.length());
qstmark.delete(0, qstmark.length());
We get the current row
json = (JSONObject )jsonArray.get(j);
We create an array to store the name of the columns (keys)
String key[] = new String[json.length()];
We create an array to store the values for each column
String val[] = new String[json.length()];
For each key, we store the column name and its value
for(Iterator i = json.keys();i.hasNext();k++) {
key[k] = (String )i.next();
val[k] = json.getString(key[k]);
We don't add a separator (comma) for the first column and the first parameter
if(k>0) {
columns.append("," + key[k]);
qstmark.append(",?");
}
else {
columns.append(key[k]);
qstmark.append("?");
}
We build our query :
String insertString = "INSERT INTO " + root + " (" + columns.toString() + ")";
insertString += " VALUES (" + qstmark.toString() + ")";
pstmt = conn.prepareStatement(insertString);
We set the values of each parameters. For this tutorial we assume all value are string
int parameterNumber;
for(int p=0;p<val.length;p++) {
parameterNumber = p + 1;
pstmt.setString( parameterNumber, val[p] );
}
We run the query
pstmt.executeUpdate();
ResultSetMetaData rsmd = rset.getMetaData();
int numColumns = rsmd.getColumnCount();
We create an array of String to store all column name. Each column name will be a key :
String key[] = new String[numColumns];
int columnNumber;
for(int i=0;i<numColumns+1;i++) {
columnNumber = i + 1;
key[i] = rsmd.getColumnName(columnNumber);
}
We create a JSON Object. It will receive each row :
JSONObject json = new JSONObject();
We create a JSON Array to receive all JSON element :
JSONArray ajson = new JSONArray();
We read all row returned by the query :
while(rset.next()) {
For this tutorial we assume all value are string
for(int i=0;i<numColumns;i++) {
json.putOpt(key[i],
rset.getString(key[i]));
}
We add the entry (at the origin, a result row) to the JSON Array
ajson.put(json);
We create an empty JSON object and convert it to
JSONObject fJSON = new JSONObject();
We associate the JSON elements with the root key :
fJSON = fJSON.put(root,ajson);
We convert our JSON Object to a JSON String :
sJSON = fJSON.toString();
We return the JSON String :
return sJSON;
C:\>CD E:\org\json
C:\>E:
E:\>loadjava -user scott/tiger JSONArray.java
E:\>loadjava -user scott/tiger JSONException.java
E:\>loadjava -user scott/tiger JSONObject.java
E:\>loadjava -user scott/tiger JSONString.java
E:\>loadjava -user scott/tiger JSONTokener.java
E:\>loadjava -user scott/tiger JSONWriter.java
E:\oracle\>loadjava -user scott/tiger SQL.java
CREATE OR REPLACE PROCEDURE INSERT_JSON (
connection IN VARCHAR2, username IN VARCHAR2, passwd IN VARCHAR2, sJSON IN VARCHAR2)
AS LANGUAGE JAVA NAME 'org.json.oracle.SQL.insertJSON(
java.lang.String, java.lang.String, java.lang.String, java.lang.String)';
create or replace FUNCTION SELECT_JSON (
connection IN VARCHAR2, username IN VARCHAR2, passwd IN VARCHAR2, root IN VARCHAR2, sJSON IN VARCHAR2)
RETURN VARCHAR2 AS LANGUAGE JAVA NAME 'org.json.oracle.SQL.selectJSON+(
java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
return java.lang.String';
CALL INSERT_JSON('jdbc:oracle:thin:@localhost:1521:orcl', 'scott', 'tiger',
'{"bindings":[{"ircEvent":"PRIVMSG","method":"newURI","regex":"^http://.*"},
{"ircEvent":"PRIVMSG","method":"deleteURI","regex":"^delete.*"},
{"ircEvent":"PRIVMSG","method":"randomURI","regex":"^random.*"}]}');
SELECT
SELECT_JSON('jdbc:oracle:thin:@localhost:1521:orcl','scott','tiger','bindings', 'SELECT * FROM BINDINGS')
FROM DUAL;
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)