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