1 56 57 package org.jfree.data.jdbc; 58 59 import java.sql.Connection ; 60 import java.sql.DriverManager ; 61 import java.sql.SQLException ; 62 import java.sql.Timestamp ; 63 import java.sql.Types ; 64 import java.sql.ResultSet ; 65 import java.sql.ResultSetMetaData ; 66 import java.sql.Statement ; 67 68 import org.jfree.data.general.DefaultPieDataset; 69 import org.jfree.data.general.PieDataset; 70 71 80 public class JDBCPieDataset extends DefaultPieDataset { 81 82 83 private transient Connection connection; 84 85 97 public JDBCPieDataset(String url, 98 String driverName, 99 String user, 100 String password) 101 throws SQLException , ClassNotFoundException { 102 103 Class.forName(driverName); 104 this.connection = DriverManager.getConnection(url, user, password); 105 } 106 107 114 public JDBCPieDataset(Connection con) { 115 if (con == null) { 116 throw new NullPointerException ("A connection must be supplied."); 117 } 118 this.connection = con; 119 } 120 121 122 132 public JDBCPieDataset(Connection con, String query) throws SQLException { 133 this(con); 134 executeQuery(query); 135 } 136 137 148 public void executeQuery(String query) throws SQLException { 149 executeQuery(this.connection, query); 150 } 151 152 164 public void executeQuery(Connection con, String query) throws SQLException { 165 166 Statement statement = null; 167 ResultSet resultSet = null; 168 169 try { 170 statement = con.createStatement(); 171 resultSet = statement.executeQuery(query); 172 ResultSetMetaData metaData = resultSet.getMetaData(); 173 174 int columnCount = metaData.getColumnCount(); 175 if (columnCount != 2) { 176 throw new SQLException ( 177 "Invalid sql generated. PieDataSet requires 2 columns only" 178 ); 179 } 180 181 int columnType = metaData.getColumnType(2); 182 double value = Double.NaN; 183 while (resultSet.next()) { 184 Comparable key = resultSet.getString(1); 185 switch (columnType) { 186 case Types.NUMERIC: 187 case Types.REAL: 188 case Types.INTEGER: 189 case Types.DOUBLE: 190 case Types.FLOAT: 191 case Types.DECIMAL: 192 case Types.BIGINT: 193 value = resultSet.getDouble(2); 194 setValue(key, value); 195 break; 196 197 case Types.DATE: 198 case Types.TIME: 199 case Types.TIMESTAMP: 200 Timestamp date = resultSet.getTimestamp(2); 201 value = date.getTime(); 202 setValue(key, value); 203 break; 204 205 default: 206 System.err.println( 207 "JDBCPieDataset - unknown data type" 208 ); 209 break; 210 } 211 } 212 213 fireDatasetChanged(); 214 215 } 216 finally { 217 if (resultSet != null) { 218 try { 219 resultSet.close(); 220 } 221 catch (Exception e) { 222 System.err.println("JDBCPieDataset: swallowing exception."); 223 } 224 } 225 if (statement != null) { 226 try { 227 statement.close(); 228 } 229 catch (Exception e) { 230 System.err.println("JDBCPieDataset: swallowing exception."); 231 } 232 } 233 } 234 } 235 236 237 240 public void close() { 241 try { 242 this.connection.close(); 243 } 244 catch (Exception e) { 245 System.err.println("JdbcXYDataset: swallowing exception."); 246 } 247 } 248 } 249 | Popular Tags |