001: /*
002: * $Id: JDBCUrl.java,v 1.3 2002/02/24 02:10:19 skavish Exp $
003: *
004: * ==========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.url;
052:
053: import java.io.*;
054: import java.util.*;
055: import java.net.*;
056: import java.sql.*;
057: import org.openlaszlo.iv.flash.util.*;
058: import org.openlaszlo.iv.flash.api.*;
059:
060: /**
061: * Implementation of fgjdbc:// url
062: * <P>
063: * Syntax is: fgjdbc:///?driver=org.gjt.mm.mysql.Driver&url=jdbc:mysql://domen:3306/dbName&
064: * userid=username&password=password&query=select columnName from Table
065: * <p>
066: * The other parameters are (not supported now):
067: * <UL>
068: * <LI>lock
069: * <LI>cache
070: * <LI>share
071: * <LI>timeout
072: * </UL>
073: *
074: * @author Dmitry Skavish
075: */
076: public class JDBCUrl extends IVUrl {
077:
078: private String driver;
079: private String url;
080: private String userid;
081: private String password;
082: private String query;
083:
084: /*private String lock;
085: private String cache;
086: private String share;
087: private String timeout;
088: */
089:
090: public JDBCUrl(String surl) throws IVException {
091: parse(surl);
092: driver = getParameter("driver");
093: url = getParameter("url");
094: userid = getParameter("userid");
095: password = getParameter("password");
096: query = getParameter("query");
097: /* lock = getParameter("lock");
098: cache = getParameter("cache");
099: share = getParameter("share");
100: timeout = getParameter("timeout");
101: */
102: if (driver == null || url == null || query == null) {
103: throw new IVException(Resource.INVALURL,
104: new Object[] { surl });
105: }
106: }
107:
108: public String getName() {
109: return url + "@" + userid + "/" + query;
110: }
111:
112: public InputStream getInputStream() throws IOException {
113: return arrayToStream(getData(false));
114: }
115:
116: public boolean hasDataReady() {
117: return true;
118: }
119:
120: public String[][] getData() throws IOException {
121: return getData(true);
122: }
123:
124: private String[][] getData(boolean processEscapes)
125: throws IOException {
126: try {
127: Class.forName(driver);
128: } catch (Exception e) {
129: throw new IOException("driver not found");
130: }
131:
132: String[][] data = null;
133:
134: Connection conn = null;
135: try {
136: Log.logRB(Resource.SQLQUERY, new Object[] { driver, url,
137: userid, password, query });
138: if (userid == null) {
139: conn = DriverManager.getConnection(url);
140: } else {
141: conn = DriverManager.getConnection(url, userid,
142: password);
143: }
144: Statement stmt = conn.createStatement();
145:
146: ResultSet rs = stmt.executeQuery(query);
147: ResultSetMetaData meta = rs.getMetaData();
148:
149: int numCols = meta.getColumnCount();
150: String[] header = new String[numCols];
151: for (int i = 0; i < numCols; i++) {
152: header[i] = meta.getColumnLabel(i + 1);
153: }
154:
155: IVVector lines = new IVVector();
156: while (rs.next()) {
157: String[] line = new String[numCols];
158: for (int i = 0; i < numCols; i++) {
159: Object o = rs.getObject(i + 1);
160: String s = res2string(o);
161: line[i] = processEscapes ? Util.processEscapes(s)
162: : s;
163: }
164: lines.addElement(line);
165: }
166:
167: data = new String[lines.size() + 1][];
168: data[0] = header;
169: for (int i = 0; i < lines.size(); i++) {
170: data[i + 1] = (String[]) lines.elementAt(i);
171: }
172:
173: return data;
174: } catch (Exception e) {
175: throw new IOException(e.getMessage());
176: } finally {
177: try {
178: if (conn != null)
179: conn.close();
180: } catch (SQLException ee) {
181: throw new IOException(ee.getMessage());
182: }
183: }
184: }
185:
186: private String res2string(Object o) {
187: if (o == null)
188: return "";
189: if (o instanceof byte[])
190: return new String((byte[]) o);
191: return o.toString();
192: }
193: }
|