Revision 301
Added by bojilova over 24 years ago
src/edu/ucsb/nceas/metacat/DBUtil.java | ||
---|---|---|
1 |
/** |
|
2 |
* '$RCSfile$' |
|
3 |
* Purpose: A Class that implements utility methods like: |
|
4 |
* 1/ Reding all doctypes from db connection |
|
5 |
* 2/ Reading Lore type Data Guide from db connection |
|
6 |
* Copyright: 2000 Regents of the University of California and the |
|
7 |
* National Center for Ecological Analysis and Synthesis |
|
8 |
* Authors: Matt Jones |
|
9 |
* |
|
10 |
* '$Author$' |
|
11 |
* '$Date$' |
|
12 |
* '$Revision$' |
|
13 |
*/ |
|
14 |
|
|
15 |
package edu.ucsb.nceas.metacat; |
|
16 |
|
|
17 |
import java.sql.Connection; |
|
18 |
import java.sql.SQLException; |
|
19 |
import java.sql.PreparedStatement; |
|
20 |
import java.sql.ResultSet; |
|
21 |
import java.util.Hashtable; |
|
22 |
import java.util.Enumeration; |
|
23 |
import java.util.Vector; |
|
24 |
|
|
25 |
/** |
|
26 |
* A suite of utility classes for quering DB |
|
27 |
*/ |
|
28 |
public class DBUtil { |
|
29 |
|
|
30 |
private Connection conn = null; |
|
31 |
|
|
32 |
/** |
|
33 |
* main routine used for testing. |
|
34 |
* <p> |
|
35 |
* Usage: java DBUtil <-dt|-dg> |
|
36 |
* |
|
37 |
* @param -dt for selecting all doctypes |
|
38 |
* -dg for selecting DataGuide |
|
39 |
*/ |
|
40 |
static public void main(String[] args) { |
|
41 |
|
|
42 |
if (args.length < 1) |
|
43 |
{ |
|
44 |
System.err.println("Wrong number of arguments!!!"); |
|
45 |
System.err.println("USAGE: java DBUtil <-dt|-dg>"); |
|
46 |
return; |
|
47 |
} else { |
|
48 |
try { |
|
49 |
|
|
50 |
// Open a connection to the database |
|
51 |
MetaCatUtil util = new MetaCatUtil(); |
|
52 |
Connection dbconn = util.openDBConnection(); |
|
53 |
|
|
54 |
DBUtil dbutil = new DBUtil( dbconn ); |
|
55 |
|
|
56 |
if ( args[0].equals("-dt") ) { |
|
57 |
String doctypes = dbutil.readDoctypes(); |
|
58 |
System.out.println(doctypes); |
|
59 |
} else if ( args[0].equals("-dg") ) { |
|
60 |
String dataguide = dbutil.readDataGuide(""); |
|
61 |
System.out.println(dataguide); |
|
62 |
} else { |
|
63 |
System.err.println("USAGE: java DBUtil <-dt|-dg>"); |
|
64 |
} |
|
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 |
/** |
|
75 |
* Construct an instance of the utility class |
|
76 |
*/ |
|
77 |
public DBUtil( Connection conn ) { |
|
78 |
this.conn = conn; |
|
79 |
} |
|
80 |
|
|
81 |
/** |
|
82 |
* read all doctypes from db connection in XML format |
|
83 |
* select all Public Id from xml_catalog table |
|
84 |
*/ |
|
85 |
public String readDoctypes() |
|
86 |
throws SQLException { |
|
87 |
|
|
88 |
Vector doctypeList = new Vector(); |
|
89 |
|
|
90 |
try { |
|
91 |
|
|
92 |
PreparedStatement pstmt = |
|
93 |
conn.prepareStatement("SELECT public_id FROM xml_catalog"); |
|
94 |
|
|
95 |
pstmt.execute(); |
|
96 |
ResultSet rs = pstmt.getResultSet(); |
|
97 |
boolean tableHasRows = rs.next(); |
|
98 |
while (tableHasRows) { |
|
99 |
doctypeList.addElement(rs.getString(1)); |
|
100 |
tableHasRows = rs.next(); |
|
101 |
} |
|
102 |
|
|
103 |
pstmt.close(); |
|
104 |
|
|
105 |
} catch (SQLException e) { |
|
106 |
System.out.println("DBUtil.readDoctypes(): " + e.getMessage()); |
|
107 |
throw e; |
|
108 |
} |
|
109 |
|
|
110 |
return formatToXML(doctypeList, "doctype"); |
|
111 |
} |
|
112 |
|
|
113 |
/** |
|
114 |
* read Data Guide for a given doctype from db connection in XML format |
|
115 |
* select all distinct absolute paths from xml_index table |
|
116 |
*/ |
|
117 |
public String readDataGuide( String doctype ) |
|
118 |
throws SQLException { |
|
119 |
|
|
120 |
Vector dataguide = new Vector(); |
|
121 |
String path; |
|
122 |
|
|
123 |
try { |
|
124 |
|
|
125 |
PreparedStatement pstmt = |
|
126 |
conn.prepareStatement("SELECT distinct path, nodeid FROM xml_index" + |
|
127 |
" WHERE path LIKE '/' || ? || '%'" + |
|
128 |
" ORDER BY nodeid"); |
|
129 |
pstmt.setString(1, doctype); |
|
130 |
pstmt.execute(); |
|
131 |
ResultSet rs = pstmt.getResultSet(); |
|
132 |
boolean tableHasRows = rs.next(); |
|
133 |
while (tableHasRows) { |
|
134 |
path = rs.getString(1); |
|
135 |
if ( dataguide.indexOf(path) == -1 ) { |
|
136 |
dataguide.addElement(path); |
|
137 |
} |
|
138 |
tableHasRows = rs.next(); |
|
139 |
} |
|
140 |
|
|
141 |
pstmt.close(); |
|
142 |
|
|
143 |
} catch (SQLException e) { |
|
144 |
System.out.println("DBUtil.readDoctypes(): " + e.getMessage()); |
|
145 |
throw e; |
|
146 |
} |
|
147 |
|
|
148 |
return formatToXML(dataguide, "path"); |
|
149 |
} |
|
150 |
|
|
151 |
/** |
|
152 |
* format the ResultSet to XML |
|
153 |
*/ |
|
154 |
private String formatToXML(Vector resultset, String tag) { |
|
155 |
|
|
156 |
String val = null; |
|
157 |
String doctype = null; |
|
158 |
StringBuffer result = new StringBuffer(); |
|
159 |
Enumeration rs = resultset.elements(); |
|
160 |
|
|
161 |
result.append("<?xml version=\"1.0\"?>\n"); |
|
162 |
result.append("<resultset>\n"); |
|
163 |
while (rs.hasMoreElements()) { |
|
164 |
val = (String)rs.nextElement(); |
|
165 |
result.append(" <" + tag + ">" + val + "</" + tag + ">\n"); |
|
166 |
} |
|
167 |
result.append("</resultset>\n"); |
|
168 |
|
|
169 |
return result.toString(); |
|
170 |
} |
|
171 |
|
|
172 |
|
|
173 |
} |
|
0 | 174 |
Also available in: Unified diff
utility class for reading various of data from db