KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > model > JDBCPlotter


1 /*
2  JOpenChart Java Charting Library and Toolkit
3  Copyright (C) 2001 Sebastian Müller
4  http://jopenchart.sourceforge.net
5
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20  JDBCPlotter.java
21  Created on 9. October 2002
22  Based on SQLPlotter.java, created on 29. December 2001
23  
24  */

25
26 package de.progra.charting.model;
27
28 import java.sql.*;
29 import java.util.ArrayList JavaDoc;
30
31
32 /**
33  * The class is used to convert database queries into ChartDataModels.
34  * You can initialize the Plotter with database parameters and afterwards
35  * you can run consecutive queries resulting in a new database.
36  */

37 public class JDBCPlotter {
38
39     /** The SQL connection. */
40     protected Connection conn;
41
42     /**
43      * Creates a new JDBCPlotter using the given driver and URL.
44      * @param jdbcDriver the fully qualified classname of the SQL driver class.
45      * @param jdbcURL the URL of the JDBC database to connect to.
46      * @param username the username for the JDBC resource
47      * @param password the user's password
48      */

49     public JDBCPlotter(String JavaDoc jdbcDriver, String JavaDoc jdbcURL, String JavaDoc username, String JavaDoc password)
50             throws JDBCPlotterException {
51         try {
52             Class.forName( jdbcDriver);
53             conn = DriverManager.getConnection(jdbcURL, username, password);
54         } catch(Exception JavaDoc e) {
55             throw new JDBCPlotterException("Exception while creating a database connection.", e);
56         }
57     }
58
59     /**
60      * Given a SQL query and the row titles this method creates a DefaultChartDataModel.
61      * The columns are initialized with values starting from 0.
62      * @param sqlQuery the SQL query to be performed
63      * @param sqlRows the rows from the ResultSet which should be included in the ChartDataModel and which
64      * will be used as the DataSet titles.
65      */

66     public DefaultChartDataModel createChartDataModelInstance(String JavaDoc sqlQuery, String JavaDoc[] sqlRows)
67             throws JDBCPlotterException {
68         return createChartDataModelInstance(sqlQuery, sqlRows, sqlRows);
69     }
70     
71     /**
72      * Given a SQL query and the row titles this method creates a DefaultChartDataModel.
73      * The columns are initialized with values starting from 0.
74      * @param sqlQuery the SQL query to be performed
75      * @param sqlRows the rows from the ResultSet which should be included in the ChartDataModel
76      * @param dataSets the DataSet titles which should be given to the ChartDataModel instead of the sqlRows titles
77      */

78     public DefaultChartDataModel createChartDataModelInstance(String JavaDoc sqlQuery, String JavaDoc[] sqlRows, String JavaDoc[] dataSets)
79             throws JDBCPlotterException {
80         try {
81             Statement stmt = conn.createStatement();
82             ResultSet sqlResult = stmt.executeQuery(sqlQuery);
83
84             ArrayList JavaDoc model[] = new ArrayList JavaDoc[sqlRows.length];
85             ArrayList JavaDoc columnList = new ArrayList JavaDoc();
86
87             for(int i = 0; i < model.length; i++) {
88                 model[i]=new ArrayList JavaDoc();
89             }
90
91             double x = 0.0;
92             while(sqlResult.next()) {
93
94                 columnList.add(new Double JavaDoc(x));
95                 x += 1.0;
96
97                 for(int i = 0; i < sqlRows.length; i++) {
98                     model[i].add(new Double JavaDoc(sqlResult.getDouble(sqlRows[i])));
99
100                 }
101             }
102
103             Number JavaDoc[][] modelArray = new Number JavaDoc[model.length][];
104             
105             for(int i = 0; i < model.length; i++)
106                 modelArray[i] = (Number JavaDoc[])model[i].toArray(new Number JavaDoc[0]);
107             
108             double[] columns = new double[columnList.size()];
109             
110             for(int i = 0; i < columns.length; i++)
111                 columns[i] = ((Double JavaDoc)columnList.get(i)).doubleValue();
112             
113             return new DefaultChartDataModel(modelArray, columns, dataSets);
114             
115         } catch( Exception JavaDoc e ) {
116             throw new JDBCPlotterException("Exception while performing task.", e);
117         }
118     }
119
120     /**
121      * Given a SQL query and the row titles this method creates a DefaultChartDataModel.
122      * The columns are initialized with values from columnRow.
123      * @param sqlQuery the SQL query to be performed
124      * @param columnRow the row from the ResultSet which should be taken as the column (x-axis) values
125      * @param sqlRows the rows from the ResultSet which should be included in the ChartDataModel and which
126      * will be used as the DataSet titles.
127      */

128     public DefaultChartDataModel createChartDataModelInstance(String JavaDoc sqlQuery, String JavaDoc columnRow, String JavaDoc[] sqlRows)
129             throws JDBCPlotterException {
130         return createChartDataModelInstance(sqlQuery, columnRow, sqlRows, sqlRows);
131     }
132     
133     /**
134      * Given a SQL query and the row titles this method creates a DefaultChartDataModel.
135      * The columns are initialized with values from columnRow.
136      * @param sqlQuery the SQL query to be performed
137      * @param columnRow the row from the ResultSet which should be taken as the column (x-axis) values
138      * @param sqlRows the rows from the ResultSet which should be included in the ChartDataModel
139      * @param dataSets the DataSet titles which should be given to the ChartDataModel instead of the sqlRows titles
140      */

141     public DefaultChartDataModel createChartDataModelInstance(String JavaDoc sqlQuery, String JavaDoc columnRow, String JavaDoc[] sqlRows, String JavaDoc[] dataSets)
142             throws JDBCPlotterException {
143         try {
144             Statement stmt = conn.createStatement();
145             ResultSet sqlResult = stmt.executeQuery(sqlQuery);
146
147             ArrayList JavaDoc model[] = new ArrayList JavaDoc[sqlRows.length];
148             ArrayList JavaDoc columnList = new ArrayList JavaDoc();
149
150             for(int i = 0; i < model.length; i++) {
151                 model[i]=new ArrayList JavaDoc();
152             }
153
154             while(sqlResult.next()) {
155
156                 columnList.add(new Double JavaDoc(sqlResult.getDouble(columnRow)));
157                 
158                 for(int i = 0; i < sqlRows.length; i++) {
159                     model[i].add(new Double JavaDoc(sqlResult.getDouble(sqlRows[i])));
160
161                 }
162             }
163
164             Number JavaDoc[][] modelArray = new Number JavaDoc[model.length][];
165             
166             for(int i = 0; i < model.length; i++)
167                 modelArray[i] = (Number JavaDoc[])model[i].toArray(new Number JavaDoc[0]);
168             
169             double[] columns = new double[columnList.size()];
170             
171             for(int i = 0; i < columns.length; i++)
172                 columns[i] = ((Double JavaDoc)columnList.get(i)).doubleValue();
173             
174             return new DefaultChartDataModel(modelArray, columns, dataSets);
175         } catch( Exception JavaDoc e ) {
176             throw new JDBCPlotterException("Exception while performing task.", e);
177         }
178     }
179
180     /**
181      * Given a SQL query and the row titles this method creates a ObjectChartDataModel.
182      * The columns are initialized with values from row columnRow.
183      * @param sqlQuery the SQL query to be performed
184      * @param columnRow the row from the ResultSet which should be taken as the column (x-axis) values
185      * @param sqlRows the rows from the ResultSet which should be included in the ChartDataModel and which
186      * will be used as the DataSet titles.
187      */

188     public ObjectChartDataModel createObjectChartDataModelInstance(String JavaDoc sqlQuery, String JavaDoc columnRow, String JavaDoc[] sqlRows)
189             throws JDBCPlotterException {
190         return createObjectChartDataModelInstance(sqlQuery, columnRow, sqlRows, sqlRows);
191     }
192
193     /**
194      * Given a SQL query and the row titles this method creates an ObjectChartDataModel.
195      * The columns are initialized with values of row columnRow.
196      * @param sqlQuery the SQL query to be performed
197      * @param columnRow the row from the ResultSet which should be taken as the column (x-axis) values
198      * @param sqlRows the rows from the ResultSet which should be included in the ChartDataModel
199      * @param dataSets the DataSet titles which should be given to the ChartDataModel instead of the sqlRows titles
200      */

201     public ObjectChartDataModel createObjectChartDataModelInstance(String JavaDoc sqlQuery, String JavaDoc columnRow, String JavaDoc[] sqlRows, String JavaDoc[] dataSets)
202             throws JDBCPlotterException {
203         try {
204             Statement stmt = conn.createStatement();
205             ResultSet sqlResult = stmt.executeQuery(sqlQuery);
206
207             ArrayList JavaDoc model[] = new ArrayList JavaDoc[sqlRows.length];
208             ArrayList JavaDoc columnList = new ArrayList JavaDoc();
209
210             for(int i = 0; i < model.length; i++) {
211                 model[i]=new ArrayList JavaDoc();
212             }
213
214             while(sqlResult.next()) {
215
216                 columnList.add(sqlResult.getString(columnRow));
217
218                 for(int i = 0; i < sqlRows.length; i++) {
219                     model[i].add(new Double JavaDoc(sqlResult.getDouble(sqlRows[i])));
220
221                 }
222             }
223
224             Number JavaDoc[][] modelArray = new Number JavaDoc[model.length][];
225             
226             for(int i = 0; i < model.length; i++)
227                 modelArray[i] = (Number JavaDoc[])model[i].toArray(new Number JavaDoc[0]);
228             
229             String JavaDoc[] columns = (String JavaDoc[])columnList.toArray(new String JavaDoc[0]);
230             
231             return new ObjectChartDataModel(modelArray, columns, dataSets);
232         } catch( Exception JavaDoc e ) {
233             throw new JDBCPlotterException("Exception while performing task.", e);
234         }
235     }
236 }
237
Popular Tags