1
|
/**
|
2
|
* Name: DBReader.java
|
3
|
* Purpose: A Class that creates an XML text document
|
4
|
* from a query to a relational DB containing a DOM representation
|
5
|
* Institution: National Center for Ecological Analysis and Synthesis
|
6
|
* Copyright: 2000
|
7
|
* Authors: Matt Jones
|
8
|
*
|
9
|
* Version: '$Id: DBReader.java 21 2000-04-11 20:26:43Z jones $'
|
10
|
*/
|
11
|
|
12
|
import java.io.*;
|
13
|
import java.net.URL;
|
14
|
import java.net.MalformedURLException;
|
15
|
import java.sql.*;
|
16
|
import java.util.Stack;
|
17
|
|
18
|
public class DBReader {
|
19
|
|
20
|
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
|
21
|
private Connection conn = null;
|
22
|
|
23
|
static public void main(String[] args) {
|
24
|
|
25
|
if (args.length < 3)
|
26
|
{
|
27
|
System.err.println("Wrong number of arguments!!!");
|
28
|
System.err.println("USAGE: java DBReader " +
|
29
|
"<nodeid> <user> <password> [dbstring]");
|
30
|
return;
|
31
|
} else {
|
32
|
try {
|
33
|
|
34
|
String nodeid = args[0];
|
35
|
String user = args[1];
|
36
|
String password = args[2];
|
37
|
String dbstring = null;
|
38
|
|
39
|
if (args.length <= 3) {
|
40
|
dbstring = defaultDB;
|
41
|
} else {
|
42
|
dbstring = args[3];
|
43
|
}
|
44
|
|
45
|
DBReader rd = new DBReader(user, password, dbstring);
|
46
|
String xml = rd.readDocument(nodeid);
|
47
|
System.out.println(xml);
|
48
|
|
49
|
} catch (Exception e) {
|
50
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
51
|
System.err.println(e.getMessage());
|
52
|
e.printStackTrace(System.err);
|
53
|
}
|
54
|
}
|
55
|
}
|
56
|
|
57
|
private DBReader( String user, String password, String dbstring)
|
58
|
throws IOException,
|
59
|
SQLException,
|
60
|
ClassNotFoundException
|
61
|
{
|
62
|
// Open a connection to the database
|
63
|
conn = openDBConnection(
|
64
|
"oracle.jdbc.driver.OracleDriver",
|
65
|
dbstring, user, password);
|
66
|
|
67
|
}
|
68
|
|
69
|
private Connection openDBConnection(String dbDriver, String connection,
|
70
|
String user, String password)
|
71
|
throws SQLException, ClassNotFoundException {
|
72
|
// Load the Oracle JDBC driver
|
73
|
Class.forName (dbDriver);
|
74
|
|
75
|
// Connect to the database
|
76
|
Connection conn = DriverManager.getConnection( connection, user, password);
|
77
|
return conn;
|
78
|
}
|
79
|
|
80
|
public String readDocument(String nodeid) {
|
81
|
StringBuffer doc = new StringBuffer();
|
82
|
|
83
|
System.out.println("\nGetting document with nodeid: " + nodeid + "\n");
|
84
|
BasicElement element = readNodeFromDB(nodeid);
|
85
|
doc.append(element);
|
86
|
|
87
|
return (doc.toString());
|
88
|
}
|
89
|
|
90
|
/** look up the assigned element id from DB connection */
|
91
|
private BasicElement readNodeFromDB(String nodeid) {
|
92
|
long element_id=0;
|
93
|
long nodeparentid=0;
|
94
|
String nodetype=null;
|
95
|
String nodename=null;
|
96
|
String nodedata=null;
|
97
|
|
98
|
PreparedStatement pstmt;
|
99
|
try {
|
100
|
pstmt =
|
101
|
conn.prepareStatement("SELECT nodeid,nodeparentid,nodetype,
|
102
|
nodename,nodedata FROM xml_nodes WHERE nodeid = ?");
|
103
|
// Bind the values to the query
|
104
|
pstmt.setString(1, nodeid);
|
105
|
|
106
|
pstmt.execute();
|
107
|
try {
|
108
|
ResultSet rs = pstmt.getResultSet();
|
109
|
try {
|
110
|
boolean tableHasRows = rs.next();
|
111
|
if (tableHasRows) {
|
112
|
try {
|
113
|
element_id = rs.getLong(1);
|
114
|
nodeparentid = rs.getLong(2);
|
115
|
nodetype = rs.getString(3);
|
116
|
nodename = rs.getString(4);
|
117
|
nodedata = rs.getString(5);
|
118
|
} catch (SQLException e) {
|
119
|
System.out.println("Error with getInt: " + e.getMessage());
|
120
|
}
|
121
|
}
|
122
|
} catch (SQLException e) {
|
123
|
System.out.println("Error with next: " + e.getMessage());
|
124
|
}
|
125
|
} catch (SQLException e) {
|
126
|
System.out.println("Error with getrset: " + e.getMessage());
|
127
|
}
|
128
|
pstmt.close();
|
129
|
} catch (SQLException e) {
|
130
|
System.out.println("Error getting id: " + e.getMessage());
|
131
|
}
|
132
|
|
133
|
if (nodetype.equals("ELEMENT")) {
|
134
|
BasicElement element =
|
135
|
new BasicElement(element_id,nodename,nodeparentid);
|
136
|
element.appendContent(nodedata);
|
137
|
}
|
138
|
|
139
|
// Get a list of child nodes and recursively retrieve them as well
|
140
|
try {
|
141
|
pstmt =
|
142
|
conn.prepareStatement("SELECT nodeid,nodeparentid,nodetype, " +
|
143
|
"nodename,nodedata FROM xml_nodes " +
|
144
|
"WHERE nodeparentid = ?");
|
145
|
// Bind the values to the query
|
146
|
pstmt.setString(1, nodeid);
|
147
|
|
148
|
pstmt.execute();
|
149
|
try {
|
150
|
ResultSet rs = pstmt.getResultSet();
|
151
|
try {
|
152
|
boolean tableHasRows = rs.next();
|
153
|
if (tableHasRows) {
|
154
|
try {
|
155
|
element_id = rs.getLong(1);
|
156
|
nodeparentid = rs.getLong(2);
|
157
|
nodetype = rs.getString(3);
|
158
|
nodename = rs.getString(4);
|
159
|
nodedata = rs.getString(5);
|
160
|
} catch (SQLException e) {
|
161
|
System.out.println("Error with getInt: " + e.getMessage());
|
162
|
}
|
163
|
}
|
164
|
} catch (SQLException e) {
|
165
|
System.out.println("Error with next: " + e.getMessage());
|
166
|
}
|
167
|
} catch (SQLException e) {
|
168
|
System.out.println("Error with getrset: " + e.getMessage());
|
169
|
}
|
170
|
pstmt.close();
|
171
|
} catch (SQLException e) {
|
172
|
System.out.println("Error getting id: " + e.getMessage());
|
173
|
}
|
174
|
|
175
|
|
176
|
return element;
|
177
|
}
|
178
|
}
|