1 45 46 package org.jfree.data; 47 48 import java.sql.Connection ; 49 import java.sql.DriverManager ; 50 import java.sql.ResultSet ; 51 import java.sql.ResultSetMetaData ; 52 import java.sql.SQLException ; 53 import java.sql.Statement ; 54 import java.sql.Types ; 55 import java.util.Vector ; 56 57 66 public class JDBCXYDataset extends AbstractDataset implements XYDataset, RangeInfo { 67 68 69 Connection connection; 70 71 72 Statement statement; 73 74 75 ResultSet resultSet; 76 77 78 ResultSetMetaData metaData; 79 80 81 String [] columnNames = {}; 82 83 84 Vector rows = new Vector (0); 85 86 87 protected double maxValue = 0.0; 88 89 90 protected double minValue = 0.0; 91 92 93 public boolean isTimeSeries = false; 94 95 99 public JDBCXYDataset() { 100 } 101 102 110 public JDBCXYDataset(String url, 111 String driverName, 112 String user, 113 String passwd) { 114 115 try { 116 Class.forName(driverName); 117 connection = DriverManager.getConnection(url, user, passwd); 118 statement = connection.createStatement(); 119 } 120 catch (ClassNotFoundException ex) { 121 System.err.println("Cannot find the database driver classes."); 122 System.err.println(ex); 123 } 124 catch (SQLException ex) { 125 System.err.println("Cannot connect to this database."); 126 System.err.println(ex); 127 } 128 } 129 130 135 public JDBCXYDataset(Connection con) { 136 try { 137 connection = con; 138 } 139 catch (Exception e) { 140 e.printStackTrace(); 141 } 142 } 143 144 151 public JDBCXYDataset(Connection con, String query) { 152 this(con); 153 executeQuery(query); 154 } 155 156 166 public void executeQuery(String query) { 167 executeQuery(connection, query); 168 } 169 170 180 public void executeQuery(Connection con, String query) { 181 Object xObject = null; 182 int column = 0; 183 int currentColumn = 0; 184 int numberOfColumns = 0; 185 int numberOfValidColumns = 0; 186 int columnTypes[] = null; 187 188 if (con == null) { 189 System.err.println("There is no database to execute the query."); 190 return; 191 } 192 193 try { 194 statement = con.createStatement(); 195 resultSet = statement.executeQuery(query); 196 metaData = resultSet.getMetaData(); 197 198 numberOfColumns = metaData.getColumnCount(); 199 columnTypes = new int[numberOfColumns]; 200 for (column = 0; column < numberOfColumns; column++) { 201 try { 202 int type = metaData.getColumnType(column + 1); 203 switch (type) { 204 205 case Types.NUMERIC: 206 case Types.REAL: 207 case Types.INTEGER: 208 case Types.DOUBLE: 209 case Types.FLOAT: 210 case Types.DECIMAL: 211 case Types.BIT: 212 case Types.DATE: 213 case Types.TIME: 214 case Types.TIMESTAMP: 215 case Types.BIGINT: 216 ++numberOfValidColumns; 217 columnTypes[column] = type; 218 break; 219 default: 220 System.err.println("Unable to load column " 221 + column + " (" + type + "," 222 + metaData.getColumnClassName(column + 1) + ")"); 223 columnTypes[column] = Types.NULL; 224 break; 225 } 226 } 227 catch (SQLException e) { 228 e.printStackTrace(); 229 columnTypes[column] = Types.NULL; 230 } 231 } 232 233 234 if (numberOfValidColumns <= 1) { 235 System.err.println("Not enough valid columns where generated by query."); 236 return; 237 } 238 239 columnNames = new String [numberOfValidColumns - 1]; 241 currentColumn = 0; 243 for (column = 1; column < numberOfColumns; column++) { 244 if (columnTypes[column] != Types.NULL) { 245 columnNames[currentColumn] = metaData.getColumnLabel(column + 1); 246 ++currentColumn; 247 } 248 } 249 250 if (rows != null) { 252 for (column = 0; column < rows.size(); column++) { 253 Vector row = (Vector ) rows.get(column); 254 row.removeAllElements(); 255 } 256 rows.removeAllElements(); 257 } 258 259 switch (columnTypes[0]) { 261 case Types.DATE: 262 case Types.TIME: 263 case Types.TIMESTAMP: 264 isTimeSeries = true; 265 break; 266 default : 267 isTimeSeries = false; 268 break; 269 } 270 271 rows = new Vector (); 273 while (resultSet.next()) { 274 Vector newRow = new Vector (); 275 for (column = 0; column < numberOfColumns; column++) { 276 xObject = resultSet.getObject(column + 1); 277 switch (columnTypes[column]) { 278 case Types.NUMERIC: 279 case Types.REAL: 280 case Types.INTEGER: 281 case Types.DOUBLE: 282 case Types.FLOAT: 283 case Types.DECIMAL: 284 case Types.BIGINT: 285 newRow.addElement(xObject); 286 break; 287 288 case Types.DATE: 289 case Types.TIME: 290 case Types.TIMESTAMP: 291 newRow.addElement(new Long (((java.util.Date ) xObject).getTime())); 292 break; 293 case Types.NULL: 294 break; 295 default: 296 System.err.println("Unknown data"); 297 columnTypes[column] = Types.NULL; 298 break; 299 } 300 } 301 rows.addElement(newRow); 302 } 303 304 if (rows.size() == 0) { 306 Vector newRow = new Vector (); 307 for (column = 0; column < numberOfColumns; column++) { 308 if (columnTypes[column] != Types.NULL) { 309 newRow.addElement(new Integer (0)); 310 } 311 } 312 rows.addElement(newRow); 313 } 314 315 if (rows.size() < 1) { 317 maxValue = 0.0; 318 minValue = 0.0; 319 } 320 else { 321 Vector row = (Vector ) rows.elementAt(0); 322 double test; 323 maxValue = ((Number ) row.get(1)).doubleValue(); 324 minValue = maxValue; 325 for (int rowNum = 0; rowNum < rows.size(); ++rowNum) { 326 row = (Vector ) rows.elementAt(rowNum); 327 for (column = 1; column < numberOfColumns; column++) { 328 test = ((Number ) row.get(column)).doubleValue(); 329 if (test < minValue) { 330 minValue = test; 331 } 332 if (test > maxValue) { 333 maxValue = test; 334 } 335 } 336 } 337 } 338 339 fireDatasetChanged(); } 341 catch (SQLException ex) { 342 System.err.println(ex); 343 ex.printStackTrace(); 344 } 345 finally { 346 if (resultSet != null) { 347 try { 348 resultSet.close(); 349 } 350 catch (Exception e) { 351 } 352 } 353 if (statement != null) { 354 try { 355 statement.close(); 356 } 357 catch (Exception e) { 358 } 359 } 360 } 361 362 } 363 364 376 public Number getXValue(int seriesIndex, int itemIndex) { 377 Vector row = (Vector ) rows.elementAt(itemIndex); 378 return (Number ) row.elementAt(0); 379 } 380 381 391 public Number getYValue(int seriesIndex, int itemIndex) { 392 Vector row = (Vector ) rows.elementAt(itemIndex); 393 return (Number ) row.elementAt(seriesIndex + 1); 394 } 395 396 405 public int getItemCount(int seriesIndex) { 406 return rows.size(); 407 } 408 409 417 public int getSeriesCount() { 418 return columnNames.length; 419 } 420 421 431 public String getSeriesName(int seriesIndex) { 432 433 if ((seriesIndex < columnNames.length) && (columnNames[seriesIndex] != null)) { 434 return columnNames[seriesIndex]; 435 } 436 else { 437 return ""; 438 } 439 440 } 441 442 447 public int getLegendItemCount() { 448 return getSeriesCount(); 449 } 450 451 456 public String [] getLegendItemLabels() { 457 return columnNames; 458 } 459 460 467 public Number getMinimumRangeValue() { 468 return new Double (minValue); 469 } 470 471 478 public Number getMaximumRangeValue() { 479 return new Double (maxValue); 480 } 481 482 485 public void close() { 486 487 try { 488 resultSet.close(); 489 } 490 catch (Exception e) { 491 System.err.println("JdbcXYDataset: swallowing exception."); 492 } 493 try { 494 statement.close(); 495 } 496 catch (Exception e) { 497 System.err.println("JdbcXYDataset: swallowing exception."); 498 } 499 try { 500 connection.close(); 501 } 502 catch (Exception e) { 503 System.err.println("JdbcXYDataset: swallowing exception."); 504 } 505 506 } 507 508 513 public Range getValueRange() { 514 return new Range(minValue, maxValue); 515 } 516 517 } 518 | Popular Tags |