1 41 42 package org.jfree.data; 43 44 import java.sql.Connection ; 45 import java.sql.DriverManager ; 46 import java.sql.ResultSet ; 47 import java.sql.ResultSetMetaData ; 48 import java.sql.SQLException ; 49 import java.sql.Statement ; 50 import java.sql.Timestamp ; 51 import java.sql.Types ; 52 53 62 public class JDBCPieDataset extends DefaultPieDataset { 63 64 65 private Connection connection; 66 67 68 private Statement statement; 69 70 71 private ResultSet resultSet; 72 73 74 private ResultSetMetaData metaData; 75 76 79 public JDBCPieDataset() { 80 } 81 82 90 public JDBCPieDataset(String url, 91 String driverName, 92 String user, 93 String 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 ex) { 101 System.err.println("JDBCPieDataset: cannot find the database driver classes."); 102 System.err.println(ex); 103 } 104 catch (SQLException ex) { 105 System.err.println("JDBCPieDataset: cannot connect to this database."); 106 System.err.println(ex); 107 } 108 } 109 110 117 public JDBCPieDataset(Connection con) { 118 this.connection = con; 119 } 120 121 122 130 public JDBCPieDataset(Connection con, String query) { 131 this(con); 132 executeQuery(query); 133 } 134 135 144 public void executeQuery(String query) { 145 executeQuery(connection, query); 146 } 147 148 158 public void executeQuery(Connection con, String 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 ("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 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 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 sqle) { 209 System.err.println(sqle); 210 } 211 catch (Exception e) { 212 System.err.println(e); 213 } 214 215 finally { 216 if (resultSet != null) { 217 try { 218 resultSet.close(); 219 } 220 catch (Exception e) { 221 System.err.println("JDBCPieDataset: swallowing exception."); 222 } 223 } 224 if (statement != null) { 225 try { 226 statement.close(); 227 } 228 catch (Exception e) { 229 System.err.println("JDBCPieDataset: swallowing exception."); 230 } 231 } 232 } 233 } 234 235 } 236 | Popular Tags |