Project

General

Profile

1
/**
2
 *        Name: DBSimpleQuery.java
3
 *     Purpose: A Class that searches a relational DB for elements and 
4
 *              attributes that have free text matches to the query string.  
5
 *              It returns a result set consisting of the root nodeid for 
6
 *              each document that satisfies the query
7
 *   Copyright: 2000 Regents of the University of California and the
8
 *              National Center for Ecological Analysis and Synthesis
9
 *     Authors: Matt Jones
10
 *
11
 *     Version: '$Id: DBSimpleQuery.java 50 2000-04-17 22:40:19Z jones $'
12
 */
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
import java.util.Hashtable;
20
import java.util.Enumeration;
21

    
22
/** 
23
 * A Class that searches a relational DB for elements and attributes that
24
 * have free text matches to the query string.  It returns a result set 
25
 * consisting of the root nodeid for each document that satisfies the query
26
 */
27
public class DBSimpleQuery {
28

    
29
  static  String 	defaultDB = "jdbc:oracle:thin:@localhost:1521:test";
30
  private Connection	conn = null;
31

    
32
  /**
33
   * the main routine used to test the DBSimpleQuery utility.
34
   *
35
   * Usage: java DBSimpleQuery <query> <user> <password> [dbstring]
36
   *
37
   * @param query the text to search for in the element and attribute content
38
   * @param user the username to use for the database connection
39
   * @param password the password to use for the database connection
40
   * @param dbstring the connection info to use for the database connection
41
   */
42
  static public void main(String[] args) {
43
     
44
     if (args.length < 3)
45
     {
46
        System.err.println("Wrong number of arguments!!!");
47
        System.err.println("USAGE: java DBSimpleQuery " +
48
                           "<query> <user> <password> [dbstring]");
49
        return;
50
     } else {
51
        try {
52
                    
53
          String query    = args[0];
54
          String user     = args[1];
55
          String password = args[2];
56
          String dbstring = null;
57

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

    
64
          // Open a connection to the database
65
          Connection dbconn = MetaCatUtil.openDBConnection( 
66
                              "oracle.jdbc.driver.OracleDriver",
67
                              dbstring, user, password);
68
          // Execute the simple query
69
          DBSimpleQuery rd = new DBSimpleQuery(dbconn);
70
          Hashtable nodelist = rd.findRootNodes(query);
71

    
72
          // Print the reulting root nodes
73
          StringBuffer result = new StringBuffer();
74
          long nodeid;
75
          result.append("<?xml version=\"1.0\"?>\n");
76
          result.append("<resultset>\n");
77
          Enumeration rootlist = nodelist.keys(); 
78
          while (rootlist.hasMoreElements()) {
79
            nodeid = ((Long)rootlist.nextElement()).longValue();
80
            result.append("  <nodeid>").append(nodeid).append("</nodeid>\n");
81
          }
82
          result.append("</resultset>");
83

    
84
          System.out.println(result);
85

    
86
        } catch (Exception e) {
87
          System.err.println("EXCEPTION HANDLING REQUIRED");
88
          System.err.println(e.getMessage());
89
          e.printStackTrace(System.err);
90
        }
91
     }
92
  }
93
  
94
  /**
95
   * construct an instance of the DBSimpleQuery class 
96
   *
97
   * <p>Generally, one would call the findRootNodes() routine after creating 
98
   * an instance to specify the search query</p>
99
   *
100
   * @param conn the JDBC connection that we use for the query
101
   */
102
  public DBSimpleQuery( Connection conn ) 
103
                  throws IOException, 
104
                         SQLException, 
105
                         ClassNotFoundException
106
  {
107
    this.conn = conn;
108
  }
109
  
110
  /** 
111
   * routine to search the elements and attributes looking to match query
112
   *
113
   * @param query the text to search for
114
   */
115
  public Hashtable findRootNodes(String query) {
116
      Hashtable	nodeListResult = new Hashtable();
117
      Hashtable	rootListResult = new Hashtable();
118

    
119
      PreparedStatement pstmt;
120
      long nodeid;
121

    
122
      try {
123
        pstmt =
124
          conn.prepareStatement("SELECT nodeid " +
125
                  "FROM xml_nodes WHERE nodedata LIKE ?");
126
        // Bind the values to the query
127
        pstmt.setString(1, query);
128

    
129
        pstmt.execute();
130
        try {
131
          ResultSet rs = pstmt.getResultSet();
132
          try {
133
            boolean tableHasRows = rs.next();
134
            while (tableHasRows) {
135
              try {
136
                nodeid = rs.getLong(1);
137
                nodeListResult.put(new Long(nodeid),new Long(nodeid));
138

    
139
              } catch (SQLException e) {
140
                System.out.println("Error with getInt: " + e.getMessage());
141
              }
142

    
143
              // Advance to the next record in the cursor
144
              tableHasRows = rs.next();
145
            }
146
          } catch (SQLException e) {
147
            System.out.println("Error with next: " + e.getMessage());
148
          }
149
        } catch (SQLException e) {
150
          System.out.println("Error with getrset: " + e.getMessage());
151
        }
152
        pstmt.close();
153
      } catch (SQLException e) {
154
        System.out.println("Error getting id: " + e.getMessage());
155
      }
156

    
157
      Enumeration nodelist = nodeListResult.keys(); 
158
      while (nodelist.hasMoreElements()) {
159
        nodeid = ((Long)nodelist.nextElement()).longValue();
160

    
161
        try {
162
          pstmt =
163
            conn.prepareStatement("SELECT nodeid " +
164
                    "FROM xml_nodes START WITH nodeid = ? " +
165
                    "CONNECT BY nodeid = PRIOR parentnodeid");
166
          // Bind the values to the query
167
          pstmt.setLong(1, nodeid);
168
  
169
          pstmt.execute();
170
          try {
171
            ResultSet rs = pstmt.getResultSet();
172
            try {
173
              boolean tableHasRows = rs.next();
174
              while (tableHasRows) {
175
                try {
176
                  nodeid = rs.getLong(1);
177
  
178
                } catch (SQLException e) {
179
                  System.out.println("Error with getInt: " + e.getMessage());
180
                }
181

    
182
                // Advance to the next record in the cursor
183
                tableHasRows = rs.next();
184

    
185
              }
186
            } catch (SQLException e) {
187
              System.out.println("Error with next: " + e.getMessage());
188
            }
189
          } catch (SQLException e) {
190
            System.out.println("Error with getrset: " + e.getMessage());
191
          }
192
          pstmt.close();
193
        } catch (SQLException e) {
194
          System.out.println("Error getting id: " + e.getMessage());
195
        }
196
        
197
        // Record that the last record should be the root
198
        rootListResult.put(new Long(nodeid),new Long(nodeid));
199
      } 
200

    
201
      return rootListResult;
202
  }
203
}
(8-8/18)