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
 * Institution: National Center for Ecological Analysis and Synthesis
6
 *   Copyright: 2000
7
 *     Authors: Matt Jones
8
 *
9
 *     Version: '$Id: DBReader.java 24 2000-04-11 22:19:11Z 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
    ReaderElement element = new ReaderElement(conn, nodeid);
85
    doc.append("<?xml version=\"1.0\"?>\n");
86
    doc.append(element.toString());
87

    
88
    return (doc.toString());
89
  }
90
}
(4-4/12)