1 |
27
|
jones
|
/**
|
2 |
|
|
* Name: DBSimpleQuery.java
|
3 |
|
|
* Purpose: A Class that searches a relational DB for XPath expressions
|
4 |
|
|
* and returns a reult set consisting of the root nodeid for
|
5 |
|
|
* each document that satisfies the query
|
6 |
|
|
* Institution: National Center for Ecological Analysis and Synthesis
|
7 |
|
|
* Copyright: 2000
|
8 |
|
|
* Authors: Matt Jones
|
9 |
|
|
*
|
10 |
|
|
* Version: '$Id$'
|
11 |
|
|
*/
|
12 |
|
|
|
13 |
|
|
import java.io.*;
|
14 |
|
|
import java.net.URL;
|
15 |
|
|
import java.net.MalformedURLException;
|
16 |
|
|
import java.sql.*;
|
17 |
|
|
import java.util.Stack;
|
18 |
|
|
import java.util.Hashtable;
|
19 |
|
|
import java.util.Enumeration;
|
20 |
|
|
|
21 |
|
|
public class DBSimpleQuery {
|
22 |
|
|
|
23 |
|
|
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
|
24 |
|
|
private Connection conn = null;
|
25 |
|
|
|
26 |
|
|
static public void main(String[] args) {
|
27 |
|
|
|
28 |
|
|
if (args.length < 3)
|
29 |
|
|
{
|
30 |
|
|
System.err.println("Wrong number of arguments!!!");
|
31 |
|
|
System.err.println("USAGE: java DBSimpleQuery " +
|
32 |
|
|
"<query> <user> <password> [dbstring]");
|
33 |
|
|
return;
|
34 |
|
|
} else {
|
35 |
|
|
try {
|
36 |
|
|
|
37 |
|
|
String query = args[1];
|
38 |
|
|
String user = args[1];
|
39 |
|
|
String password = args[2];
|
40 |
|
|
String dbstring = null;
|
41 |
|
|
|
42 |
|
|
if (args.length <= 3) {
|
43 |
|
|
dbstring = defaultDB;
|
44 |
|
|
} else {
|
45 |
|
|
dbstring = args[3];
|
46 |
|
|
}
|
47 |
|
|
|
48 |
|
|
// Execute the simple query
|
49 |
|
|
DBSimpleQuery rd = new DBSimpleQuery(user, password, dbstring);
|
50 |
|
|
Hashtable nodelist = rd.findRootNodes(query);
|
51 |
|
|
|
52 |
|
|
// Print the reulting root nodes
|
53 |
|
|
StringBuffer result = new StringBuffer();
|
54 |
|
|
long nodeid;
|
55 |
|
|
result.append("<?xml version=\"1.0\"?>\n");
|
56 |
|
|
result.append("<resultset>\n");
|
57 |
|
|
Enumeration rootlist = nodelist.keys();
|
58 |
|
|
while (rootlist.hasMoreElements()) {
|
59 |
|
|
nodeid = ((Long)rootlist.nextElement()).longValue();
|
60 |
|
|
result.append(" <nodeid>").append(nodeid).append("</nodeid>\n");
|
61 |
|
|
}
|
62 |
|
|
result.append("</resultset>\n");
|
63 |
|
|
|
64 |
|
|
System.out.println(result);
|
65 |
|
|
|
66 |
|
|
} catch (Exception e) {
|
67 |
|
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
68 |
|
|
System.err.println(e.getMessage());
|
69 |
|
|
e.printStackTrace(System.err);
|
70 |
|
|
}
|
71 |
|
|
}
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
private DBSimpleQuery( String user, String password, String dbstring)
|
75 |
|
|
throws IOException,
|
76 |
|
|
SQLException,
|
77 |
|
|
ClassNotFoundException
|
78 |
|
|
{
|
79 |
|
|
// Open a connection to the database
|
80 |
|
|
conn = openDBConnection(
|
81 |
|
|
"oracle.jdbc.driver.OracleDriver",
|
82 |
|
|
dbstring, user, password);
|
83 |
|
|
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
private Connection openDBConnection(String dbDriver, String connection,
|
87 |
|
|
String user, String password)
|
88 |
|
|
throws SQLException, ClassNotFoundException {
|
89 |
|
|
// Load the Oracle JDBC driver
|
90 |
|
|
Class.forName (dbDriver);
|
91 |
|
|
|
92 |
|
|
// Connect to the database
|
93 |
|
|
Connection conn = DriverManager.getConnection( connection, user, password);
|
94 |
|
|
return conn;
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
public Hashtable findRootNodes(String query) {
|
98 |
|
|
Hashtable nodeListResult = new Hashtable();
|
99 |
|
|
Hashtable rootListResult = new Hashtable();
|
100 |
|
|
|
101 |
|
|
PreparedStatement pstmt;
|
102 |
|
|
long nodeid;
|
103 |
|
|
|
104 |
|
|
try {
|
105 |
|
|
pstmt =
|
106 |
|
|
conn.prepareStatement("SELECT nodeid " +
|
107 |
|
|
"FROM xml_nodes WHERE nodedata LIKE 'Value1'");
|
108 |
|
|
//"FROM xml_nodes WHERE nodedata LIKE ?");
|
109 |
|
|
// Bind the values to the query
|
110 |
|
|
//pstmt.setString(1, query);
|
111 |
|
|
|
112 |
|
|
pstmt.execute();
|
113 |
|
|
try {
|
114 |
|
|
ResultSet rs = pstmt.getResultSet();
|
115 |
|
|
try {
|
116 |
|
|
boolean tableHasRows = rs.next();
|
117 |
|
|
while (tableHasRows) {
|
118 |
|
|
try {
|
119 |
|
|
nodeid = rs.getLong(1);
|
120 |
|
|
nodeListResult.put(new Long(nodeid),new Long(nodeid));
|
121 |
|
|
|
122 |
|
|
} catch (SQLException e) {
|
123 |
|
|
System.out.println("Error with getInt: " + e.getMessage());
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
// Advance to the next record in the cursor
|
127 |
|
|
tableHasRows = rs.next();
|
128 |
|
|
}
|
129 |
|
|
} catch (SQLException e) {
|
130 |
|
|
System.out.println("Error with next: " + e.getMessage());
|
131 |
|
|
}
|
132 |
|
|
} catch (SQLException e) {
|
133 |
|
|
System.out.println("Error with getrset: " + e.getMessage());
|
134 |
|
|
}
|
135 |
|
|
pstmt.close();
|
136 |
|
|
} catch (SQLException e) {
|
137 |
|
|
System.out.println("Error getting id: " + e.getMessage());
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
Enumeration nodelist = nodeListResult.keys();
|
141 |
|
|
while (nodelist.hasMoreElements()) {
|
142 |
|
|
nodeid = ((Long)nodelist.nextElement()).longValue();
|
143 |
|
|
|
144 |
|
|
try {
|
145 |
|
|
pstmt =
|
146 |
|
|
conn.prepareStatement("SELECT nodeid " +
|
147 |
|
|
"FROM xml_nodes START WITH nodeid = ? " +
|
148 |
|
|
"CONNECT BY nodeid = PRIOR parentnodeid");
|
149 |
|
|
// Bind the values to the query
|
150 |
|
|
pstmt.setLong(1, nodeid);
|
151 |
|
|
|
152 |
|
|
pstmt.execute();
|
153 |
|
|
try {
|
154 |
|
|
ResultSet rs = pstmt.getResultSet();
|
155 |
|
|
try {
|
156 |
|
|
boolean tableHasRows = rs.next();
|
157 |
|
|
while (tableHasRows) {
|
158 |
|
|
try {
|
159 |
|
|
nodeid = rs.getLong(1);
|
160 |
|
|
|
161 |
|
|
} catch (SQLException e) {
|
162 |
|
|
System.out.println("Error with getInt: " + e.getMessage());
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
// Advance to the next record in the cursor
|
166 |
|
|
tableHasRows = rs.next();
|
167 |
|
|
|
168 |
|
|
}
|
169 |
|
|
} catch (SQLException e) {
|
170 |
|
|
System.out.println("Error with next: " + e.getMessage());
|
171 |
|
|
}
|
172 |
|
|
} catch (SQLException e) {
|
173 |
|
|
System.out.println("Error with getrset: " + e.getMessage());
|
174 |
|
|
}
|
175 |
|
|
pstmt.close();
|
176 |
|
|
} catch (SQLException e) {
|
177 |
|
|
System.out.println("Error getting id: " + e.getMessage());
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
// Record that the last record should be the root
|
181 |
|
|
rootListResult.put(new Long(nodeid),new Long(nodeid));
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
return rootListResult;
|
185 |
|
|
}
|
186 |
|
|
}
|