Project

General

Profile

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
 *   Copyright: 2000 Regents of the University of California and the
6
 *              National Center for Ecological Analysis and Synthesis
7
 *     Authors: Matt Jones
8
 *
9
 *     Version: '$Id: DBReader.java 51 2000-04-17 23:06:07Z jones $'
10
 */
11

    
12
package edu.ucsb.nceas.metacat;
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

    
20
/** 
21
 * A Class that creates an XML text document
22
 * from a query to a relational DB containing a DOM representation
23
 */
24
public class DBReader {
25

    
26
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
27
  private Connection	conn = null;
28

    
29
  /**
30
   * main routine used for testing.
31
   *
32
   * Usage: java DBReader <nodeid> <user> <password> [dbstring]
33
   *
34
   * @param nodeid the id number of the root of the subtree to display
35
   * @param user the username to use for the database connection
36
   * @param password the password to use for the database connection
37
   * @param dbstring the connection info to use for the database connection
38
   */
39
  static public void main(String[] args) {
40
     
41
     if (args.length < 3)
42
     {
43
        System.err.println("Wrong number of arguments!!!");
44
        System.err.println("USAGE: java DBReader " +
45
                           "<nodeid> <user> <password> [dbstring]");
46
        return;
47
     } else {
48
        try {
49
                    
50
          String nodeidstr = args[0];
51
          long nodeid = (new Long(nodeidstr).longValue());
52
          String user     = args[1];
53
          String password = args[2];
54
          String dbstring = null;
55

    
56
          if (args.length <= 3) {
57
            dbstring = defaultDB;
58
          } else {
59
            dbstring = args[3];
60
          }
61

    
62
          // Open a connection to the database
63
          Connection dbconn = MetaCatUtil.openDBConnection(
64
                "oracle.jdbc.driver.OracleDriver",
65
                dbstring, user, password);
66

    
67
          DBReader rd = new DBReader( dbconn );
68
          String xml = rd.readXMLDocument(nodeid);
69
          System.out.println(xml);
70

    
71
        } catch (Exception e) {
72
          System.err.println("EXCEPTION HANDLING REQUIRED");
73
          System.err.println(e.getMessage());
74
          e.printStackTrace(System.err);
75
        }
76
     }
77
  }
78
  
79
  /**
80
   * construct a DBReader instance.
81
   *
82
   * Generally, one calls readXMLDocument() after constructing the instance
83
   *
84
   * @param conn the database connection from which to read the document
85
   */
86
  public DBReader( Connection conn ) 
87
                  throws IOException, 
88
                         SQLException, 
89
                         ClassNotFoundException
90
  {
91
    this.conn = conn;
92
  }
93
  
94
  /**
95
   * Create an XML document from the database starting with the element 
96
   * having element_id nodeid
97
   *
98
   * @param nodeid the node that will represent the root of the document
99
   */
100
  public String readXMLDocument(long nodeid) {
101
    StringBuffer doc = new StringBuffer();
102

    
103
    ReaderElement element = new ReaderElement(conn, nodeid);
104
    doc.append("<?xml version=\"1.0\"?>\n");
105
    doc.append(element.toString());
106

    
107
    return (doc.toString());
108
  }
109
}
(6-6/29)