KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > data > JDBCPieDataset


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * -------------------
23  * JDBCPieDataset.java
24  * -------------------
25  * (C) Copyright 2002, 2003, by Bryan Scott and Contributors.
26  *
27  * Original Author: Bryan Scott; Andy
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * Changes
31  * -------
32  * 26-Apr-2002 : Creation based on JdbcXYDataSet, but extending DefaultPieDataset (BS);
33  * 24-Jun-2002 : Removed unnecessary import and local variable (DG);
34  * 13-Aug-2002 : Updated Javadoc comments and imports, removed default constructor (DG);
35  * 18-Sep-2002 : Updated to support BIGINT (BS);
36  * 21-Jan-2003 : Renamed JdbcPieDataset --> JDBCPieDataset (DG);
37  * 03-Feb-2003 : Added Types.DECIMAL (see bug report 677814) (DG);
38  * 05-Jun-2003 : Updated to support TIME, optimised executeQuery method (BS);
39  * 30-Jul-2003 : Added empty contructor and executeQuery(connection,string) method (BS);
40  */

41
42 package org.jfree.data;
43
44 import java.sql.Connection JavaDoc;
45 import java.sql.DriverManager JavaDoc;
46 import java.sql.ResultSet JavaDoc;
47 import java.sql.ResultSetMetaData JavaDoc;
48 import java.sql.SQLException JavaDoc;
49 import java.sql.Statement JavaDoc;
50 import java.sql.Timestamp JavaDoc;
51 import java.sql.Types JavaDoc;
52
53 /**
54  * A {@link PieDataset} that reads data from a database via JDBC.
55  * <P>
56  * A query should be supplied that returns data in two columns, the first containing
57  * VARCHAR data, and the second containing numerical data. The data is cached in-memory
58  * and can be refreshed at any time.
59  *
60  * @author Bryan Scott.
61  */

62 public class JDBCPieDataset extends DefaultPieDataset {
63
64     /** The database connection. */
65     private Connection JavaDoc connection;
66
67     /** A statement. */
68     private Statement JavaDoc statement;
69
70     /** The query result set. */
71     private ResultSet JavaDoc resultSet;
72
73     /** Meta data about the result set. */
74     private ResultSetMetaData JavaDoc metaData;
75
76     /**
77      * Creates a new JDBCPieDataset with no database connection.
78      */

79     public JDBCPieDataset() {
80     }
81
82     /**
83      * Creates a new JDBCPieDataset and establishes a new database connection.
84      *
85      * @param url the URL of the database connection.
86      * @param driverName The database driver class name.
87      * @param user The database user.
88      * @param passwd The database users password.
89      */

90     public JDBCPieDataset(String JavaDoc url,
91                           String JavaDoc driverName,
92                           String JavaDoc user,
93                           String JavaDoc passwd) {
94
95         try {
96             Class.forName(driverName);
97             this.connection = DriverManager.getConnection(url, user, passwd);
98             this.statement = connection.createStatement();
99         }
100         catch (ClassNotFoundException JavaDoc ex) {
101             System.err.println("JDBCPieDataset: cannot find the database driver classes.");
102             System.err.println(ex);
103         }
104         catch (SQLException JavaDoc ex) {
105             System.err.println("JDBCPieDataset: cannot connect to this database.");
106             System.err.println(ex);
107         }
108     }
109
110     /**
111      * Creates a new JDBCPieDataset using a pre-existing database connection.
112      * <P>
113      * The dataset is initially empty, since no query has been supplied yet.
114      *
115      * @param con the database connection.
116      */

117     public JDBCPieDataset(Connection JavaDoc con) {
118         this.connection = con;
119     }
120
121
122     /**
123      * Creates a new JDBCPieDataset using a pre-existing database connection.
124      * <P>
125      * The dataset is initialised with the supplied query.
126      *
127      * @param con the database connection.
128      * @param query the database connection.
129      */

130     public JDBCPieDataset(Connection JavaDoc con, String JavaDoc query) {
131         this(con);
132         executeQuery(query);
133     }
134
135     /**
136      * ExecuteQuery will attempt execute the query passed to it against the
137      * existing database connection. If no connection exists then no action
138      * is taken.
139      * The results from the query are extracted and cached locally, thus
140      * applying an upper limit on how many rows can be retrieved successfully.
141      *
142      * @param query The query to be executed
143      */

144     public void executeQuery(String JavaDoc query) {
145       executeQuery(connection, query);
146     }
147
148     /**
149      * ExecuteQuery will attempt execute the query passed to it against the
150      * existing database connection. If no connection exists then no action
151      * is taken.
152      * The results from the query are extracted and cached locally, thus
153      * applying an upper limit on how many rows can be retrieved successfully.
154      *
155      * @param query The query to be executed
156      * @param con The connection the query is to be executed against
157      */

158     public void executeQuery(Connection JavaDoc con, String JavaDoc query) {
159
160         if (con == null) {
161             System.err.println("There is no database to execute the query.");
162             return;
163         }
164
165         try {
166             statement = con.createStatement();
167             resultSet = statement.executeQuery(query);
168             metaData = resultSet.getMetaData();
169
170             int columnCount = metaData.getColumnCount();
171             if (columnCount != 2) {
172                 throw new Exception JavaDoc("Invalid sql generated. PieDataSet requires 2 columns only");
173             }
174
175             int columnType = metaData.getColumnType(2);
176             double value = Double.NaN;
177             while (resultSet.next()) {
178                 Comparable JavaDoc key = resultSet.getString(1);
179                 switch (columnType) {
180                     case Types.NUMERIC:
181                     case Types.REAL:
182                     case Types.INTEGER:
183                     case Types.DOUBLE:
184                     case Types.FLOAT:
185                     case Types.DECIMAL:
186                     case Types.BIGINT:
187                         value = resultSet.getDouble(2);
188                         setValue(key, value);
189                         break;
190
191                     case Types.DATE:
192                     case Types.TIME:
193                     case Types.TIMESTAMP:
194                         Timestamp JavaDoc date = resultSet.getTimestamp(2);
195                         value = date.getTime();
196                         setValue(key, value);
197                         break;
198
199                     default:
200                         System.err.println("JDBCPieDataset - unknown data type");
201                         break;
202                 }
203             }
204
205             fireDatasetChanged();
206
207         }
208         catch (SQLException JavaDoc sqle) {
209             System.err.println(sqle);
210         }
211         catch (Exception JavaDoc e) {
212             System.err.println(e);
213         }
214
215         finally {
216             if (resultSet != null) {
217                 try {
218                     resultSet.close();
219                 }
220                 catch (Exception JavaDoc e) {
221                     System.err.println("JDBCPieDataset: swallowing exception.");
222                 }
223             }
224             if (statement != null) {
225                 try {
226                     statement.close();
227                 }
228                 catch (Exception JavaDoc e) {
229                     System.err.println("JDBCPieDataset: swallowing exception.");
230                 }
231             }
232         }
233     }
234
235 }
236
Popular Tags