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 23 2000-04-11 20:56:55Z 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 nodeidstr = args[0];
|
35
|
long nodeid = (new Long(nodeidstr).longValue());
|
36
|
String user = args[1];
|
37
|
String password = args[2];
|
38
|
String dbstring = null;
|
39
|
|
40
|
if (args.length <= 3) {
|
41
|
dbstring = defaultDB;
|
42
|
} else {
|
43
|
dbstring = args[3];
|
44
|
}
|
45
|
|
46
|
DBReader rd = new DBReader(user, password, dbstring);
|
47
|
String xml = rd.readDocument(nodeid);
|
48
|
System.out.println(xml);
|
49
|
|
50
|
} catch (Exception e) {
|
51
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
52
|
System.err.println(e.getMessage());
|
53
|
e.printStackTrace(System.err);
|
54
|
}
|
55
|
}
|
56
|
}
|
57
|
|
58
|
private DBReader( String user, String password, String dbstring)
|
59
|
throws IOException,
|
60
|
SQLException,
|
61
|
ClassNotFoundException
|
62
|
{
|
63
|
// Open a connection to the database
|
64
|
conn = openDBConnection(
|
65
|
"oracle.jdbc.driver.OracleDriver",
|
66
|
dbstring, user, password);
|
67
|
|
68
|
}
|
69
|
|
70
|
private Connection openDBConnection(String dbDriver, String connection,
|
71
|
String user, String password)
|
72
|
throws SQLException, ClassNotFoundException {
|
73
|
// Load the Oracle JDBC driver
|
74
|
Class.forName (dbDriver);
|
75
|
|
76
|
// Connect to the database
|
77
|
Connection conn = DriverManager.getConnection( connection, user, password);
|
78
|
return conn;
|
79
|
}
|
80
|
|
81
|
public String readDocument(long nodeid) {
|
82
|
StringBuffer doc = new StringBuffer();
|
83
|
|
84
|
System.out.println("\nGetting document with nodeid: " + nodeid + "\n");
|
85
|
//BasicElement element = readNodeFromDB(nodeid);
|
86
|
ReaderElement element = new ReaderElement(conn, nodeid);
|
87
|
doc.append(element);
|
88
|
|
89
|
return (doc.toString());
|
90
|
}
|
91
|
|
92
|
/** look up the assigned element id from DB connection */
|
93
|
private BasicElement readNodeFromDB(long nodeid) {
|
94
|
long element_id=0;
|
95
|
long nodeparentid=0;
|
96
|
String nodetype=null;
|
97
|
String nodename=null;
|
98
|
String nodedata=null;
|
99
|
|
100
|
PreparedStatement pstmt;
|
101
|
try {
|
102
|
pstmt =
|
103
|
conn.prepareStatement("SELECT nodeid,nodeparentid,nodetype, " +
|
104
|
"nodename,nodedata FROM xml_nodes WHERE nodeid = ?");
|
105
|
// Bind the values to the query
|
106
|
pstmt.setLong(1, nodeid);
|
107
|
|
108
|
pstmt.execute();
|
109
|
try {
|
110
|
ResultSet rs = pstmt.getResultSet();
|
111
|
try {
|
112
|
boolean tableHasRows = rs.next();
|
113
|
if (tableHasRows) {
|
114
|
try {
|
115
|
element_id = rs.getLong(1);
|
116
|
nodeparentid = rs.getLong(2);
|
117
|
nodetype = rs.getString(3);
|
118
|
nodename = rs.getString(4);
|
119
|
nodedata = rs.getString(5);
|
120
|
} catch (SQLException e) {
|
121
|
System.out.println("Error with getInt: " + e.getMessage());
|
122
|
}
|
123
|
}
|
124
|
} catch (SQLException e) {
|
125
|
System.out.println("Error with next: " + e.getMessage());
|
126
|
}
|
127
|
} catch (SQLException e) {
|
128
|
System.out.println("Error with getrset: " + e.getMessage());
|
129
|
}
|
130
|
pstmt.close();
|
131
|
} catch (SQLException e) {
|
132
|
System.out.println("Error getting id: " + e.getMessage());
|
133
|
}
|
134
|
|
135
|
BasicElement element = null;
|
136
|
if (nodetype.equals("ELEMENT")) {
|
137
|
element = new BasicElement(element_id,nodename,nodeparentid);
|
138
|
element.appendContent(nodedata);
|
139
|
}
|
140
|
|
141
|
// Get a list of child nodes and recursively retrieve them as well
|
142
|
try {
|
143
|
pstmt =
|
144
|
conn.prepareStatement("SELECT nodeid,nodeparentid,nodetype, " +
|
145
|
"nodename,nodedata FROM xml_nodes " +
|
146
|
"WHERE nodeparentid = ?");
|
147
|
// Bind the values to the query
|
148
|
pstmt.setLong(1, nodeid);
|
149
|
|
150
|
pstmt.execute();
|
151
|
try {
|
152
|
ResultSet rs = pstmt.getResultSet();
|
153
|
try {
|
154
|
boolean tableHasRows = rs.next();
|
155
|
if (tableHasRows) {
|
156
|
try {
|
157
|
element_id = rs.getLong(1);
|
158
|
nodeparentid = rs.getLong(2);
|
159
|
nodetype = rs.getString(3);
|
160
|
nodename = rs.getString(4);
|
161
|
nodedata = rs.getString(5);
|
162
|
} catch (SQLException e) {
|
163
|
System.out.println("Error with getInt: " + e.getMessage());
|
164
|
}
|
165
|
}
|
166
|
} catch (SQLException e) {
|
167
|
System.out.println("Error with next: " + e.getMessage());
|
168
|
}
|
169
|
} catch (SQLException e) {
|
170
|
System.out.println("Error with getrset: " + e.getMessage());
|
171
|
}
|
172
|
pstmt.close();
|
173
|
} catch (SQLException e) {
|
174
|
System.out.println("Error getting id: " + e.getMessage());
|
175
|
}
|
176
|
|
177
|
|
178
|
return element;
|
179
|
}
|
180
|
}
|