Project

General

Profile

1
/**
2
 *        Name: DBSimpleQuery.java
3
 *     Purpose: A Class that searches a relational DB for XPath expressions
4
 *              and returns a reult set consisting of the root nodeid for
5
 *              each document that satisfies the query
6
 * Institution: National Center for Ecological Analysis and Synthesis
7
 *   Copyright: 2000
8
 *     Authors: Matt Jones
9
 *
10
 *     Version: '$Id: DBSimpleQuery.java 28 2000-04-12 01:54:06Z jones $'
11
 */
12

    
13
import java.io.*;
14
import java.net.URL;
15
import java.net.MalformedURLException;
16
import java.sql.*;
17
import java.util.Stack;
18
import java.util.Hashtable;
19
import java.util.Enumeration;
20

    
21
public class DBSimpleQuery {
22

    
23
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
24
  private Connection	conn = null;
25

    
26
  static public void main(String[] args) {
27
     
28
     if (args.length < 3)
29
     {
30
        System.err.println("Wrong number of arguments!!!");
31
        System.err.println("USAGE: java DBSimpleQuery " +
32
                           "<query> <user> <password> [dbstring]");
33
        return;
34
     } else {
35
        try {
36
                    
37
          String query    = args[0];
38
          String user     = args[1];
39
          String password = args[2];
40
          String dbstring = null;
41

    
42
          if (args.length <= 3) {
43
            dbstring = defaultDB;
44
          } else {
45
            dbstring = args[3];
46
          }
47

    
48
          // Execute the simple query
49
          DBSimpleQuery rd = new DBSimpleQuery(user, password, dbstring);
50
          Hashtable nodelist = rd.findRootNodes(query);
51

    
52
          // Print the reulting root nodes
53
          StringBuffer result = new StringBuffer();
54
          long nodeid;
55
          result.append("<?xml version=\"1.0\"?>\n");
56
          result.append("<resultset>\n");
57
          Enumeration rootlist = nodelist.keys(); 
58
          while (rootlist.hasMoreElements()) {
59
            nodeid = ((Long)rootlist.nextElement()).longValue();
60
            result.append("  <nodeid>").append(nodeid).append("</nodeid>\n");
61
          }
62
          result.append("</resultset>");
63

    
64
          System.out.println(result);
65

    
66
        } catch (Exception e) {
67
          System.err.println("EXCEPTION HANDLING REQUIRED");
68
          System.err.println(e.getMessage());
69
          e.printStackTrace(System.err);
70
        }
71
     }
72
  }
73
  
74
  private DBSimpleQuery( String user, String password, String dbstring) 
75
                  throws IOException, 
76
                         SQLException, 
77
                         ClassNotFoundException
78
  {
79
     // Open a connection to the database
80
     conn = openDBConnection(
81
                "oracle.jdbc.driver.OracleDriver",
82
                dbstring, user, password);
83

    
84
  }
85
  
86
  private Connection openDBConnection(String dbDriver, String connection,
87
                String user, String password)
88
                throws SQLException, ClassNotFoundException {
89
     // Load the Oracle JDBC driver
90
     Class.forName (dbDriver);
91

    
92
     // Connect to the database
93
     Connection conn = DriverManager.getConnection( connection, user, password);
94
     return conn;
95
  }
96

    
97
  public Hashtable findRootNodes(String query) {
98
      Hashtable	nodeListResult = new Hashtable();
99
      Hashtable	rootListResult = new Hashtable();
100

    
101
      PreparedStatement pstmt;
102
      long nodeid;
103

    
104
      try {
105
        pstmt =
106
          conn.prepareStatement("SELECT nodeid " +
107
                  "FROM xml_nodes WHERE nodedata LIKE ?");
108
        // Bind the values to the query
109
        pstmt.setString(1, query);
110

    
111
        pstmt.execute();
112
        try {
113
          ResultSet rs = pstmt.getResultSet();
114
          try {
115
            boolean tableHasRows = rs.next();
116
            while (tableHasRows) {
117
              try {
118
                nodeid = rs.getLong(1);
119
                nodeListResult.put(new Long(nodeid),new Long(nodeid));
120

    
121
              } catch (SQLException e) {
122
                System.out.println("Error with getInt: " + e.getMessage());
123
              }
124

    
125
              // Advance to the next record in the cursor
126
              tableHasRows = rs.next();
127
            }
128
          } catch (SQLException e) {
129
            System.out.println("Error with next: " + e.getMessage());
130
          }
131
        } catch (SQLException e) {
132
          System.out.println("Error with getrset: " + e.getMessage());
133
        }
134
        pstmt.close();
135
      } catch (SQLException e) {
136
        System.out.println("Error getting id: " + e.getMessage());
137
      }
138

    
139
      Enumeration nodelist = nodeListResult.keys(); 
140
      while (nodelist.hasMoreElements()) {
141
        nodeid = ((Long)nodelist.nextElement()).longValue();
142

    
143
        try {
144
          pstmt =
145
            conn.prepareStatement("SELECT nodeid " +
146
                    "FROM xml_nodes START WITH nodeid = ? " +
147
                    "CONNECT BY nodeid = PRIOR parentnodeid");
148
          // Bind the values to the query
149
          pstmt.setLong(1, nodeid);
150
  
151
          pstmt.execute();
152
          try {
153
            ResultSet rs = pstmt.getResultSet();
154
            try {
155
              boolean tableHasRows = rs.next();
156
              while (tableHasRows) {
157
                try {
158
                  nodeid = rs.getLong(1);
159
  
160
                } catch (SQLException e) {
161
                  System.out.println("Error with getInt: " + e.getMessage());
162
                }
163

    
164
                // Advance to the next record in the cursor
165
                tableHasRows = rs.next();
166

    
167
              }
168
            } catch (SQLException e) {
169
              System.out.println("Error with next: " + e.getMessage());
170
            }
171
          } catch (SQLException e) {
172
            System.out.println("Error with getrset: " + e.getMessage());
173
          }
174
          pstmt.close();
175
        } catch (SQLException e) {
176
          System.out.println("Error getting id: " + e.getMessage());
177
        }
178
        
179
        // Record that the last record should be the root
180
        rootListResult.put(new Long(nodeid),new Long(nodeid));
181
      } 
182

    
183
      return rootListResult;
184
  }
185
}
(8-8/11)