1 45 46 package org.jfree.data; 47 48 import java.sql.Connection ; 49 import java.sql.Date ; 50 import java.sql.DriverManager ; 51 import java.sql.ResultSet ; 52 import java.sql.ResultSetMetaData ; 53 import java.sql.SQLException ; 54 import java.sql.Statement ; 55 import java.sql.Types ; 56 57 74 public class JDBCCategoryDataset extends DefaultCategoryDataset { 75 76 77 private Connection connection; 78 79 80 private Statement statement; 81 82 83 private ResultSet resultSet; 84 85 86 private ResultSetMetaData metaData; 87 88 92 private boolean transpose = true; 93 94 98 public JDBCCategoryDataset() { 99 super(); 100 } 101 102 110 public JDBCCategoryDataset(String url, 111 String driverName, 112 String user, 113 String passwd) { 114 115 super(); 116 try { 117 Class.forName(driverName); 118 connection = DriverManager.getConnection(url, user, passwd); 119 this.statement = connection.createStatement(); 120 } 121 catch (ClassNotFoundException ex) { 122 System.err.println("JDBCCategoryDataset: cannot find the database driver classes."); 123 System.err.println(ex); 124 } 125 catch (SQLException ex) { 126 System.err.println("JDBCCategoryDataset: cannot connect to the database."); 127 System.err.println(ex); 128 } 129 } 130 131 136 public JDBCCategoryDataset(Connection connection) { 137 super(); 138 this.connection = connection; 139 } 140 141 148 public JDBCCategoryDataset(Connection connection, String query) { 149 this(connection); 150 executeQuery(query); 151 } 152 153 159 public boolean getTranspose() { 160 return this.transpose; 161 } 162 163 169 public void setTranspose(boolean transpose) { 170 this.transpose = transpose; 171 } 172 173 182 public void executeQuery(String query) { 183 executeQuery(connection, query); 184 } 185 186 196 public void executeQuery(Connection con, String query) { 197 198 if (con == null) { 200 System.err.println("JDBCCategoryDataset.executeQuery(...) : there is no connection."); 201 return; 202 } 203 204 try { 205 statement = con.createStatement(); 206 resultSet = statement.executeQuery(query); 207 metaData = resultSet.getMetaData(); 208 209 int columnCount = metaData.getColumnCount(); 210 211 if (columnCount < 2) { 212 System.err.println("JDBCCategoryDataset.executeQuery(...) : insufficient columns " 213 + "returned from the database."); 214 return; 215 } 216 217 int i = getRowCount(); 219 for (; i > 0; --i) { 220 removeRow(i); 221 } 222 223 while (resultSet.next()) { 224 Comparable rowKey = resultSet.getString(1); 226 for (int column = 2; column <= columnCount; column++) { 227 228 Comparable columnKey = metaData.getColumnName(column); 229 Number value = null; 230 int columnType = metaData.getColumnType(column); 231 232 switch (columnType) { 233 case Types.TINYINT: 234 case Types.SMALLINT: 235 case Types.INTEGER: 236 case Types.BIGINT: 237 case Types.FLOAT: 238 case Types.DOUBLE: 239 case Types.DECIMAL: 240 case Types.NUMERIC: 241 case Types.REAL: 242 value = (Number ) resultSet.getObject(column); 243 if (transpose) { 244 setValue(value, columnKey, rowKey); 245 } 246 else { 247 setValue(value, rowKey, columnKey); 248 } 249 break; 250 251 case Types.DATE: 252 case Types.TIME: 253 case Types.TIMESTAMP: 254 Date date = (Date ) resultSet.getObject(column); 255 value = new Long (date.getTime()); 256 if (transpose) { 257 setValue(value, columnKey, rowKey); 258 } 259 else { 260 setValue(value, rowKey, columnKey); 261 } 262 break; 263 264 case Types.CHAR: 265 case Types.VARCHAR: 266 case Types.LONGVARCHAR: 267 String string = (String ) resultSet.getObject(column); 268 try { 269 value = Double.valueOf(string); 270 if (transpose) { 271 setValue(value, columnKey, rowKey); 272 } 273 else { 274 setValue(value, rowKey, columnKey); 275 } 276 } 277 catch (NumberFormatException e) { 278 } 280 break; 281 282 default: 283 break; 285 } 286 } 287 } 288 289 fireDatasetChanged(); 290 } 291 catch (SQLException ex) { 292 System.err.println(ex); 293 } 294 finally { 295 if (resultSet != null) { 296 try { 297 resultSet.close(); 298 } 299 catch (Exception e) { 300 } 302 } 303 if (statement != null) { 304 try { 305 statement.close(); 306 } 307 catch (Exception e) { 308 } 310 } 311 } 312 } 313 314 } 315 | Popular Tags |