1 63 64 package org.jfree.data.jdbc; 65 66 import java.sql.Connection ; 67 import java.sql.Date ; 68 import java.sql.DriverManager ; 69 import java.sql.ResultSet ; 70 import java.sql.ResultSetMetaData ; 71 import java.sql.SQLException ; 72 import java.sql.Statement ; 73 import java.sql.Types ; 74 75 import org.jfree.data.category.CategoryDataset; 76 import org.jfree.data.category.DefaultCategoryDataset; 77 78 93 public class JDBCCategoryDataset extends DefaultCategoryDataset { 94 95 96 private transient Connection connection; 97 98 103 private boolean transpose = true; 104 105 106 118 public JDBCCategoryDataset(String url, 119 String driverName, 120 String user, 121 String passwd) 122 throws ClassNotFoundException , SQLException { 123 124 Class.forName(driverName); 125 this.connection = DriverManager.getConnection(url, user, passwd); 126 } 127 128 133 public JDBCCategoryDataset(Connection connection) { 134 if (connection == null) { 135 throw new NullPointerException ("A connection must be supplied."); 136 } 137 this.connection = connection; 138 } 139 140 149 public JDBCCategoryDataset(Connection connection, String query) 150 throws SQLException { 151 this(connection); 152 executeQuery(query); 153 } 154 155 161 public boolean getTranspose() { 162 return this.transpose; 163 } 164 165 171 public void setTranspose(boolean transpose) { 172 this.transpose = transpose; 173 } 174 175 187 public void executeQuery(String query) throws SQLException { 188 executeQuery(this.connection, query); 189 } 190 191 204 public void executeQuery(Connection con, String query) throws SQLException { 205 206 Statement statement = null; 207 ResultSet resultSet = null; 208 try { 209 statement = con.createStatement(); 210 resultSet = statement.executeQuery(query); 211 ResultSetMetaData metaData = resultSet.getMetaData(); 212 213 int columnCount = metaData.getColumnCount(); 214 215 if (columnCount < 2) { 216 throw new SQLException ( 217 "JDBCCategoryDataset.executeQuery() : insufficient columns " 218 + "returned from the database."); 219 } 220 221 int i = getRowCount(); 223 while (--i >= 0) { 224 removeRow(i); 225 } 226 227 while (resultSet.next()) { 228 Comparable rowKey = resultSet.getString(1); 230 for (int column = 2; column <= columnCount; column++) { 231 232 Comparable columnKey = metaData.getColumnName(column); 233 int columnType = metaData.getColumnType(column); 234 235 switch (columnType) { 236 case Types.TINYINT: 237 case Types.SMALLINT: 238 case Types.INTEGER: 239 case Types.BIGINT: 240 case Types.FLOAT: 241 case Types.DOUBLE: 242 case Types.DECIMAL: 243 case Types.NUMERIC: 244 case Types.REAL: { 245 Number value = (Number ) resultSet.getObject(column); 246 if (this.transpose) { 247 setValue(value, columnKey, rowKey); 248 } 249 else { 250 setValue(value, rowKey, columnKey); 251 } 252 break; 253 } 254 case Types.DATE: 255 case Types.TIME: 256 case Types.TIMESTAMP: { 257 Date date = (Date ) resultSet.getObject(column); 258 Number value = new Long (date.getTime()); 259 if (this.transpose) { 260 setValue(value, columnKey, rowKey); 261 } 262 else { 263 setValue(value, rowKey, columnKey); 264 } 265 break; 266 } 267 case Types.CHAR: 268 case Types.VARCHAR: 269 case Types.LONGVARCHAR: { 270 String string 271 = (String ) resultSet.getObject(column); 272 try { 273 Number value = Double.valueOf(string); 274 if (this.transpose) { 275 setValue(value, columnKey, rowKey); 276 } 277 else { 278 setValue(value, rowKey, columnKey); 279 } 280 } 281 catch (NumberFormatException e) { 282 } 284 break; 285 } 286 default: 287 break; 289 } 290 } 291 } 292 293 fireDatasetChanged(); 294 } 295 finally { 296 if (resultSet != null) { 297 try { 298 resultSet.close(); 299 } 300 catch (Exception e) { 301 } 303 } 304 if (statement != null) { 305 try { 306 statement.close(); 307 } 308 catch (Exception e) { 309 } 311 } 312 } 313 } 314 315 } 316 | Popular Tags |