1 |
21
|
jones
|
/**
|
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 |
35
|
jones
|
* Copyright: 2000 Regents of the University of California and the
|
6 |
|
|
* National Center for Ecological Analysis and Synthesis
|
7 |
21
|
jones
|
* Authors: Matt Jones
|
8 |
|
|
*
|
9 |
|
|
* Version: '$Id$'
|
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 |
31
|
jones
|
/**
|
19 |
|
|
* A Class that creates an XML text document
|
20 |
|
|
* from a query to a relational DB containing a DOM representation
|
21 |
|
|
*/
|
22 |
21
|
jones
|
public class DBReader {
|
23 |
|
|
|
24 |
|
|
static String defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
|
25 |
|
|
private Connection conn = null;
|
26 |
|
|
|
27 |
31
|
jones
|
/**
|
28 |
|
|
* main routine used for testing.
|
29 |
|
|
*
|
30 |
|
|
* Usage: java DBReader <nodeid> <user> <password> [dbstring]
|
31 |
|
|
*
|
32 |
|
|
* @param nodeid the id number of the root of the subtree to display
|
33 |
|
|
* @param user the username to use for the database connection
|
34 |
|
|
* @param password the password to use for the database connection
|
35 |
|
|
* @param dbstring the connection info to use for the database connection
|
36 |
|
|
*/
|
37 |
21
|
jones
|
static public void main(String[] args) {
|
38 |
|
|
|
39 |
|
|
if (args.length < 3)
|
40 |
|
|
{
|
41 |
|
|
System.err.println("Wrong number of arguments!!!");
|
42 |
|
|
System.err.println("USAGE: java DBReader " +
|
43 |
|
|
"<nodeid> <user> <password> [dbstring]");
|
44 |
|
|
return;
|
45 |
|
|
} else {
|
46 |
|
|
try {
|
47 |
|
|
|
48 |
23
|
jones
|
String nodeidstr = args[0];
|
49 |
|
|
long nodeid = (new Long(nodeidstr).longValue());
|
50 |
21
|
jones
|
String user = args[1];
|
51 |
|
|
String password = args[2];
|
52 |
|
|
String dbstring = null;
|
53 |
|
|
|
54 |
|
|
if (args.length <= 3) {
|
55 |
|
|
dbstring = defaultDB;
|
56 |
|
|
} else {
|
57 |
|
|
dbstring = args[3];
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
DBReader rd = new DBReader(user, password, dbstring);
|
61 |
31
|
jones
|
String xml = rd.readXMLDocument(nodeid);
|
62 |
21
|
jones
|
System.out.println(xml);
|
63 |
|
|
|
64 |
|
|
} catch (Exception e) {
|
65 |
|
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
66 |
|
|
System.err.println(e.getMessage());
|
67 |
|
|
e.printStackTrace(System.err);
|
68 |
|
|
}
|
69 |
|
|
}
|
70 |
|
|
}
|
71 |
|
|
|
72 |
31
|
jones
|
/**
|
73 |
|
|
* construct a DBReader instance.
|
74 |
|
|
*
|
75 |
|
|
* Generally, one calls readXMLDocument() after constructing the instance
|
76 |
|
|
*
|
77 |
|
|
* @param user the username to use for the database connection
|
78 |
|
|
* @param password the password to use for the database connection
|
79 |
|
|
* @param dbstring the connection info to use for the database connection
|
80 |
|
|
*/
|
81 |
29
|
jones
|
public DBReader( String user, String password, String dbstring)
|
82 |
21
|
jones
|
throws IOException,
|
83 |
|
|
SQLException,
|
84 |
|
|
ClassNotFoundException
|
85 |
|
|
{
|
86 |
|
|
// Open a connection to the database
|
87 |
|
|
conn = openDBConnection(
|
88 |
|
|
"oracle.jdbc.driver.OracleDriver",
|
89 |
|
|
dbstring, user, password);
|
90 |
|
|
|
91 |
|
|
}
|
92 |
|
|
|
93 |
31
|
jones
|
/** open a connection to the database with the supplied information */
|
94 |
21
|
jones
|
private Connection openDBConnection(String dbDriver, String connection,
|
95 |
|
|
String user, String password)
|
96 |
|
|
throws SQLException, ClassNotFoundException {
|
97 |
|
|
// Load the Oracle JDBC driver
|
98 |
|
|
Class.forName (dbDriver);
|
99 |
|
|
|
100 |
|
|
// Connect to the database
|
101 |
|
|
Connection conn = DriverManager.getConnection( connection, user, password);
|
102 |
|
|
return conn;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
31
|
jones
|
/**
|
106 |
|
|
* Create an XML document from the database starting with the element
|
107 |
|
|
* having element_id nodeid
|
108 |
|
|
*
|
109 |
|
|
* @param nodeid the node that will represent the root of the document
|
110 |
|
|
*/
|
111 |
|
|
public String readXMLDocument(long nodeid) {
|
112 |
21
|
jones
|
StringBuffer doc = new StringBuffer();
|
113 |
|
|
|
114 |
23
|
jones
|
ReaderElement element = new ReaderElement(conn, nodeid);
|
115 |
24
|
jones
|
doc.append("<?xml version=\"1.0\"?>\n");
|
116 |
|
|
doc.append(element.toString());
|
117 |
21
|
jones
|
|
118 |
|
|
return (doc.toString());
|
119 |
|
|
}
|
120 |
|
|
}
|