001: /*
002: JOpenChart Java Charting Library and Toolkit
003: Copyright (C) 2001 Sebastian Müller
004: http://jopenchart.sourceforge.net
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: JDBCPlotter.java
021: Created on 9. October 2002
022: Based on SQLPlotter.java, created on 29. December 2001
023:
024: */
025:
026: package de.progra.charting.model;
027:
028: import java.sql.*;
029: import java.util.ArrayList;
030:
031: /**
032: * The class is used to convert database queries into ChartDataModels.
033: * You can initialize the Plotter with database parameters and afterwards
034: * you can run consecutive queries resulting in a new database.
035: */
036: public class JDBCPlotter {
037:
038: /** The SQL connection. */
039: protected Connection conn;
040:
041: /**
042: * Creates a new JDBCPlotter using the given driver and URL.
043: * @param jdbcDriver the fully qualified classname of the SQL driver class.
044: * @param jdbcURL the URL of the JDBC database to connect to.
045: * @param username the username for the JDBC resource
046: * @param password the user's password
047: */
048: public JDBCPlotter(String jdbcDriver, String jdbcURL,
049: String username, String password)
050: throws JDBCPlotterException {
051: try {
052: Class.forName(jdbcDriver);
053: conn = DriverManager.getConnection(jdbcURL, username,
054: password);
055: } catch (Exception e) {
056: throw new JDBCPlotterException(
057: "Exception while creating a database connection.",
058: e);
059: }
060: }
061:
062: /**
063: * Given a SQL query and the row titles this method creates a DefaultChartDataModel.
064: * The columns are initialized with values starting from 0.
065: * @param sqlQuery the SQL query to be performed
066: * @param sqlRows the rows from the ResultSet which should be included in the ChartDataModel and which
067: * will be used as the DataSet titles.
068: */
069: public DefaultChartDataModel createChartDataModelInstance(
070: String sqlQuery, String[] sqlRows)
071: throws JDBCPlotterException {
072: return createChartDataModelInstance(sqlQuery, sqlRows, sqlRows);
073: }
074:
075: /**
076: * Given a SQL query and the row titles this method creates a DefaultChartDataModel.
077: * The columns are initialized with values starting from 0.
078: * @param sqlQuery the SQL query to be performed
079: * @param sqlRows the rows from the ResultSet which should be included in the ChartDataModel
080: * @param dataSets the DataSet titles which should be given to the ChartDataModel instead of the sqlRows titles
081: */
082: public DefaultChartDataModel createChartDataModelInstance(
083: String sqlQuery, String[] sqlRows, String[] dataSets)
084: throws JDBCPlotterException {
085: try {
086: Statement stmt = conn.createStatement();
087: ResultSet sqlResult = stmt.executeQuery(sqlQuery);
088:
089: ArrayList model[] = new ArrayList[sqlRows.length];
090: ArrayList columnList = new ArrayList();
091:
092: for (int i = 0; i < model.length; i++) {
093: model[i] = new ArrayList();
094: }
095:
096: double x = 0.0;
097: while (sqlResult.next()) {
098:
099: columnList.add(new Double(x));
100: x += 1.0;
101:
102: for (int i = 0; i < sqlRows.length; i++) {
103: model[i].add(new Double(sqlResult
104: .getDouble(sqlRows[i])));
105:
106: }
107: }
108:
109: Number[][] modelArray = new Number[model.length][];
110:
111: for (int i = 0; i < model.length; i++)
112: modelArray[i] = (Number[]) model[i]
113: .toArray(new Number[0]);
114:
115: double[] columns = new double[columnList.size()];
116:
117: for (int i = 0; i < columns.length; i++)
118: columns[i] = ((Double) columnList.get(i)).doubleValue();
119:
120: return new DefaultChartDataModel(modelArray, columns,
121: dataSets);
122:
123: } catch (Exception e) {
124: throw new JDBCPlotterException(
125: "Exception while performing task.", e);
126: }
127: }
128:
129: /**
130: * Given a SQL query and the row titles this method creates a DefaultChartDataModel.
131: * The columns are initialized with values from columnRow.
132: * @param sqlQuery the SQL query to be performed
133: * @param columnRow the row from the ResultSet which should be taken as the column (x-axis) values
134: * @param sqlRows the rows from the ResultSet which should be included in the ChartDataModel and which
135: * will be used as the DataSet titles.
136: */
137: public DefaultChartDataModel createChartDataModelInstance(
138: String sqlQuery, String columnRow, String[] sqlRows)
139: throws JDBCPlotterException {
140: return createChartDataModelInstance(sqlQuery, columnRow,
141: sqlRows, sqlRows);
142: }
143:
144: /**
145: * Given a SQL query and the row titles this method creates a DefaultChartDataModel.
146: * The columns are initialized with values from columnRow.
147: * @param sqlQuery the SQL query to be performed
148: * @param columnRow the row from the ResultSet which should be taken as the column (x-axis) values
149: * @param sqlRows the rows from the ResultSet which should be included in the ChartDataModel
150: * @param dataSets the DataSet titles which should be given to the ChartDataModel instead of the sqlRows titles
151: */
152: public DefaultChartDataModel createChartDataModelInstance(
153: String sqlQuery, String columnRow, String[] sqlRows,
154: String[] dataSets) throws JDBCPlotterException {
155: try {
156: Statement stmt = conn.createStatement();
157: ResultSet sqlResult = stmt.executeQuery(sqlQuery);
158:
159: ArrayList model[] = new ArrayList[sqlRows.length];
160: ArrayList columnList = new ArrayList();
161:
162: for (int i = 0; i < model.length; i++) {
163: model[i] = new ArrayList();
164: }
165:
166: while (sqlResult.next()) {
167:
168: columnList.add(new Double(sqlResult
169: .getDouble(columnRow)));
170:
171: for (int i = 0; i < sqlRows.length; i++) {
172: model[i].add(new Double(sqlResult
173: .getDouble(sqlRows[i])));
174:
175: }
176: }
177:
178: Number[][] modelArray = new Number[model.length][];
179:
180: for (int i = 0; i < model.length; i++)
181: modelArray[i] = (Number[]) model[i]
182: .toArray(new Number[0]);
183:
184: double[] columns = new double[columnList.size()];
185:
186: for (int i = 0; i < columns.length; i++)
187: columns[i] = ((Double) columnList.get(i)).doubleValue();
188:
189: return new DefaultChartDataModel(modelArray, columns,
190: dataSets);
191: } catch (Exception e) {
192: throw new JDBCPlotterException(
193: "Exception while performing task.", e);
194: }
195: }
196:
197: /**
198: * Given a SQL query and the row titles this method creates a ObjectChartDataModel.
199: * The columns are initialized with values from row columnRow.
200: * @param sqlQuery the SQL query to be performed
201: * @param columnRow the row from the ResultSet which should be taken as the column (x-axis) values
202: * @param sqlRows the rows from the ResultSet which should be included in the ChartDataModel and which
203: * will be used as the DataSet titles.
204: */
205: public ObjectChartDataModel createObjectChartDataModelInstance(
206: String sqlQuery, String columnRow, String[] sqlRows)
207: throws JDBCPlotterException {
208: return createObjectChartDataModelInstance(sqlQuery, columnRow,
209: sqlRows, sqlRows);
210: }
211:
212: /**
213: * Given a SQL query and the row titles this method creates an ObjectChartDataModel.
214: * The columns are initialized with values of row columnRow.
215: * @param sqlQuery the SQL query to be performed
216: * @param columnRow the row from the ResultSet which should be taken as the column (x-axis) values
217: * @param sqlRows the rows from the ResultSet which should be included in the ChartDataModel
218: * @param dataSets the DataSet titles which should be given to the ChartDataModel instead of the sqlRows titles
219: */
220: public ObjectChartDataModel createObjectChartDataModelInstance(
221: String sqlQuery, String columnRow, String[] sqlRows,
222: String[] dataSets) throws JDBCPlotterException {
223: try {
224: Statement stmt = conn.createStatement();
225: ResultSet sqlResult = stmt.executeQuery(sqlQuery);
226:
227: ArrayList model[] = new ArrayList[sqlRows.length];
228: ArrayList columnList = new ArrayList();
229:
230: for (int i = 0; i < model.length; i++) {
231: model[i] = new ArrayList();
232: }
233:
234: while (sqlResult.next()) {
235:
236: columnList.add(sqlResult.getString(columnRow));
237:
238: for (int i = 0; i < sqlRows.length; i++) {
239: model[i].add(new Double(sqlResult
240: .getDouble(sqlRows[i])));
241:
242: }
243: }
244:
245: Number[][] modelArray = new Number[model.length][];
246:
247: for (int i = 0; i < model.length; i++)
248: modelArray[i] = (Number[]) model[i]
249: .toArray(new Number[0]);
250:
251: String[] columns = (String[]) columnList
252: .toArray(new String[0]);
253:
254: return new ObjectChartDataModel(modelArray, columns,
255: dataSets);
256: } catch (Exception e) {
257: throw new JDBCPlotterException(
258: "Exception while performing task.", e);
259: }
260: }
261: }
|